示例1:
step(1)创建容器并编写内容
[root@docker-learn ~]# docker run -it --rm busybox:latest sh
/ # echo "This is a test page" > /data/html/index.html
sh: can't create /data/html/index.html: nonexistent directory
/ # mkdir -p /data/html
/ # echo "This is a test page" > /data/html/index.html
/ # cat /data/html/index.html
step(2)镜像(此时不要退出不然busybox会被删除掉)
#格式:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
[root@docker-learn ~]# docker commit 57678e340bb9 busybox:v1
sha256:92baf1e11828d79b8704ba2b16d42d28278de9b878ea9939b3a7d11b4663bd2e
使用docker images查看可以看到已经有busybox:v1镜像
step(3)容器运行镜像
[root@docker-learn ~]# docker run -it busybox:v1 sh
/ # cat /data/html/index.html
This is a test page
docker build 格式:
docker build [OPTIONS] PATH | URL | -
常用选项:
-t, --tag stringArray 名称和标签(格式:" Name:tag")
--build-arg variable name and value of a buildarg.
-m, --memory MEMORY Memory limit
--cpu-shares 0 CPU shares (relative weight).
FROM指令必须是dockerfile中非注释行的第一个指令。
提供维护者详细信息
从上下文目录中复制 文件/目录 到容器里指定的路径
示例:
[root@docker-learn busybox_test]# echo 'Hello world!' > text
[root@docker-learn busybox_test]# mkdir -p ./date/01/01
[root@docker-learn busybox_test]# cat dockerfile
# Description: test image
FROM busybox:latest
MAINTAINER "rose "
COPY text /home/
COPY date /home/
[root@docker-learn busybox_test]# docker build . -t busybox_test:v1
[root@docker-learn busybox_test]# docker run -it --rm busybox_test:v1 sh
/ # cd /home
/home # ls
01 text
/home # cat text
Hello world!
当在dockerfile中add *.tar.gz包时存入镜像会自动解压
示例:
[root@docker-learn docker_build]# mkdir busybox_test2
[root@docker-learn docker_build]# cd busybox_test2
[root@docker-learn busybox_test2]# echo 'Hello world' > text
[root@docker-learn busybox_test2]# wget -c https://nginx.org/download/nginx-1.15.7.tar.gz
[root@docker-learn busybox_test2]# cat dockerfile
[root@docker-learn busybox_test2]# cat dockerfile
# description: test file
FROM busybox:latest
MAINTAINER "rose"
COPY text /home/
ADD https://nginx.org/download/nginx-1.15.8.tar.gz /usr/local/src/
ADD nginx-1.15.7.tar.gz /usr/local/src/
[root@docker-learn busybox_test2]# docker build . -t busybox_test2:v1
[root@docker-learn busybox_test2]# docker run -it --rm busybox_test2:v1 sh
/ # cat /home/text
Hello world
/ # ls /usr/local/src/
nginx-1.15.7 nginx-1.15.8.tar.gz
workdir和run对比示例:
WORKDIR /usr/local/
#上面这一步改变了工作路径,所以下一步内容在新的工作路径下进行
ADD nginx-1.15.7.tar.gz ./src/
RUN cd /home
#上面这一步只改变了运行上面这一步的路径,下一步不发生改变
ADD nginx-1.15.7.tar.gz ./
示例:
[root@docker-learn busybox_test3]# ls
dockerfile nginx-1.15.7.tar.gz text
[root@docker-learn busybox_test3]# cat text
this is a file
[root@docker-learn busybox_test3]# cat dockerfile
# Discription: test image
FROM busybox:latest
MAINTAINER "rose "
COPY text /homt/
WORKDIR /usr/local/
ADD nginx-1.15.7.tar.gz ./src/
VOLUME /test/file
[root@docker-learn busybox_test3]# docker build . -t busybox_test3:v1
[root@docker-learn busybox_test3]# docker run --rm -it busybox_test3:v1 sh
/usr/local # ls
src
/usr/local # ls src/
nginx-1.15.7
/usr/local # cat /homt/text
this is a file
[root@docker-learn busybox_test3]# docker inspect ae8
#可以查看到挂载信息
示例:
[root@docker-learn busybox_test3]# cat dockerfile
# Discription: test image
FROM busybox:latest
MAINTAINER "rose "
COPY index.html /data/web/html/
WORKDIR /usr/local/
ADD nginx-1.15.7.tar.gz ./src/
VOLUME /data/test_volume
EXPOSE 80/tcp
[root@docker-learn busybox_test3]# cat index.html
This is test page
[root@docker-learn busybox_test3]# docker build -t busybox_test3:v2 .
#运行容器后会一直卡在一个界面,因此需要换一个终端来进行ip获取
[root@docker-learn busybox_test3]# docker run -it busybox_test3:v2 httpd -f -h /data/web/html/
#获取ip
[root@docker-learn busybox_test3]# docker inspect -f "{{.NetworkSettings.IPAddress}}" 1bf
172.17.0.3
[root@docker-learn busybox_test3]# curl 172.17.0.3:80
This is test page
示例:
###设置全局变量DOC_ROOT和WEB_SERVER_PACKAGE('\'只用来换行)
ENV DOC_ROOT=/data/web/html/ \
WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz"
###调用变量
COPY index.html ${DOC_ROOT}
WORKDIR /usr/local/
ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} ./src/
在启动容器时,可以使用docker run -e进行修改变量
docker run -e WEB_SERVER_PACKAGE=nginx-1.15.7 --rm -it image_name:tag printenv
# Discription: test image
FROM busybox:latest
MAINTAINER "rose "
###设置全局变量DOC_ROOT和WEB_SERVER_PACKAGE('\'只用来换行)
ENV DOC_ROOT=/data/web/html/ \
WEB_SERVER_PACKAGE="nginx-1.15.8.tar.gz"
###调用变量
COPY index.html ${DOC_ROOT}
WORKDIR /usr/local/
ADD http://nginx.org/download/${WEB_SERVER_PACKAGE} ./src/
#ADD ${WEB_SERVER_PACKAGE} ./src/
EXPOSE 8080:80/tcp
###下面run的仅是run自己运行改变的部分('\'只用来换行),其他指令运行的路径仍是WORKDIR
RUN cd ./src && \
tar -xf ${WEB_SERVER_PACKAGE}
json数组中要使用双引号,单引号会出错
CMD指定的命令其可以被docker run的命令行选项所覆盖;在Dockerfile中可以存在多个CMD指令,但仅最后一个会生效。
示例:
[root@docker-learn busybox_test4]# cat dockerfile
# Discription: test image
FROM busybox:latest
LABEL MAINTAINER "rose " app="httpd"
ENV WEB_DOC_ROOT="/data/web/html"
RUN mkdir -p $WEB_DOC_ROOT
COPY index.html $WEB_DOC_ROOT
WORKDIR /usr/local/
ADD nginx-1.15.7.tar.gz ./src/
###-f 可以让容器一直run不停止
CMD /bin/httpd -f -h ${WEB_DOC_ROOT}
如下有sh和没有会影响容器运行方式
类似CMD指令的功能,用于为容器指定默认运行程序,从而使得容器像是一个单独的可执行程序 ,与CMD不同的是,由ENTRYPOINT启动的程序不会被docker run命令行指定的参数所覆盖。
docker run命令中 --entrypoint 选项的参数,可覆盖ENTRYPOINT指令指定的程序
示例:
# Discription: test image
FROM busybox:latest
LABEL MAINTAINER "rose " app="httpd"
ENV WEB_DOC_ROOT="/data/web/html"
RUN mkdir -p ${WEB_DOC_ROOT}
ENTRYPOINT /bin/httpd -f -h ${WEB_DOC_ROOT}
如下两种运行都是一样会卡着占用终端,可以检验得到ENTRYPOINT在dockerfile中设定之后,docker run启动时设置COMMAND并不会改变容器的运行方式
[root@docker-learn busybox_test5]# docker run -it b5:v1
[root@docker-learn busybox_test5]# docker run -it b5:v1 sh
只出现一次,如果写了多个默认最后一个有效
格式:
HEALTHCHECK [OPTIONS] CMD command
定义触发器
格式:
ONBUILD < Instruction>
触发情况:第一个dockerfile中写了onbuild之后生成镜像,第二个dockerfile中from第一个镜像之后,此时在第二个镜像所生成的容器中触发。
USER
实践中其必须为/etc/ passwd中某用户的有效UID,否则, docker run命令将运行失败
ARG指令类似ENV,设定一些在镜像中不会存在的变量,只在dockerfile文件中生效,可以在构建时docker build --build-arg = 进行对变量的修改;ENV不可以。
语法 SHELL指令可以多次出现。
SHELL ["executable", "parameters"]