Docker学习的第五篇,主要写Dockerfile,以及我们使用Dockerfile可以去构建我们自己的镜像!
一些命令更详细的介绍可以看菜鸟教程上面写的:https://www.runoob.com/docker/docker-dockerfile.html
Dockerfile介绍 dockerfile 是用来构建docker镜像的文件!命令参数脚本!
构建步骤:
1、编写一个 dockerfile 文件
2、docker build 构建成一个镜像
3、docker run 运行镜像
4、docker push 发布镜像(DockerHub、阿里云镜像仓库)
Dockerfile构建过程 基础知识:
1、每个保留关键字(指令)都必须是大写字母
2、执行从上到下顺序执行
3、# 表示注释
4、每一行命令都会提交创建一个新的镜像层,并提交!
Dockerfile:构建文件,定义了一切步骤,源代码!
Dockerfile的指令 1 2 3 4 5 6 7 8 9 10 11 12 FROM MAINTAINER RUN ADD WORKDIR VOLUME EXPOSE CMD ENTRYPOIINT ONBUILD COPY ENV
实战:构建我们自己的centos 我们先看看官方的centos是怎么构建的:
我们发现我们用官方的centos,没有 vim
以及 ifconfig
命令,接下来我们自己构建一个centos镜像,让它带上这两个服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [root@localhost mycentos] FROM centos MAINTAINER gs<917139754@qq.com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE 80 CMD echo $MYPATH CMD echo "---end---" CMD /bin/bash [root@localhost mycentos] Sending build context to Docker daemon 2.048kB Step 1/10 : FROM centos ---> 300e315adb2f Step 2/10 : MAINTAINER gs<917139754@qq.com> ---> Running in e818528ec067 Removing intermediate container e818528ec067 ---> 1f6c2637ae05 Step 3/10 : ENV MYPATH /usr/local ---> Running in cdaa3a87d1c2 Removing intermediate container cdaa3a87d1c2 ---> 32aa92aae2bc Step 4/10 : WORKDIR $MYPATH ---> Running in ecd170ac426a Removing intermediate container ecd170ac426a ---> 0accc388d2f0 Step 5/10 : RUN yum -y install vim .......
构建成功后查看我们的docker 镜像:
下面我们运行一下看看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@localhost mycentos] [root@bc9e731b5fa8 local ] /usr/local [root@bc9e731b5fa8 local ] eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet) RX packets 7 bytes 586 (586.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 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 [root@bc9e731b5fa8 local ] [root@bc9e731b5fa8 local ]
可以看到已经按照我们的要求运行了!
我们可以列出本地变更的历史:docker history xxx
还可以看到这些每一层的详细信息!
CMD 和 ENTRYPOINT 的区别
测试CMD:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@localhost mycentos] FROM centos CMD ["ls" ,"-a" ] [root@localhost mycentos] . .. .dockerenv bin dev etc home xxx
可以看到我们的 ls -a
命令已经运行成功了!
但是如果我们想在后面再添加一个 -l
的参数,会发现会报错!
1 2 3 4 [root@localhost mycentos] docker: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec : "-l" : executable file not found in $PATH : unknown.
如果想要达到 ls-la
的效果,得按下面这样做:
1 2 3 4 5 6 7 8 9 10 11 12 [root@localhost mycentos] total 0 drwxr-xr-x. 1 root root 6 Jan 16 07:57 . drwxr-xr-x. 1 root root 6 Jan 16 07:57 .. -rwxr-xr-x. 1 root root 0 Jan 16 07:57 .dockerenv lrwxrwxrwx. 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x. 5 root root 340 Jan 16 07:57 dev drwxr-xr-x. 1 root root 66 Jan 16 07:57 etc drwxr-xr-x. 2 root root 6 Nov 3 15:22 home lrwxrwxrwx. 1 root root 7 Nov 3 15:22 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64 drwx------. 2 root root 6 Dec 4 17:37 lost+found
测试ENTRYPOINT:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@localhost mycentos] FROM centos ENTRYPOINT ["ls" ,"-a" ] [root@localhost mycentos] . .. .dockerenv bin dev etc xxx
可以看到我们的 ls -a
命令已经运行成功了!
但是如果我们想在后面再添加一个 -l
的参数,发现不会报错!
1 2 3 4 5 6 7 8 9 [root@localhost mycentos] total 0 drwxr-xr-x. 1 root root 6 Jan 16 08:02 . drwxr-xr-x. 1 root root 6 Jan 16 08:02 .. -rwxr-xr-x. 1 root root 0 Jan 16 08:02 .dockerenv lrwxrwxrwx. 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x. 5 root root 340 Jan 16 08:02 dev drwxr-xr-x. 1 root root 66 Jan 16 08:02 etc drwxr-xr-x. 2 root root 6 Nov 3 15:22 home
发布自己的镜像
发布到DockerHub
1、现在官网 https://hub.docker.com/ 上注册自己的账号
2、在我们的docker终端登录
1 2 3 4 5 6 7 8 9 10 11 12 [root@localhost mycentos] Usage: docker login [OPTIONS] [SERVER] Log in to a Docker registry. If no server is specified, the default is defined by the daemon. Options: -p, --password string Password --password-stdin Take the password from stdin -u, --username string Username [root@localhost mycentos]
3、push镜像
1 docker push 账号名/镜像名字:tag
发布到阿里云镜像上
1、登录阿里云,找到容器镜像服务:https://cr.console.aliyun.com/cn-hangzhou/instances/repositories
2、创建命名空间
3、创建容器镜像
我将详细信息先截取一张图:
接下来按照上面提示进行操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@localhost mycentos] 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/ Login Succeeded [root@localhost mycentos] [root@localhost mycentos] The push refers to repository [registry.cn-hangzhou.aliyuncs.com/gs_docker_study/docker_study_centos] e98e02e8342b: Pushing [=============> ] 6.253MB/23.34MB b76d811f9326: Pushing [==============> ] 16.76MB/58.06MB 2653d992f4ef: Layer already exists
然后在阿里云这边就可以看到上传的镜像了:
如果想要拉取镜像,还可以按照阿里云这上面的提示进行操作即可!