Dockerfile
Docker镜像原理
Linux文件系统有bootfs和rootfs两部分组成
-
Docker镜像由特殊文件系统叠加
-
最底端bootfs,使用宿主机bootfs
-
第二次时rootfs,被称为基础镜像
-
向上可以叠加其他镜像文件
-
同一文件系统能将多层整合成一层,隐藏了多层存在
-
镜像可以放置另一个镜像上面,位于下面的镜像成为父镜像
-
当一个镜像启动容器时,Docker会在最顶层加载一个读写文件系统作为容器
镜像制作
-
容器转为镜像
镜像不能传输
镜像可以转为压缩文件,然后再去传输
查看容器
[root@server ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ef7a9d24cdd mysql:5.6 "docker-entrypoint.s…" 13 hours ago Exited (255) 3 hours ago 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp c_mysql
把c_mysql转成镜像
[root@server ~]# docker commit 8ef7a9d24cdd mysql_iso
sha256:ee9adf5b156851692b4cf0fe6c25265ce9dc89924b3fc064935c859f875d1894
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql_iso latest ee9adf5b1568 8 seconds ago 303MB
nginx latest 605c77e624dd 16 months ago 141MB
redis 5.0 c5da061a611a 16 months ago 110MB
mysql 5.6 dd3b2a5dcb48 16 months ago 303MB
centos 7 eeb6ee3f44bd 19 months ago 204MB
把镜像压缩 可以传输给别人
[root@server ~]# docker save -o mysql_iso.tar mysql_iso
[root@server ~]# ll
总用量 300448
-rw-------. 1 root root 1241 4月 24 13:04 anaconda-ks.cfg
drwxr-xr-x. 2 root root 50 5月 6 19:41 data
drwxr-xr-x. 5 root root 42 5月 7 00:50 mysql
-rw-------. 1 root root 307652096 5月 7 13:39 mysql_iso.tar
把mysql_iso删除,通过压缩文件还原成镜像
[root@server ~]# docker rmi mysql_iso
Untagged: mysql_iso:latest
Deleted: sha256:ee9adf5b156851692b4cf0fe6c25265ce9dc89924b3fc064935c859f875d1894
Deleted: sha256:bde6bf72f86d605ec4185f4e7b6686b27a4704219760871f1cdedeff2f41b607
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 16 months ago 141MB
redis 5.0 c5da061a611a 16 months ago 110MB
mysql 5.6 dd3b2a5dcb48 16 months ago 303MB
centos 7 eeb6ee3f44bd 19 months ago 204MB
还原镜像
[root@server ~]# docker load -i mysql_iso.tar
01bd5c6edefd: Loading layer 6.656kB/6.656kB
Loaded image: mysql_iso:latest
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql_iso latest ee9adf5b1568 10 minutes ago 303MB
nginx latest 605c77e624dd 16 months ago 141MB
redis 5.0 c5da061a611a 16 months ago 110MB
mysql 5.6 dd3b2a5dcb48 16 months ago 303MB
centos 7 eeb6ee3f44bd 19 months ago 204MB
通过这个镜像来运行一个新容器
[root@server ~]# docker run -id --name=c_mysql_n mysql_iso bash
[root@server ~]# docker exec -it c_mysql_n /bin/bash
root@97c993b5e148:/#
- dockerfile
概念
-
文本文件
-
包含了一条条指令
-
每一条指令构建一层,基于基础镜像,最终构建出新镜像
Docker自定义centos7镜像
要求
-
默认登录路径为/usr
-
可以使用vim
查看镜像
[root@server ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 16 months ago 141MB
tomcat latest fb5657adc892 16 months ago 680MB
redis 5.0 c5da061a611a 16 months ago 110MB
mysql 5.6 dd3b2a5dcb48 16 months ago 303MB
centos 7 eeb6ee3f44bd 19 months ago 204MB
[root@server ~]# docker run -id --name=my_c centos:7
8cace6a4806c37f2c14d4b2779ff863087e8525959fc85238b023786f799cc5f
[root@server ~]# docker exec -it my_c bash
[root@8cace6a4806c /]# ll
total 12
-rw-r--r--. 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx. 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 340 May 7 06:56 dev
drwxr-xr-x. 1 root root 66 May 7 06:56 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 home
lrwxrwxrwx. 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 media
drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x. 125 root root 0 May 7 06:56 proc
dr-xr-x---. 2 root root 114 Nov 13 2020 root
drwxr-xr-x. 11 root root 148 Nov 13 2020 run
lrwxrwxrwx. 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x. 13 root root 0 May 7 05:08 sys
drwxrwxrwt. 7 root root 132 Nov 13 2020 tmp
drwxr-xr-x. 13 root root 155 Nov 13 2020 usr
drwxr-xr-x. 18 root root 238 Nov 13 2020 var
[root@8cace6a4806c /]# cd ~
[root@8cace6a4806c ~]# vim tmp.txt
bash: vim: command not found
这里没有安装vim
根据需求编写Dockerfile
-
定义父镜像:FROM centos:7
-
定义作者信息:MAINAINER
-
执行安装vim命令:RUN yum install -y vim
-
定义默认工作路径:WORKDIR:/usr
-
定义容器启动后执行命令:CMD /bin/bash
清空所有容器
[root@server ~]# docker stop $(docker ps -aq)
8cace6a4806c
[root@server ~]# docker rm $(docker ps -aq)
8cace6a4806c
[root@server ~]# mkdir docker-files
[root@server ~]# cd docker-files/
[root@server docker-files]# ll
总用量 0
[root@server docker-files]# vim centos_dockerfile
[root@server docker-files]# cat centos_dockerfile
FROM centos:7
MAINTAINER rkun18
RUN yum install -y vim
WORKDIR /usr
CMD /bin/bash
[root@server docker-files]# docker build -f ./centos_dockerfile -t my_centos:1 .
[+] Building 497.0s (7/7) FINISHED
=> [internal] load build definition from centos_dockerfile 0.1s
=> => transferring dockerfile: 188B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [1/3] FROM docker.io/library/centos:7 0.0s
=> [2/3] RUN yum install -y vim 494.9s
=> [3/3] WORKDIR /usr 0.0s
=> exporting to image 1.9s
=> => exporting layers 1.9s
=> => writing image sha256:d110499cc5a06e4e74ea2b4c84b70ab745f7932ae6d1b54f4eb78f4 0.0s
=> => naming to docker.io/library/my_centos:1 0.0s
[root@server docker-files]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_centos 1 d110499cc5a0 About a minute ago 468MB
nginx latest 605c77e624dd 16 months ago 141MB
tomcat latest fb5657adc892 16 months ago 680MB
redis 5.0 c5da061a611a 16 months ago 110MB
mysql 5.6 dd3b2a5dcb48 16 months ago 303MB
centos 7 eeb6ee3f44bd 19 months ago 204MB
- 创建容器测试是否满足需求
[root@server ~]# docker run -id --name=c1 my_centos:1
821adf3c8a63d30daad86b759f2a7f5a021c0bb461700974e47ff43c1871fd5c
[root@server ~]# docker exec -it c1 /bin/bash
[root@821adf3c8a63 usr]# vim test.txt 直接进入usr目录 并且可以使用vim
[root@821adf3c8a63 usr]# cat test.txt
hello docker
[root@821adf3c8a63 usr]# exit
exit