一、定义
Dockerfile是用来构建Docker镜像的文本文件,是由一条条构建镜像所需的指令和参数构成的脚本。
二、使用DockerFile构建镜像
image.png1.构建步骤
- 编写Dockerfile文件
- docker build构建镜像
- docker run 依镜像运行容器实例
2.执行流程:
- docker从基础镜像运行一个容器
- 执行一条指令并对容器作出修改
- 执行类似docker commit的操作提交一个新的镜像层
- docker再基于刚提交的镜像运行一个新容器
- 执行dockerfile中的下一条指令直到所有指令都执行完成
FROM alpine:latest 4e38e38c7ct0
MAINTAINER hcx fb1gail4315b
CMD echo 'hello docker' 8gf243gsfe8
镜像分层.png
3.三者关系
image.png三、DockerFile常用保留字指令
命令 | 用途 |
---|---|
FROM | base image ,指定一个基础镜像;(当前新镜像是基于哪个镜像的,指定一个已经存在的镜像作为模板,第一条必须是from) |
RUN | 执行命令 ,在容器内执行一个命令;(在 docker build时运行) |
ADD | 添加文件(将宿主机目录下的文件拷贝进镜像且会自动处理URL和解压tar压缩包) |
COPY | 拷贝文件(类似ADD,拷贝宿主机的文件/目录到镜像中) |
CMD | 执行命令 (指定容器启动后的要干的事情,在docker run 时运行) |
EXPOSE | 暴露端口(当前容器对外暴露出的端口) |
WORKDIR | 指定命令运行的路径;(指定在创建容器后,终端默认登陆的进来工作目录,一个落脚点) |
MAINTAINER | 维护者,镜像维护者的姓名和邮箱地址 |
ENV | 设定环境变量(在构建镜像过程中设置环境变量) |
ENTRYPOINT | 容器入口,指定一个容器启动时要运行的命令 |
USER | 指定用户,指定执行该命令的用户;(指定该镜像以什么样的用户去执行,如果都不指定,默认是root) |
VOLUME | mount point,指定容器所挂载的卷;(用于数据保存和持久化工作) |
- 每条保留字指令都必须为大写字母且后面要跟随至少一个参数
- 指令按照从上到下,顺序执行
- 每条指令都会创建一个新的镜像层并对镜像进行提交
1.RUN命令
- shell格式:
RUN <命令行命令>
- exec格式:
RUN ["可执行文件","参数1","参数2"]
2.ENV命令
- 声明:ENV MY_PATH /usr/hcx
- 使用:WORKDIR $MY_PATH
3.CMD命令
指定容器启动后要运行的命令。
- shell格式:
CMD <命令>
- exec格式:
CMD ["可执行文件","参数1","参数2",···]
- 参数列表:
CMD ["参数1","参数2",····]
;在指定了ENTRYPOINT指令后,用CMD指定具体的参数。
CMD指令可以有多个,但只有最后一个生效,CMD会被 docker run 之后的参数替换
4.ENTRYPOINT命令
指定容器启动后要运行的命令。类似CMD,但是ENTRYPOINT不会被docker run后面的命令覆盖,这些命令行参数会被当作参数传递给ENTRYPOINT指令指定的程序。
- ENTRYPOINT["executeable","<param1>","<param1>",···]
即CMD等同于给ENTRYPOINT传参。当指定了ENTRYPOINT后,CMD的内容会作为参数传递给ENTRYPOINT指令:[<ENTRYPOINT>"<CMD>"]
FROM nginx
ENTRYPOINT ["nginx","-c"]
CMD ["/etc/nginx/nginx.conf"]
- 不加参数正常运行:
nginx -c /etc/nginx/nginx.conf
- 手动加参数运行:docker run nginx -c /etc/nginx/nginx1.conf 就会变成:
nginx -c /etc/nginx/nginx1.conf
,即nginx1.conf会覆盖掉原本的nginx.conf
Dockerfile 中可以有多个 CMD 指令,但只有最后一个生效,CMD 会被 docker run 之后的参数替换
demo:
redis的dockerfile(部分内容):
FROM debian:bullseye-slim # 基于debian:bullseye-slim镜像构建
RUN groupadd -r -g 999 redis && useradd -r -g redis -u 999 redis
# 环境变量 可以在后续的run命令中使用 key-value $key来引用变量 例:$GOSU_VERSION等同于1.16
ENV GOSU_VERSION 1.16
ENV REDIS_VERSION 6.2.12
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-6.2.12.tar.gz
ENV REDIS_DOWNLOAD_SHA 75352eef41e97e84bfa94292cbac79e5add5345fc79787df5cbdff703353fb1b
RUN mkdir /data && chown redis:redis /data
VOLUME /data
WORKDIR /data # 容器创建成功后,通过终端登录进来默认在/data目录下
COPY docker-entrypoint.sh /usr/local/bin/ # 拷贝docker-entrypoint.sh到镜像中的/usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 6379 # 容器暴露6379端口
CMD ["redis-server"]
四、自定义镜像
在基础的centos镜像上安装java环境。
1.编写Dockerfile文件:
Dockerfile:
# 基于centos
FROM centos:7
MAINTAINER hcx<hcx@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 安装java8
RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
# 将宿主机的文件jdk-8u351-linux-x64.tar.gz 解压添加到容器的 /usr/local/java下
ADD jdk-8u351-linux-x64.tar.gz /usr/local/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 "install centos success--------------ok"
CMD /bin/bash
2.构建镜像 docker build -t 新镜像名字:TAG .
在宿主机上运行:
在docker文件夹下准备好Dockerfile和对应的资源:
hongcaixia@hongcaixiadeMacBook-Pro Documents % cd docker
hongcaixia@hongcaixiadeMacBook-Pro docker % ls
Dockerfile jdk-8u351-linux-x64.tar.gz
hongcaixia@hongcaixiadeMacBook-Pro docker % docker build -t centos-java8:1.0 .
[+] Building 15.6s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 745B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 15.4s
=> [1/5] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 190B 0.0s
=> CACHED [2/5] WORKDIR /usr/local 0.0s
=> CACHED [3/5] RUN yum -y install glibc.i686 0.0s
=> CACHED [4/5] RUN mkdir /usr/local/java 0.0s
=> CACHED [5/5] ADD jdk-8u351-linux-x64.tar.gz /usr/local/java 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:ed61e051f801980577988e66ac8c31b9a63844ad8f2b078aa9b36232699741b9 0.0s
=> => naming to docker.io/library/centos-java8:1.0 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
hongcaixia@hongcaixiadeMacBook-Pro docker %
hongcaixia@hongcaixiadeMacBook-Pro docker % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-java8 1.0 ed61e051f801 11 hours ago 830MB
bitnami/kafka latest e7e7893c6d36 4 months ago 552MB
zookeeper latest 248549379309 6 months ago 300MB
xuxueli/xxl-job-admin 2.3.1 252b168021da 12 months ago 237MB
nginx latest 605c77e624dd 17 months ago 141MB
redis latest 7614ae9453d1 18 months ago 113MB
apacherocketmq/rocketmq-dashboard latest eae6c5db5d11 19 months ago 738MB
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
centos latest 300e315adb2f 2 years ago 209MB
docker.elastic.co/kibana/kibana 7.1.0 714b175e84e8 4 years ago 745MB
docker.elastic.co/elasticsearch/elasticsearch 7.1.0 12ad640a1ec0 4 years ago 894MB
lmenezes/cerebro 0.8.3 3a2daf87f0c7 4 years ago 333MB
hongcaixia@hongcaixiadeMacBook-Pro docker %
3.运行镜像 docker run -it 新镜像名字:TAG
hongcaixia@hongcaixiadeMacBook-Pro docker % docker run -it centos-java8:1.0
[root@5e4d9b15417d local]# java -version
java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)
[root@5e4d9b15417d local]#
五、使用DockerFile构建微服务
1.代码
添加依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
MyController:
package com.hcx.dockerfiledemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Title: MyController.java
* @Package com.hcx.dockerfiledemo.controller
* @Description: (用一句话描述该文件做什么)
* @Author: hongcaixia
* @Date: 2023/6/15 21:35
* @Version V1.0
*/
@RestController
@RequestMapping(value = "/my")
public class MyController {
@GetMapping("/test")
public String test() {
return "hello,world";
}
}
2.编写Dockerfile
# 基于centos
FROM java:8
MAINTAINER hcx<hcx@qq.com>
# 指定临时文件目录为/tmp 在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器并更名为 hcx-docker.jar
ADD dockerfile-demo-0.0.1-SNAPSHOT.jar hcx-docker.jar
# 运行jar
RUN bash -c 'touch /hcx-docker.jar'
ENTROYPOINT ["java","-jar","/hcx-docker.jar"]
# 暴露80端口
EXPOSE 80
3.构建镜像 docker build -t 新镜像名字:TAG .
hongcaixia@hongcaixiadeMacBook-Pro docker % cd java
hongcaixia@hongcaixiadeMacBook-Pro java % ls
Dockerfile dockerfile-demo-0.0.1-SNAPSHOT.jar
hongcaixia@hongcaixiadeMacBook-Pro java % docker build -t hcx-docker:1.0 .
[+] Building 2.1s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 785B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/java:8 1.0s
=> [internal] load build context 0.4s
=> => transferring context: 17.70MB 0.4s
=> [1/3] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 0.3s
=> => resolve docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 0.0s
=> => sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 2.00kB / 2.00kB 0.0s
=> => sha256:d23bdf5b1b1b1afce5f1d0fd33e7ed8afbc084b594b9ccf742a5b27080d8a4a8 4.73kB / 4.73kB 0.0s
=> [2/3] ADD dockerfile-demo-0.0.1-SNAPSHOT.jar hcx-docker.jar 0.1s
=> [3/3] RUN bash -c 'touch /hcx-docker.jar' 0.4s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:2c8e8a4cedccca75b565b93fe411a40d1885ddd30b43beb57ecc9bba02b797f9 0.0s
=> => naming to docker.io/library/hcx-docker:1.0 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
hongcaixia@hongcaixiadeMacBook-Pro java % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hcx-docker 1.0 2c8e8a4cedcc 2 minutes ago 679MB
centosjava8 latest e90626d6775b 24 hours ago 830MB
centos-java8 1.0 ed61e051f801 24 hours ago 830MB
bitnami/kafka latest e7e7893c6d36 5 months ago 552MB
zookeeper latest 248549379309 6 months ago 300MB
xuxueli/xxl-job-admin 2.3.1 252b168021da 13 months ago 237MB
nginx latest 605c77e624dd 17 months ago 141MB
redis latest 7614ae9453d1 18 months ago 113MB
apacherocketmq/rocketmq-dashboard latest eae6c5db5d11 19 months ago 738MB
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
centos latest 300e315adb2f 2 years ago 209MB
docker.elastic.co/kibana/kibana 7.1.0 714b175e84e8 4 years ago 745MB
docker.elastic.co/elasticsearch/elasticsearch 7.1.0 12ad640a1ec0 4 years ago 894MB
lmenezes/cerebro 0.8.3 3a2daf87f0c7 4 years ago 333MB
hongcaixia@hongcaixiadeMacBook-Pro java %
4.运行镜像 docker run -it 新镜像名字:TAG
hongcaixia@hongcaixiadeMacBook-Pro java % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hcx-docker 1.0 2c8e8a4cedcc 2 minutes ago 679MB
centosjava8 latest e90626d6775b 24 hours ago 830MB
centos-java8 1.0 ed61e051f801 24 hours ago 830MB
bitnami/kafka latest e7e7893c6d36 5 months ago 552MB
zookeeper latest 248549379309 6 months ago 300MB
xuxueli/xxl-job-admin 2.3.1 252b168021da 13 months ago 237MB
nginx latest 605c77e624dd 17 months ago 141MB
redis latest 7614ae9453d1 18 months ago 113MB
apacherocketmq/rocketmq-dashboard latest eae6c5db5d11 19 months ago 738MB
hello-world latest feb5d9fea6a5 20 months ago 13.3kB
centos latest 300e315adb2f 2 years ago 209MB
docker.elastic.co/kibana/kibana 7.1.0 714b175e84e8 4 years ago 745MB
docker.elastic.co/elasticsearch/elasticsearch 7.1.0 12ad640a1ec0 4 years ago 894MB
lmenezes/cerebro 0.8.3 3a2daf87f0c7 4 years ago 333MB
hongcaixia@hongcaixiadeMacBook-Pro java % docker run -d -p 8080:8080 2c8e8a4cedcc
ef2afc6e7cb7658e1cb0df788d0c23b85e2fded3c0091288e624088526a6d31f
hongcaixia@hongcaixiadeMacBook-Pro java % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef2afc6e7cb7 2c8e8a4cedcc "java -jar /hcx-dock…" 41 seconds ago Up 41 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp stoic_khorana
hongcaixia@hongcaixiadeMacBook-Pro java %
5.访问测试
hongcaixia@hongcaixiadeMacBook-Pro java % curl 127.0.0.1:8080/my/test
hello,world% hongcaixia@hongcaixiadeMacBook-Pro java %
网友评论