docker 简单启动 nginx
下载镜像
官方镜像仓库:hub.docker.com
搜索:nginx
bash
docker pull nginx #下载
docker images #查看镜像列表
启动
bash
docker run --name nginx -p 80:80 -d nginx #启动
docker ps # 查看容器列表
--name 给你启动的容器起个名字,以后可以使用这个名字启动或者停止容器
-p 映射端口,将docker宿主机的80端口和容器的80端口进行绑定
-d 后台启动
已经做了宿主机80端口映射, 浏览器输入:http://localhost/ 即可访问到 nginx页面
docker 运行react 前端
创建react项目
bash
npx create-react-app docker-react
cd docker-react
新建Dockerfile 和 .dockerignore 文件
在项目根目录下创建这2文件
bash
FROM node:alpine
RUN mkdir /app
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm", "start"]
.dockerignore
lua
node_modules
npm-debug.log
创建镜像
bash
docker build -t <yourname/projectname> .
创建并运行容器
arduino
docker run -d -it -p 3000:3000 <yourname/projectname>
浏览器访问
现在打开你的浏览器并前往 http://localhost:3000/,你将会看到以下输出。
docker 运行 Springboot
新建 Springboot项目
idea New Project
添加代码
非常简单的 Web 项目,只有一个控制器,代码如下:
less
@RequestMapping
@RestController
public class DockerController {
@RequestMapping("/")
public String hello() {
return "Docker, Hello ~";
}
}
application.properties:
ini
server.port= 8082
配置pom.xml
这里用到了docker插件:dockerfile-maven-plugin
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dcoker</groupId>
<artifactId>docker-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>docker-springboot</name>
<description>Docker Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>limoss</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
配置Dockerfile
项目根目录下新建 Dockerfile文件:
css
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 8082
ENTRYPOINT ["java","-jar","/app.jar"]
项目打包及docker build
打包之前 把pom中相关的依赖下载完整
运行 docker run
arduino
run -d -p 8082:8082 limoss/docker-springboot
下面命令查看运行情况
docker ps
docker logs containerId
成功访问
浏览器输入:http://localhost:8082/