Home >Java >javaTutorial >How to use dockerfile to deploy springboot project
Dockerfile is a text file used to build a Docker image. It is a script composed of instructions and parameters required to build the image.
1: Each reserved word instruction must be an uppercase letter
and must be followed by at least one parameter
2: The instructions are as above Go to the bottom and execute it sequentially
3: # represents a comment
4: Each instruction will create a new image layer and submit the image
(1) docker runs a container from the base image
(2) Execute an instruction and make modifications to the container
(3) Perform an operation similar to docker commit to submit a new image layer
(4 ) Docker then runs a new container based on the just submitted image
(5) Execute the next instruction in the dockerfile until all instructions are executed
From the perspective of application software, Dockerfile, Docker image and Docker container respectively represent three different stages of software.
Dockerfile is software The raw material
Docker image is the deliverable of the software
The Docker container can be considered as the running state of the software image, that is, it runs according to the image Container instance
Dockerfile is oriented to development, Docker image becomes the delivery standard, and Docker container involves deployment and operation and maintenance. The three are indispensable, and together they serve as the cornerstone of the Docker system.
1 Dockerfile, you need to define a Dockerfile, which defines everything the process needs. The content involved in Dockerfile includes executing code or files, environment variables, dependent packages, runtime environment, dynamic link library, operating system distribution, service process and kernel process (when the application process needs to deal with system services and kernel processes, this You need to consider how to design namespace permission control), etc.;
2 Docker image, after defining a file with Dockerfile, a Docker image will be generated during docker build, and when the Docker image is run, it will actually start to provide services;
3 Docker container, the container directly provides services.
Basic construction steps: (1) Write the Dockerfile file (2) docker build command to build the image (3) docker run to run the container instance according to the image
Basic image. Which image is the current new image based on? Specify an existing image as a template. The first command must be FROM
# 命令格式 FROM [--platform=<platform>] <image> [AS <name>] FROM [--platform=<platform>] <image>[:<tag>] [AS <name>] FROM [--platform=<platform>] <image>[@<digest>] [AS <name>] # 实例: FROM centos:7.0
Name and email address of the image maintainer
# 命令格式 MAINTAINER <name>
This command has been deprecated. LABEL is recommended, it can set any metadata you need.
# 命令格式 LABEL <key>=<value> <key>=<value> <key>=<value> ... LABEL "com.example.vendor"="ACME Incorporated" LABEL com.example.label-with-value="foo" LABEL version="1.0" LABEL description="This text illustrates \ that label-values can span multiple lines."
The command that needs to be run when building the container. RUN is run during docker build
(1)shell format (commonly used)
# <命令行命令> 等同于,在终端操作的shell命令 RUN <命令行命令>
(2) exec format
# 录入:RUN ["./test.php", "dev", "offline"] 等价于RUN ./test.php dev offline RUN ["可执行文件", "param1", "param2"]
The port exposed by the current container to the outside world
EXPOSE 80 EXPOSE 80/tcp EXPOSE 80/udp
specified when creating the container After that, the terminal logs in by default and enters the working directory. A starting point is
WORKDIR /path/to/workdir
specifies the user to execute the image. If neither is specified, the default is root
# 命令格式 USER <user>[:<group>] USER <UID>[:<GID>] # 实例 USER cxf
is used to set environment variables during the image building process
# 命令格式 ENV <key>=<value> ... # 实例 ENV MY_PATH /usr/mytest #这个环境变量可以在后续的任何RUN指令中使用,这就如同在命令前面指定了环境变量前缀一样;也可以在其它指令中直接使用这些环境变量, #比如:WORKDIR $MY_PATH
Copy the files in the host directory into the image And it will automatically process the URL and decompress the tar compressed package
ADD a.tar.gz /mydir/
, similar to ADD, copy files and directories to the image.
Copy the files/directories from the
COPY src dest COPY ["src", "dest"] <src源路径>:源文件或者源目录 <dest目标路径>:容器内的指定路径,该路径不用事先建好,路径不存在的话,会自动创建。
Container data volume, used for data storage and persistence
Specify what to do after the container is started
Attention! There can be multiple CMD instructions in the Dockerfile, but only the last one takes effect, CMD will be replaced by the parameters after docker run
CMD ["executable","param1","param2"] CMD ["param1","param2"] CMD command param1 param2 # 覆盖默认的CMD,不会启动tomcat docker exec -it comcat /bin/bash
CMD is run when docker run. RUN is run during docker build.
is also used to specify the command to be run when a container starts.
Similar to the CMD command, but ENTRYPOINT will not be overwritten by the command after docker run
, and these command line parameters will be used as parameters to the program specified by the ENTRYPOINT command
# 命令格式 ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT command param1 param2
ENTRYPOINT can be used together with CMD. Generally, CMD is used to change parameters. CMD here is equivalent to passing parameters to ENTRYPOINT.
When ENTRYPOINT is specified, the meaning of CMD changes. Instead of running its command directly, the content of CMD is passed as a parameter to the ENTRYPOINT instruction. The combination of the two will become:
FROM ubuntu ENTRYPOINT ["top", "-b"] # 定参 CMD ["-c"] # 变参 # 最终执行top -c,而如果使用命令行指定 -H,会替换CMD的-c
When executing docker run, you can specify the parameters required for ENTRYPOINT operation. If there are multiple ENTRYPOINT instructions in the Dockerfile, only the last one will take effect.
# 注意大写字母D vi Dockerfile
# 指定基础镜像,需要保证本地有这个镜像 FROM centos # 指定作者邮箱 MAINTAINER cxf # 工作目录 ENV MYPATH /usr/local WORKDIR $MYPATH # 准备工作 RUN cd /etc/yum.repos.d/ RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-* RUN yum makecache RUN cd /usr/local #安装vim编辑器 RUN yum -y install vim #安装ifconfig命令查看网络IP RUN yum -y install net-tools #安装java8及lib库 RUN yum -y install glibc.i686 RUN mkdir /usr/local/java #ADD 是相对路径jar,把jdk-8u351-linux-x64.tar.gz添加到容器中,安装包必须要和Dockerfile文件在同一位置 ADD jdk-8u351-linux-x64.tar.gz /usr/local/java/ #配置java环境变量 ENV JAVA_HOME /usr/local/java/jdk1.8.0_351 ENV JRE_HOME $JAVA_HOME/jre ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH ENV PATH $JAVA_HOME/bin:$PATH EXPOSE 80 CMD echo $MYPATH CMD echo "success--------------ok" CMD /bin/bash
注意!本目录下只有一个Dockerfile并且包含jdk安装包
# 构建新镜像命令格式 。注意,上面TAG后面有个空格,有个点 docker build -t 新镜像名字:TAG . # 构建我们的镜像,等一会就会构建成功 docker build -t centosjava8:1.0 . [root@localhost myfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centosjava8 1.0 4dbff0755585 34 seconds ago 747MB centos latest 5d0da3dc9764 19 months ago 231MB
docker run -it 4dbff0755585 /bin/bash #我们发现,新的镜像携带着vim、ifconfig和java环境了!
仓库名、标签都是
(1)创建Dockerfile
FROM centos CMD echo 'action is success'
(2)构建
docker build .
(3)查看
[root@localhost xu]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centosjava8 1.0 4dbff0755585 31 minutes ago 747MB <none> <none> 9818ccf3738e 19 months ago 231MB centos latest 5d0da3dc9764 19 months ago 231MB # 查看所有虚悬镜像 [root@localhost xu]# docker image ls -f dangling=true REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 9818ccf3738e 19 months ago 231MB
(4)删除
# 删除虚悬镜像 docker image prune
虚悬镜像已经失去存在价值,可以删除
编写一个测试Controller,以供测试。
@RestController @RequestMapping public class TestController { @GetMapping("/test") public String test(){ return "启动成功 ===================test"; } }
我们使用maven package命令手动打包并上传到服务器上。
[root@localhost ~]# cd mydocker/ [root@localhost mydocker]# ll total 17084 -rw-r--r--. 1 root root 17491177 Apr 9 14:18 demo-0.0.1-SNAPSHOT.jar
注!通常来说这一步可以使用jenkins自动构建。
# 基础镜像使用java FROM centosjava8:1.0 # 作者 MAINTAINER cxf # VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp VOLUME /tmp # 将jar包添加到容器中并更名为cxf_docker.jar ADD demo-0.0.1-SNAPSHOT.jar /cxf_docker.jar # 运行jar包 RUN bash -c 'touch /cxf_docker.jar' ENTRYPOINT ["java","-jar","/cxf_docker.jar"] #暴露6001端口作为微服务 EXPOSE 8088
docker build -t mydemo:1.0 . [root@localhost mydocker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mydemo 1.0 7124eca083ad 26 seconds ago 764MB centosjava8 1.0 4dbff0755585 7 hours ago 747MB
docker run -d -p 8088:8088 7124eca083ad
访问服务器我们的接口,发现测试成功!
The above is the detailed content of How to use dockerfile to deploy springboot project. For more information, please follow other related articles on the PHP Chinese website!