Docker学习笔记03-----使用Dockerfile构建SpringBoot项目Docker镜像

1、创建SpringBoot项目

2、编写测试Controller

@RestController
public class HelloWorld {

    @GetMapping("/hellodocker")
    public String sayHello() {
        return "Hello Docker";
    }
}

3、使用maven将项目打成jar包

mvn clean package

4、在服务器上创建目录test_dockerfile

mkdir -p /root/test_dockerfile

5、将helloworld.jar上传到/root/test_dockerfile

6、在/root/test_dockerfile目录创建Dockerfile文件

FROM java:8
MAINTAINER itmrl
LABEL name="helloworld" version="1.0.0" author="itmrl"
COPY helloworld.jar helloworld.jar
EXPOSE 8080
CMD ["java","-jar","-Dfile.encoding=UTF-8","-Xms1024m","-Xmx1024m","helloworld.jar"]

7、使用当前目录的Dockerfile构建镜像

[root@localhost test_dockerfile]# docker build -t helloworld:1.0.0 .
Sending build context to Docker daemon   16.3MB
Step 1/6 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/6 : MAINTAINER itmrl
 ---> Running in 47ce209a095c
Removing intermediate container 47ce209a095c
 ---> 120755b80993
Step 3/6 : LABEL name="helloworld" version="1.0.0" author="itmrl"
 ---> Running in 3afaab82019e
Removing intermediate container 3afaab82019e
 ---> abb2d50ac587
Step 4/6 : COPY helloworld.jar helloworld.jar
 ---> e32226c676eb
Step 5/6 : EXPOSE 8080
 ---> Running in 9392954ee72b
Removing intermediate container 9392954ee72b
 ---> 8cfe98171a1e
Step 6/6 : CMD ["java","-jar","-Dfile.encoding=UTF-8","-Xms1024m","-Xmx1024m","helloworld.jar"]
 ---> Running in ee8f5b6eb4b3
Removing intermediate container ee8f5b6eb4b3
 ---> 4a97d3158956
Successfully built 4a97d3158956
Successfully tagged helloworld:1.0.0

8、基于镜像创建容器

[root@localhost test_dockerfile]# docker run -d --name my-helloworld -p 8888:8080 helloworld:1.0.0
0de475687d4b724afb1d9f144ac70ba82c9032a633898b4419a734c81aeb1be2

9、查看容器

[root@localhost test_dockerfile]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
0de475687d4b        helloworld:1.0.0    "java -jar -Dfile.en…"   5 seconds ago       Up 4 seconds        0.0.0.0:8888->8080/tcp               my-helloworld

10、验证

Docker学习笔记03-----使用Dockerfile构建SpringBoot项目Docker镜像_第1张图片

你可能感兴趣的:(Docker,docker,spring,boot,maven)