一般流程:
- 编写docker文件
- docker build构建成为一个镜像
- docker run 运行镜像
- docker push 发布镜像
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
Dockerfile文件格式如下:
# Comment
INSTRUCTION arguments
1、Dockerfile的指令
指令 | 说明 |
---|---|
FROM | 基础镜像, Dockerfile文件从这里开始 |
MAINTAINER | 镜像是谁写的(维护者信息) |
RUN | 镜像构建的时候需要运行的命令 |
ADD | The ADD instruction copies new files, directories or remote file URLs from and adds them to the filesystem of the image at the path . ,复制文件自动解压 |
WORKDIR | 镜像的工作目录 |
VOLUME | 挂载的目录 |
EXPOSE | 保留端口的配置 |
CMD | 指定这个容器启动时要运行的命令,只有最后一个才会生效,可被替代 |
ENTRYPOINT | 指定这个容器启动时候要运行的命令,可以追加命令 |
ONBUILD | 当构建一个被继承Dockerfile这个时候就会运行该指令。 |
COPY | 复制文件 |
ENV | 构建时候设置环境变量 |
2 案例:
# centos的官方镜像没有 tree ifconfig命令
[root@localhost testdocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 2 months ago 209MB
[root@localhost testdocker]# docker run -it --name centos01 centos:latest /bin/bash
[root@58c794c4eca7 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@58c794c4eca7 /]# cd /home/
[root@58c794c4eca7 home]# tree
bash: tree: command not found
[root@58c794c4eca7 home]# ifconfig
bash: clear: ifconfig not found
[root@58c794c4eca7 home]# [root@localhost testdocker]#
[root@localhost testdocker]# ls
mydfcentos
# 1 根据需求,写dockerfile
[root@localhost testdocker]# vi mydfcentos
[root@localhost testdocker]# cat mydfcentos
FROM centos
MAINTAINER fancy<23XXXXXX52@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install tree
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "-----end-------"
CMD /bin/bash
[root@localhost testdocker]#
# 2 通过这个文件构建镜像
[root@localhost testdocker]# docker build -f mydfcentos -t mycentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 300e315adb2f
Step 2/10 : MAINTAINER fancy<2310403052@qq.com>
---> Running in 94af9ad614a2
Removing intermediate container 94af9ad614a2
---> 16134c96d09c
Step 3/10 : ENV MYPATH /usr/local
---> Running in 05f220b1505b
Removing intermediate container 05f220b1505b
---> e04508df11a9
Step 4/10 : WORKDIR $MYPATH
---> Running in a815783377fd
Removing intermediate container a815783377fd
---> 8361aa5d51d8
Step 5/10 : RUN yum -y install tree
。。。
。。。
Step 10/10 : CMD /bin/bash
---> Running in eaf547e75afd
Removing intermediate container eaf547e75afd
---> c2662a2a961d
Successfully built c2662a2a961d
Successfully tagged mycentos:0.1
[root@localhost testdocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 c2662a2a961d 12 minutes ago 264MB
centos latest 300e315adb2f 2 months ago 209MB
# 3 运行刚才制作的镜像
[root@localhost testdocker]# docker run -it --name mycentos01 mycentos:0.1
# 此时进入了容器内部,尝试输入命令ifconfig
[root@04bbe3830a7e local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 36 bytes 6287 (6.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 默认工作路径
# ENV MYPATH /usr/local
# WORKDIR $MYPATH
[root@04bbe3830a7e local]# pwd
/usr/local
[root@04bbe3830a7e ~]# pwd
/root
[root@04bbe3830a7e ~]# ls
anaconda-ks.cfg anaconda-post.log original-ks.cfg
[root@04bbe3830a7e ~]# tree
.
|-- anaconda-ks.cfg
|-- anaconda-post.log
`-- original-ks.cfg
0 directories, 3 files
补充:查看镜像的制作(推测)
[root@localhost testdocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 c2662a2a961d About an hour ago 264MB
centos latest 300e315adb2f 2 months ago 209MB
[root@localhost testdocker]# docker history c2662a2a961d
IMAGE CREATED CREATED BY SIZE COMMENT
c2662a2a961d About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
264365743e1e About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
07129637df28 About an hour ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
4f745e67d3eb About an hour ago /bin/sh -c #(nop) EXPOSE 80 0B
599676abbae9 About an hour ago /bin/sh -c yum -y install net-tools 22.7MB
70918d97218d About an hour ago /bin/sh -c yum -y install tree 31.9MB
8361aa5d51d8 About an hour ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
e04508df11a9 About an hour ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
16134c96d09c About an hour ago /bin/sh -c #(nop) MAINTAINER fancy<23104030… 0B
300e315adb2f 2 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 2 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
如何发布自己的镜像?
# 1、 注册https://hub.docker.com/ 账号
# 2、 登陆服务器
[root@localhost testdocker]# docker login -u fXXXXX
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@localhost testdocker]#
[root@localhost testdocker]# docker tag c2662a2a961d fXXXXX/mycentos:1.0
[root@localhost testdocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fXXXXX/mycentos 1.0 c2662a2a961d 21 hours ago 264MB
mycentos 0.1 c2662a2a961d 21 hours ago 264MB
centos latest 300e315adb2f 2 months ago 209MB
# 3 提交
[root@localhost testdocker]# docker push fXXXXX/mycentos:1.0
The push refers to repository [docker.io/fXXXXX/mycentos]
6a1c957064f0: Pushed
389a478c3967: Pushed
2653d992f4ef: Pushed
1.0: digest: sha256:5b0ce7a33fe586653b131b7dace000f565f53063d5823a5a3560dc597cd3bea3 size: 953