dockerfile 简介
Dockerfile 是⼀个⽤来构建镜像的⽂本⽂件,⽂本内容包含了⼀条条构建镜像所需的指令和 说明,每条指令构建⼀层,最终构建出⼀个新的镜像。
docker镜像的本质是⼀个分层的⽂件系统
centos的iso镜像⽂件是包含bootfs和rootfs及⼤量软件包,⽽docker的centos镜像复 ⽤了操作系统的bootfs,也没有⼤量软件包,只有rootfs和其他必要镜像层
由于docker镜像是分层的,例如:tomcat、mysql镜像本身课程也就⼏⼗MB,但是它 们需要依赖基础镜像及其他镜像层,所以对外暴露的tomcat、MySQL⼤⼩会很⼤。
环境
Redhat 9.2
安装docker docker 应用部署-CSDN博客
步骤
创建dockerfile目录 创建dockerfile 文件
[root@admin ~]# mkdir /dockerfile
[root@admin ~]# cd /dockerfile/
[root@admin dockerfile]# vim centos_dockerfile
[root@admin dockerfile]# cat centos_dockerfile
from centos:7
maintainer my
run yum -y install vim wget && wget -O /etc/yum.repos.d/Centos-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && wget -O /etc/yum.repos.d/epel.repo \
http://mirrors.aliyun.com/repo/epel-7.repo
workdir /root
cmd /bin/bash
运行dockerfile
格式:docker build -f dockerfile⽂件 -t 镜像名称:镜像标签 .
[root@admin dockerfile]# docker build -f /dockerfile/centos_dockerfile -t centos7:v1 .
[+] Building 207.4s (7/7) FINISHED docker:default
=> [internal] load build definition from centos_dockerfile 0.0s
=> => transferring dockerfile: 365B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 1.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 94.9s
=> => resolve docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 0.0s
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B 0.0s
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB 0.0s
=> => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB 90.8s
=> => sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987 1.20kB / 1.20kB 0.0s
=> => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 3.8s
=> [2/3] RUN yum -y install vim wget && wget -O /etc/yum.repos.d/Centos-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && wget -O 110.6s
=> [3/3] WORKDIR /root 0.1s
=> exporting to image 0.8s
=> => exporting layers 0.7s
=> => writing image sha256:f34ade5782a08614c2b86e56b2c67e2d766065d54299390ecaa94efe491b5910 0.0s
=> => naming to docker.io/library/centos7:v1 0.0s
[root@admin dockerfile]#
注:最后的 . 代表本次执⾏的上下⽂路径。上下⽂路径,是指 docker 在构建镜像,有时候想要使⽤到本机的⽂件(⽐如复制),docker build 命令得知这个路径后,会将路径下的 所有内容打包。
解析:由于 docker 的运⾏模式是 C/S。我们本机是 C,docker 引擎是 S。实际的构建过程 是在 docker 引擎下完成的,所以这个时候⽆法⽤到我们本机的⽂件。这就需要把我们本机 的指定⽬录下的⽂件⼀起打包提供给 docker 引擎使⽤。 如果未说明最后⼀个参数,那么默认上下⽂路径就是 Dockerfile 所在的位置。
注意:上下⽂路径下不要放⽆⽤的⽂件,因为会⼀起打包发送给 docker 引擎,如果⽂件过 多会造成过程缓慢。
查看构建的镜像
[root@admin dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 v1 f34ade5782a0 About a minute ago 505MB
[root@admin dockerfile]#
基于镜像创建一个容器验证是否可行
[root@admin dockerfile]# docker run -it --name centos7 centos7:v1 /bin/bash
[root@5fa0bb694e6e ~]#
[root@5fa0bb694e6e ~]#
[root@5fa0bb694e6e ~]#
[root@5fa0bb694e6e ~]# pwd
/root
[root@5fa0bb694e6e ~]# yum repolist //列出仓库
Loaded plugins: fastestmirror, ovl
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
- base: mirrors.ustc.edu.cn
- extras: mirrors.ustc.edu.cn
- updates: mirrors.ustc.edu.cn
epel | 4.7 kB 00:00:00
(1/3): epel/x86_64/group_gz | 100 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:02
(3/3): epel/x86_64/primary_db | 7.0 MB 00:00:09
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10072
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13798
extras/7/x86_64 CentOS-7 - Extras 526
updates/7/x86_64 CentOS-7 - Updates 5802
repolist: 30198
[root@5fa0bb694e6e ~]# vim test //验证vim工具是否安装
[root@5fa0bb694e6e ~]# cat test
sdaj
[root@5fa0bb694e6e ~]#
通过Dockerfile构建php连接mysql扩展模块的镜像
下载php镜像⽂件,版本为7.2版本
[root@admin dockerfile]# docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
6ec7b7d162b2: Pull complete
db606474d60c: Pull complete
afb30f0cd8e0: Pull complete
3bb2e8051594: Pull complete
4d71313b39b0: Pull complete
381de550657f: Pull complete
e671c4250cc8: Pull complete
111da53eb201: Pull complete
e12697892372: Pull complete
0540b5ed3310: Pull complete
0461cc2270a7: Pull complete
Digest: sha256:9c84ae47fddb97b94d1d2e289635b7306142a5336bc4ece0a393458c5e0d2cef
Status: Downloaded newer image for php:7.2-fpm
docker.io/library/php:7.2-fpm
[root@admin dockerfile]#
创建容器查看模块
[root@admin dockerfile]# docker run -it --name php php:7.2-fpm /bin/bash
root@e1243835e67c:/var/www/html# php -m | grep mysqld
root@e1243835e67c:/var/www/html# php -m | grep mysql
mysqlnd
root@e1243835e67c:/var/www/html#
通过dockerfile文件构建MySQL模块
[root@admin dockerfile]# ls
centos_dockerfile
[root@admin dockerfile]# vim mysql_dockerfile
[root@admin dockerfile]# cat mysql_dockerfile
from php:7.2-fpm
run docker-php-ext-install pdo_mysql mysqli
[root@admin dockerfile]# docker build -f ./mysql_dockerfile -t php7.2-mysql5.7:v1 .
[+] Building 15.3s (6/6) FINISHED docker:default
=> [internal] load build definition from mysql_dockerfile 0.0s
=> => transferring dockerfile: 161B 0.0s
=> [internal] load metadata for docker.io/library/php:7.2-fpm 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/2] FROM docker.io/library/php:7.2-fpm 0.0s
=> [2/2] RUN docker-php-ext-install pdo_mysql mysqli 15.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:e48f1a0e6209419dba3a93de4c263cd4c313224d0b490dcbb2589bb4c0eef552 0.0s
=> => naming to docker.io/library/php7.2-mysql5.7:v1 0.0s
[root@admin dockerfile]#
查看镜像
[root@admin dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php7.2-mysql5.7 v1 e48f1a0e6209 57 seconds ago 398MB //dockerfile构建的镜像
centos7 v1 f34ade5782a0 25 minutes ago 505MB
php 7.2-fpm 28f52b60203d 3 years ago 398MB
[root@admin dockerfile]#
进入容器查看mysql模块
[root@admin dockerfile]# docker run -it --name php-mysql php7.2-mysql5.7:v1 /bin/bash
root@59e58bc08b93:/var/www/html# php -m | grep mysql
mysqli
mysqlnd
pdo_mysql
root@59e58bc08b93:/var/www/html#
镜像转为压缩文件
将前面构建的phpmysql:v1镜像转为压缩⽂件,以便于保存和传输
格式:docker save -o 压缩⽂件名称 镜像名称:版本号
[root@admin dockerfile]# docker save -o php7.2_mysql5.7.tar.gz php7.2-mysql5.7:v1
[root@admin dockerfile]# ls
centos_dockerfile mysql_dockerfile php7.2_mysql5.7.tar.gz
[root@admin dockerfile]#
删除原来的镜像
[root@admin dockerfile]# docker rmi php7.2-mysql5.7:v1
Error response from daemon: conflict: unable to remove repository reference "php7.2-mysql5.7:v1" (must force) - container 59e58bc08b93 is using its referenced image e48f1a0e6209
[root@admin dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@admin dockerfile]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59e58bc08b93 php7.2-mysql5.7:v1 "docker-php-entrypoi…" 3 minutes ago Exited (0) 2 minutes ago php-mysql
e1243835e67c php:7.2-fpm "docker-php-entrypoi…" 11 minutes ago Exited (0) 10 minutes ago php
5fa0bb694e6e centos7:v1 "/bin/bash" 27 minutes ago Exited (127) 15 minutes ago centos7
[root@admin dockerfile]# docker rm php
php php-mysql
[root@admin dockerfile]# docker rm php-mysql
php-mysql
[root@admin dockerfile]# docker rmi php7.2-mysql5.7:v1
Untagged: php7.2-mysql5.7:v1
Deleted: sha256:e48f1a0e6209419dba3a93de4c263cd4c313224d0b490dcbb2589bb4c0eef552
[root@admin dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 v1 f34ade5782a0 31 minutes ago 505MB
php 7.2-fpm 28f52b60203d 3 years ago 398MB
[root@admin dockerfile]#
解压为镜像
格式:docker load -i 压缩⽂件名称
[root@admin dockerfile]# docker load -i php7.2_mysql5.7.tar.gz
Loaded image: php7.2-mysql5.7:v1
[root@admin dockerfile]#
[root@admin dockerfile]#
[root@admin dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php7.2-mysql5.7 v1 e48f1a0e6209 7 minutes ago 398MB
centos7 v1 f34ade5782a0 32 minutes ago 505MB
php 7.2-fpm 28f52b60203d 3 years ago 398MB
[root@admin dockerfile]#
基于镜像创建容器验证
[root@admin dockerfile]# docker run -it --name php_mysql php7.2-mysql5.7:v1 /bin/bash
root@d992a245404b:/var/www/html#
root@d992a245404b:/var/www/html# php -m |grep mysql
mysqli
mysqlnd
pdo_mysql
root@d992a245404b:/var/www/html#
容器转为镜像
容器可以是运⾏状态,也可以是退出状态。
提示:容器转为镜像时,容器内的配置或数据也会被保存到新镜像⽂件中,但是如果容器内做过数据卷,数据卷挂载的配置不会被保存到新镜像中
命令格式:docker commit 容器ID/容器名称 镜像名称:版本号
[root@admin ~]# docker run -d --name mysql_php php7.2-mysql5.7:v1
8e93a06e84ce237074a0fcf70b7f463ae4b6f1e214fec7910aec99a1591012f2
[root@admin ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e93a06e84ce php7.2-mysql5.7:v1 "docker-php-entrypoi…" 5 seconds ago Up 4 seconds 9000/tcp mysql_php
[root@admin ~]#
[root@admin ~]# docker commit mysql_php mysqlmy
sha256:d522ac70fafc0aafcea84221ea6edd3ff05d08fd0cade142490f5e18df2edd99
[root@admin ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysqlmy latest d522ac70fafc 6 seconds ago 398MB
php7.2-mysql5.7 v1 e48f1a0e6209 34 minutes ago 398MB
[root@admin ~]# docker run -d --name mysqlphp mysqlmy
ca9843d72c1bcd73094d4b369d7396ca3ae55a1c8a1f550d0c1097acda310cc8
[root@admin ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca9843d72c1b mysqlmy "docker-php-entrypoi…" 2 seconds ago Up 1 second 9000/tcp mysqlphp
8e93a06e84ce php7.2-mysql5.7:v1 "docker-php-entrypoi…" About a minute ago Up About a minute 9000/tcp mysql_php
[root@admin ~]#