a创建镜像,创建自定义的镜像
包括配置文件,挂载点,对外暴露的端口,设置环境变量
docker的创建镜像的方式
1、基于已有镜像进行创建
根据官方提供的镜像源,创建镜像,然后拉起容器,是一个白板。只能提供基础的功能,扩展性的功能还是需要自定义(进入容器操作)
2、基于模板进行创建
[root@test2 opt]# docker import ubuntu-14.04-x86-minimal.tar.gz -- ubuntu:14
sha256:f53e7b676c40c7f9f98c51c0a179e53b1c9e9893a79be3491d6f17d2e8b89c1e
[root@test2 opt]# docker run -itd --name test1 ubuntu:14 /bin/bash
a92b9ce3913c8460abe2819371dbcd094600f0e285bef8b72af6a249c20af1e0
[root@test2 opt]# docker exec -it test1 bash
root@a92b9ce3913c:/# ls
bin dev fastboot lib media opt root sbin sys usr
boot etc home lost+found mnt proc run srv tmp var
3、dockerfile
联合文件系统(UnionFS),docker 镜像的基础
镜像通过分层来进行集成,特性:一次同时加载多个文件系统,但是从外面来看,就是一个文件系统
[root@test2 opt]# docker pull nginx:1.22.0
1.22.0: Pulling from library/nginx
bd159e379b3b: Pull complete 分层
265da2307f4a: Pull complete
9f5a323076dc: Pull complete
1cb127bd9321: Pull complete
20d83d630f2b: Pull complete
e0c68760750a: Pull complete
Digest: sha256:f0d28f2047853cbc10732d6eaa1b57f1f4db9b017679b9fd7966b6a2f9ccc2d1
Status: Downloaded newer image for nginx:1.22.0
docker.io/library/nginx:1.22.0、
[root@test2 opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14 f53e7b676c40 14 minutes ago 205MB
只显示一个
docker镜像实际上就是由一层一层的文件系统组成 ,这种层级的文件系统就是UnionFS
每一层都是layers,每一层都包含文件系统的一部分,这些层次叠加在一起,最终形成的就是rootfs
bootfs:宿主机提供的内核和引导程序
rootfs:就是容器的操作系统,在dockerfile中,我们可以自己指定
rootfs是多个基础镜像和应用镜像结合起来的只读层 。镜像实际上就是一个只读文件。
容器基于镜像实例,运行起来之后,容器就变成一个可读可写层
在dockerfile当中每创建一个指令都是一个镜像层
镜像层会被缓存和复用。
如果某一层失败,镜像将不会创建成功,所有的镜像层都会失败
镜像层是不可变的,在某一次当中添加一个新的命令,但是在下一层删除了指定,镜像中基于这个命令创建的文件还会存在,但是在容器中不可见
1 touch 123.txt
2 rm -rf 123.txt
镜像中会依然保留,但是容器里面是看不到了
dockerfile的核心:用户的个性化定制docker的镜像。
dockerfile的结构:
官方下载的镜像:nginx:已经定义好了容器执行的命令 在创建时加上了 /bin/bash ,覆盖了容器内的标准输出,所以在宿主机内无法通过logs 命令来查看容器内的日志
dockerfile的语法:
核心:FROM RUN ENTRYPOINT CMD 其他的按需使用
CMD和ENTRYPOINT的区别:
都是可以作为容器启动时的默认命令 这是他们的共同点
CMD可以把参数传给ENTRYPOINT
[root@test2 opt]# cd test1
[root@test2 test1]# ls
[root@test2 test1]# vim Dockerfile
FROM centos:7
MAINTAINER "this is my docke "
ENTRYPOINT ["echo","hello"]
CMD ["world"]
[root@test2 test1]# docker build -t centos7:test .
[+] Building 15.0s (5/5) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 187B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 5.9s
=> [1/1] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a 9.1s
=> => resolve docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a 0.0s
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b5 529B / 529B 0.0s
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a1 2.75kB / 2.75kB 0.0s
=> => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f255373 76.10MB / 76.10MB 4.7s
=> => sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d648 1.20kB / 1.20kB 0.0s
=> => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3 4.3s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:bcc86d4f6d5045cd502dc7fdaed1bd734143930afa035367cf631f94125 0.0s
=> => naming to docker.io/library/centos7:test 0.0s
[root@test2 test1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14 f53e7b676c40 About an hour ago 205MB
nginx 1.22.0 08a1cbf9c69e 14 months ago 142MB
centos7 test bcc86d4f6d50 2 years ago 204MB
[root@test2 test1]# docker run --name test3 centos7:test
hello world
区别:
[root@test2 test1]# docker run -it centos7:test ls /opt
hello ls /opt
RUN命令的优化
主要是减少镜像的层数,把多个RUN命令写在一块。
RUN yum -y install nginx && make -j 4 && make install
&符有一个特点,第一个命令执行成功后才会执行下一个命令
RUN yum -y install nginx ; make -j 4 ; make install
;符的特点 不管前一个命令是否成功,后面的命令都会执行
RUN yum -y install nginx || make -j 4
如果前面的命令执行失败,才会执行后面的操作
RUN yum -y install nginx \
&& make -j 4 \
&& make install
可读性更高
COPY和ADD的区别
相同点:都可以把本地文件复制到镜像中,但是官方推荐如果是复制,使用COPY
区别:
ADD可以解压 ,如果是一个解压文件,ADD在复制之后会自动解压 (tar.gz和tar.bz2)可以支持URL路径下载一个源文件,只能支持下载,但是不能解压 通过URL拷贝的文件无法自动解压
COPY只能复制不能解压,而且只能是本地文件,不支持URL路径。
centos7 构建一个apache的dockerfile(编译安装):
创建基础镜像
[root@test2 httpd]# ls
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz Dockerfile httpd-2.4.29.tar.bz2
[root@test2 httpd]# vim Dockerfile
FROM centos:7
MAINTAINER "this is my apache "
RUN yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl
ADD apr-1.6.2.tar.gz /opt
ADD apr-util-1.6.0.tar.gz /opt
ADD httpd-2.4.29.tar.bz2 /opt
RUN mv /opt/apr-1.6.2 /opt/httpd-2.4.29/srclib/apr && mv /opt/apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util &&\
cd /opt/httpd-2.4.29 &&\
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &&\
make -j 4 && make install
EXPOSE 80
CMD ["/usr/local/httpd/bin/apachectl","-D","FOREGROUND"]
[root@test2 httpd]# docker build -t apache:centos .
[+] Building 86.5s (11/11) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 644B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 3.0s
=> CACHED [1/6] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 282B 0.0s
=> [2/6] RUN yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl 23.8s
=> [3/6] ADD apr-1.6.2.tar.gz /opt 0.1s
=> [4/6] ADD apr-util-1.6.0.tar.gz /opt 0.1s
=> [5/6] ADD httpd-2.4.29.tar.bz2 /opt 1.7s
=> [6/6] RUN mv /opt/apr-1.6.2 /opt/httpd-2.4.29/srclib/apr && mv /opt/apr-util-1.6.0 56.5s
=> exporting to image 1.4s
=> => exporting layers 1.4s
=> => writing image sha256:87c4f4358dee8c2f9b492141dd2d4535ef7954f19c5f71228955cb65de3 0.0s
=> => naming to docker.io/library/apache:centos
[root@test2 httpd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache centos 87c4f4358dee 11 minutes ago 721MB
[root@test2 httpd]# docker run -itd --name httpd1 -p 1314:80 apache:centos
b57b9c97b2e8f4929ba092c89a31816bfbfa20b7fc1b88722e11fbf7dd107862
[root@test2 httpd]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b57b9c97b2e8 apache:centos "/usr/local/httpd/bi…" 5 seconds ago Up 4 seconds 0.0.0.0:1314->80/tcp, :::1314->80/tcp httpd1
基于镜像的二次构建
[root@test2 httpd]# vim Dockerfile
FROM centos:7 AS first
MAINTAINER "this is my apache "
RUN yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl
ADD apr-1.6.2.tar.gz /opt
ADD apr-util-1.6.0.tar.gz /opt
ADD httpd-2.4.29.tar.bz2 /opt
RUN mv /opt/apr-1.6.2 /opt/httpd-2.4.29/srclib/apr && mv /opt/apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util &&\
cd /opt/httpd-2.4.29 &&\
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi &&\
make -j 4 && make install
#二层构建:
FROM centos:7
COPY --from=first /usr/local/httpd /usr/local/httpd
RUN yum -y install pcre pcre-devel expat-devel perl
EXPOSE 80
CMD ["/usr/local/httpd/bin/apachectl","-D","FOREGROUND"]
[root@test2 httpd]# docker build -t apache1:centos .
[+] Building 27.3s (13/13) FINISHED docker:default
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 789B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 3.0s
=> CACHED [first 1/6] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 282B 0.0s
=> CACHED [first 2/6] RUN yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel 0.0s
=> CACHED [first 3/6] ADD apr-1.6.2.tar.gz /opt 0.0s
=> CACHED [first 4/6] ADD apr-util-1.6.0.tar.gz /opt 0.0s
=> CACHED [first 5/6] ADD httpd-2.4.29.tar.bz2 /opt 0.0s
=> CACHED [first 6/6] RUN mv /opt/apr-1.6.2 /opt/httpd-2.4.29/srclib/apr && mv /opt/ap 0.0s
=> [stage-1 2/3] COPY --from=first /usr/local/httpd /usr/local/httpd 0.3s
=> [stage-1 3/3] RUN yum -y install pcre pcre-devel expat-devel perl 22.9s
=> exporting to image 0.9s
=> => exporting layers 0.9s
=> => writing image sha256:1df49fafc0b6b26cc5526617a15c624669407975a5b26edfeee650553dd 0.0s
=> => naming to docker.io/library/apache1:centos
[root@test2 httpd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache1 centos 1df49fafc0b6 8 seconds ago 508MB
apache centos 87c4f4358dee 22 minutes ago 721MB