目录
一、镜像概述
二、镜像构建方式
三、镜像构建案例
3.1、基于已有容器创建镜像
3.2、基于本地模板创建镜像
3.3、基于Dockerfile构建镜像
3.3.1、Docker 镜像结构
3.3.2、Dockerfile介绍
3.3.3、Dockerfile详解
3.3.4、Dockerfile构建SSHD镜像
3.3.5、Dockerfile构建httpd镜像
3.3.6、Dockerfile构建nginx镜像
3.3.7、Dockerfile构建Apache镜像
3.3.8、Dockerfile构建Tomcat镜像
3.3.9、Dockerfile构建MySQL镜像
3.3.10、Dockerfile构建LNMP镜像
一、镜像概述
Docker 镜像是Docker容器技术中的核心,也是应用打包构建发布的标准格式。一个完整的镜像可以支撑多个容器的运行,在Docker的整个使用过程中,进入一个已经定型的容器之后,就可以在容器中进行操作,最常见的操作就是在容器中安装应用服务。
如果想要把已经安装的服务容器进行迁移,就需要把环境以及部署的服务生成新的镜像。
程序打包方式:
- 打包成Tar包
- 打包成rpm包
- 打包成镜像
二、镜像构建方式
1、基于已有的容器创建镜像
2、基于本地模板创建镜像
3、基于Dockerfile创建镜像
三、镜像构建案例
3.1、基于已有容器创建镜像
基于现有镜像创建主要使用 docker commit 命令,即把一个容器里面运行的程序以及该程序的运行环境打包起来生成新的镜像。
命令格式:
docker commit [选项] 容器ID/名称 仓库名称:[标签]
常用选项:
- -m 说明信息;
- -a 作者信息;
- -p 生成过程中停止容器的运行。
首先启动一个镜像,在容器里做相应的修改,然后将修改后的容器提交为新的镜像。需要记住该容器的ID号。
[root@localhost ~]# docker run -it centos:7 /bin/bash
[root@34343130893c /]# touch zhang
[root@34343130893c /]# ls
bin dev fastboot lib lost+found mnt proc run srv tmp var
boot etc home lib64 media opt root sbin sys usr zhang
[root@34343130893c /]# exit
exit
[root@localhost ~]# docker commit -m"crushlinux test images" -a "zhang" 34343130893c centos7:new
sha256:aea5eeaa0c754e637272d7e6f84c863efa16cffd7df2885db00a582c1f70bb25
[root@localhost ~]# docker images centos7:new
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 new aea5eeaa0c75 10 seconds ago 589MB
[root@localhost ~]# docker run -it centos7:new /bin/bash
[root@f89114733c9c /]# ls -l zhang
-rw-r--r--. 1 root root 0 Aug 2 07:57 zhang
3.2、基于本地模板创建镜像
通过导入操作系统模板文件可以生成镜像,模板可以从 OPENVZ 开源项目下载,下载地址为 Index of /template/precreated。
下面是使用docker导入命令将下载的centos模板压缩包导入为本地镜像的例子。
[root@docker ~]# wget https://download.openvz.org/template/precreated/centos-7-x86_64.tar.gz
[root@docker ~]# ls -l centos-7-x86_64.tar.gz
-rw-r--r-- 1 root root 221692852 Jun 24 2016 centos-7-x86_64.tar.gz
[root@localhost ~]# cat centos-7-x86_64.tar.gz.1 | docker import - centos:test
sha256:3ce29162069990fd00c1bfcffdf68462a5af93c90139c97a9cb3f720cc153bc5
[root@localhost ~]# docker images centos:test
REPOSITORY TAG IMAGE ID CREATED SIZE
centos test 3ce291620699 22 seconds ago 589MB
导入完成后,会返回生成镜像的 ID 信息。查看本地镜像列表时,可以看到新创建的镜像信息。
3.3、基于Dockerfile构建镜像
3.3.1、Docker 镜像结构
镜像并不是一个单一的文件,而是由多层堆叠构成。可以通过docker history 命令查看镜像中各层的内容和大小,每层对应着Dockerfile 构建时的一条指令。Docker镜像默认存储在/var/lib/docker/<storage-driver>目录中。容器其实是在镜像的最上面加了一层读写层,在运行容器中做的任何文件改动,最终都会写到这个读写层。如果删除了容器,也就是删除了这个读写层,文件改动也就会丢失了。Docker使用存储驱动管理镜像每层内容及可读写层的容器层。
Docker镜像特点:
- Dockerfile中的每个指令都会创建一个新的镜像层;
- 镜像层可以被缓存和复用;
- 当Dockerfile中的指令被修改、复制的文件发生变化或者构建镜像时指定的变量值更换了,那么对应的镜像层缓存也将会失效;
- 某一层的镜像缓存失效,它之后的镜像层缓存都会失效;
- 镜像层是不可变的,如果在某一层中添加一个文件,然后在某一层中删除它,则镜像中依然会包含该文件,只是这个文件在Docker容器中不可见了。
3.3.2、Dockerfile介绍
Dockerfile是一种可以被Docker程序解释的脚本,Dockerfile由多条的指令堆叠组成,每条指令都对应Linux下面的一条命令。Docker程序将这些Dockerfile指令翻译成真正的Linux 命令。Dockerfile有自己书写格式和支持的命令,Docker程序解决这些命令间的依赖关系,类似于源码软件中的Makefile。Docker程序将读取Dockerfile并根据指令生成定制的镜像。
Dockerfile这种显而易见的脚本更容易被使用者接受,它明确的表明镜像是怎么产生的。有了Dockerfile当有定制额外的需求时,只需在Dockerfile上添加或者修改指令,重新生成镜像即可。
Dockerfile 结构大致分为四个部分:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。Dockerfile 每行支持一条指令,每条指令可携带多个参数,支持使用以“#“号开头的注释。
3.3.3、Dockerfile详解
Dockerfile的指令根据作用可以分为两种,构建指令和设置指令。构建指令用于构建image,其指定的操作不会在运行image的容器上执行;设置指令用于设置image的属性,其指定的操作将在运行image的容器中执行。
1、FROM 镜像:Tag
构建指令,必须指定且需要在Dockerfile文件中其他指令的前面。后续的指令都依赖于该指令指定的image。FROM指令指定的基础image可以是官方远程仓库中的,也可以位于本地仓库。
2、MAINTAINER 作者信息
构建指令,用于将image的制作者相关的信息写入到image中。当我们对该image执行docker inspect命令时,输出中有相应的字段记录该信息。
4、RUN 命令
构建指令,RUN可以运行任何被基础image支持的命令并提交到新的镜像中。如基础image选择ubuntu,那么软件管理部分只能使用ubuntu的命令。
5、CMD [“要运行的程序” , ”参数1” , ”参数2”]
设置指令,用于container(容器)启动时指定的操作。该操作可以是执行自定义脚本,也可以是执行系统命令。该指令只能在文件中存在一次(最后一行),如果有多个CMD命令,则只执行最后一条。
6、ENTRYPOINT(设置container启动时执行的操作)
设置指令,指定容器启动时执行的命令,可以多次设置,但是只有最后一个有效。
7、USER 用户名/UID
设置指令,设置启动容器的用户,默认是root用户。
8、EXPOSE 端口
设置指令,该指令会将容器中的端口映射成宿主机器中的某个端口。当你需要访问容器的时候,可以不使用容器的IP地址而是使用宿主机器的IP地址和映射后的端口。要完成整个操作需要两个步骤,首先在Dockerfile使用EXPOSE设置需要映射的容器端口,然后在运行容器的时候指定-p选项加上EXPOSE设置的端口,这样EXPOSE设置的端口号会被随机映射成宿主机器中的一个端口号。也可以指定需要映射到宿主机器的那个端口,这时要确保宿主机器上的端口号没有被使用。EXPOSE指令可以一次设置多个端口号,相应的运行容器的时候,可以配套的多次使用-p选项。
9、ENV 环境变量 变量值
构建指令,在image中设置一个环境变量。
设置了后,后续的RUN命令都可以使用,container启动后,可以通过docker inspect查看这个环境变量,也可以通过在docker run --env key=value时设置或修改环境变量。假如你安装了JAVA程序,需要设置JAVA_HOME,那么可以在Dockerfile中这样写:ENV JAVA_HOME /usr/local/java
10、ADD 源文件 目标文件
构建指令,ADD命令相对于COPY命令,可以解压缩文件并把它们添加到镜像中的功能,如果我们有一个压缩文件包,并且需要把这个压缩包中的文件添加到镜像中。需不需要先解开压缩包然后执行 COPY 命令呢?当然不需要!我们可以通过 ADD 命令一次搞定。
同时ADD还可以从 url 拷贝文件到镜像中,但官方不推荐这样使用,官方建议我们当需要从远程复制文件时,最好使用 curl 或 wget 命令来代替 ADD 命令。原因是,当使用 ADD 命令时,会创建更多的镜像层,当然镜像的 size 也会更大,所以ADD命令官方推荐只有在解压缩文件并把它们添加到镜像中时才需要。
11、COPY 源文件 目标文件
COPY命令用于将于Dockerfile所在目录中的文件在镜像构建阶段从宿主机拷贝到镜像中,对于文件而言可以直接将文件复制到镜像中,对于目录而言,该命令只复制目录中的内容而不包含目录自身
12、VOLUME [“目录”]
设置指令,使容器中的一个目录具有持久化存储数据的功能,该目录可以被容器本身使用,也可以共享给其他容器使用。我们知道容器使用的是AUFS,这种文件系统不能持久化数据,当容器关闭后,所有的更改都会丢失。当容器中的应用有持久化数据的需求时可以在Dockerfile中使用该指令。
13、WORKDIR 目录
设置指令相当于cd命令,为后续RUN,CMD,ENTRYPOINT指定工作目录。
14、ONBUILD 命令
指定所生产的镜像作为一个基础镜像时所要运行的命令
15、HEALTHCHECK
健康检查
在编写 Dockerfile 时,有严格的格式需要遵循:第一行必须使用 FROM 指令指明所基于的镜像名称;之后使用 MAINTAINER 指令说明维护该镜像的用户信息;然后是镜像操作相关指令,如 RUN 指令。每运行一条指令,都会给基础镜像添加新的一层。最后使用 CMD 指令指定启动容器时要运行的命令操作。
3.3.4、Dockerfile构建SSHD镜像
基于Dockerfile制作镜像时首先需要建立工作目录,作为生成镜像的工作目录,然后分别创建并编写 Dockerfile文件、需要运行的脚本文件以及要复制到容器中的文件。
[root@docker ~]# iptables -F
[root@docker ~]# setenforce 0
[root@docker ~]# systemctl stop firewalld
关闭DNS地址
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 8.8.8.8
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
DNS1=8.8.8.8
[root@localhost ~]# systemctl restart network
[root@localhost ~]# systemctl restart docker
导入基础镜像
[root@localhost ~]# cat centos-7-x86_64.tar.gz | docker import - centos:7
sha256:4f7ecce531315a6f09ae66026dc0ef38704f442b27c5b6f5a0432d74f3b2bf2c
建立工作目录
[root@localhost ~]# mkdir sshd
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:acYOwtr5UwxqTt4UXykNqxelbcVKgt1ekO6i5G+30js root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o ..+ |
| . + = + |
| % = |
| . o.=.X |
| o..*S* |
| o+o+=* . |
| .=o= +.o |
| o.= o E |
| .+.oo+ |
+----[SHA256]-----+
[root@localhost ~]# cp .ssh/id_rsa.pub sshd/
[root@localhost ~]# cd sshd
编写Dockerfile文件
[root@localhost sshd]# vim Dockerfile
#基于的基础镜像
FROM centos:7
#镜像作者信息
MAINTAINER Crushlinux <crushlinux@163.com>
#镜像执行的命令
RUN yum -y install openssh-server net-tools openssh-devel lsof telnet
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ADD id_rsa.pub /root/.ssh/authorized_keys
#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#开启 22 端口
EXPOSE 22
#启动容器时执行指令
CMD ["/usr/sbin/sshd" , "-D"]
[root@localhost sshd]# ls
Dockerfile id_rsa.pub
构建镜像
[root@localhost sshd]# docker build -t sshd:new .
[+] Building 76.3s (12/12) FINISHED docker:default
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 661B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [1/7] FROM docker.io/library/centos:7 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 507B 0.0s
=> [2/7] RUN yum -y install openssh-server net-tools openssh-devel lsof telnet 74.4s
=> [3/7] RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config 0.3s
=> [4/7] RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key 0.3s
=> [5/7] RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key 0.3s
=> [6/7] ADD id_rsa.pub /root/.ssh/authorized_keys 0.0s
=> [7/7] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.2s
=> exporting to image 0.7s
=> => exporting layers 0.7s
=> => writing image sha256:177c3f1dadb60c3b1140d3548c79c778ab6fc2edd6fea33c51a5abb68ef46f94 0.0s
=> => naming to docker.io/library/sshd:new 0.0s
[root@localhost sshd]# docker images sshd:new
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd new 177c3f1dadb6 45 seconds ago 820MB
测试容器
[root@localhost sshd]# docker run -d -p 2222:22 --name sshd-test --restart=always sshd:new
e3d34cb2eb46e403eaeb5b19276f47fbaac4b1fcbf8e8bc876f07de1e003e900
[root@localhost sshd]# ssh localhost -p 2222
The authenticity of host '[localhost]:2222 ([::1]:2222)' can't be established.
RSA key fingerprint is SHA256:hO4MQtZzkyhj5i4s/TuV5lyiDKxCbe+6bD+qk7tkkR4.
RSA key fingerprint is MD5:2e:05:bb:f2:fb:83:8e:c8:18:a4:b1:3d:cc:d7:d6:85.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:2222' (RSA) to the list of known hosts.
[root@e3d34cb2eb46 ~]#
3.3.5、Dockerfile构建httpd镜像
建立工作目录
[root@localhost ~]# mkdir httpd
[root@localhost ~]# cd httpd/
编写Dockerfile文件
[root@docker httpd]# vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux <crushlinux@163.com>
RUN yum -y install httpd
RUN echo "crushlinux" >/var/www/html/index.html
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 80
CMD ["httpd","-DFOREGROUND"]
构建镜像
[root@localhost httpd]# docker build -t httpd:new .
[+] Building 39.4s (8/8) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 330B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> CACHED [1/4] FROM docker.io/library/centos:7 0.0s
=> [2/4] RUN yum -y install httpd 38.1s
=> [3/4] RUN echo "crushlinux" >/var/www/html/index.html 0.4s
=> [4/4] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.2s
=> exporting to image 0.7s
=> => exporting layers 0.7s
=> => writing image sha256:14e0aee71507c7bc47d02910d884f2ba6ff010df6b93f1804a156c178dde5f12 0.0s
=> => naming to docker.io/library/httpd:new 0.0s
[root@localhost httpd]# docker images httpd
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd new 14e0aee71507 About a minute ago 818MB
测试容器
[root@localhost httpd]# docker run -d -p 801:80 --name httpd-test --restart=always httpd:new
af87d01f73e443145b557c9b2f5f4a7fc59d57911144e6954243f98839f03ca2
3.3.6、Dockerfile构建nginx镜像
建立工作目录
[root@localhost ~]# mkdir nginx
[root@localhost ~]# cd nginx/
编写Dockerfile文件
[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@localhost nginx]# vim Dockerfile
#基于的基础镜像
FROM centos:7
#镜像作者信息
MAINTAINER Crushlinux <crushlinux@163.com>
#安装相关依赖包
RUN yum install -y wget proc-devel net-tools gcc zlib zlib-devel make openssl-devel
#下载并解压nginx源码包
RUN wget http://nginx.org/download/nginx-1.19.0.tar.gz && tar zxf nginx-1.19.0.tar.gz
#编译安装nginx
RUN cd nginx-1.19.0 && ./configure --prefix=/usr/local/nginx && make && make install
#开启 80 和 443 端口
EXPOSE 80
#修改 Nginx 配置文件,以非 daemon 方式启动
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#复制服务启动脚本并设置权限
ADD run.sh /run.sh
RUN chmod 775 /run.sh
#启动容器时执行脚本
CMD ["/run.sh"]
构建镜像
[root@localhost nginx]# docker build -t nginx:new .
[+] Building 213.8s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 884B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> CACHED [1/8] FROM docker.io/library/centos:7 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 133B 0.0s
=> [2/8] RUN yum install -y wget proc-devel net-tools gcc zlib zlib-devel make openssl-de 192.4s
=> [3/8] RUN wget http://nginx.org/download/nginx-1.19.0.tar.gz && tar zxf nginx-1.19.0.tar 4.7s
=> [4/8] RUN cd nginx-1.19.0 && ./configure --prefix=/usr/local/nginx && make && make inst 14.3s
=> [5/8] RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf 0.2s
=> [6/8] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.2s
=> [7/8] ADD run.sh /run.sh 0.0s
=> [8/8] RUN chmod 775 /run.sh 0.2s
=> exporting to image 1.7s
=> => exporting layers 1.7s
=> => writing image sha256:5d99f1af2c29dcbd2905194921e00ad167c7831d79bc7201530698e872b68cc6 0.0s
=> => naming to docker.io/library/nginx:new 0.0s
[root@localhost nginx]# docker images nginx:new
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx new 5d99f1af2c29 About a minute ago 1.1GB
测试容器
[root@localhost nginx]# docker run -d -p 802:80 --name nginx-test --restart=always nginx:new
4333cb30aa0710b19a26c33cadf0d7edf09ba096f9a857178a510e8b3c2e7544
3.3.7、Dockerfile构建Apache镜像
建立工作目录
[root@localhost ~]# mkdir apache
[root@localhost ~]# cd apache/
编写Dockerfile文件
#基于的基础镜像
FROM centos:7
#镜像作者信息
MAINTAINER Crushlinux <crushlinux@163.com>
#安装相关依赖包
RUN yum install -y wget net-tools zlib make openssl-devel apr apr-util apr-util-devel gcc gcc-c++ pcre-devel zlib-devel
#下载并解压apache源码包
RUN wget https://downloads.apache.org/httpd/httpd-2.4.57.tar.gz && tar zxf httpd-2.4.57.tar.gz
#编译安装apache
RUN cd httpd-2.4.57 && ./configure --prefix=/usr/local/apache && make && make install
#开启 80 和 443 端口
EXPOSE 80
#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#启动容器时执行脚本
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/local/apache/bin/apachectl"]
构建镜像
[root@localhost apache]# docker build -t apache:new .
[+] Building 1355.0s (9/9) FINISHED docker:default
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 792B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 15.6s
=> CACHED [1/5] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe 0.0s
=> [2/5] RUN yum install -y wget net-tools zlib make openssl-devel apr apr-util apr-util-devel 100.0s
=> [3/5] RUN wget https://downloads.apache.org/httpd/httpd-2.4.57.tar.gz && tar zxf httpd-2.4. 1195.4s
=> [4/5] RUN cd httpd-2.4.57 && ./configure --prefix=/usr/local/apache && make && make install 42.4s
=> [5/5] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.2s
=> exporting to image 1.3s
=> => exporting layers 1.3s
=> => writing image sha256:be6218038cf6c949bb9ca734557e8883ab077abc0526d49798566ce36e864967 0.0s
=> => naming to docker.io/library/apache:new
[root@localhost apache]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
apache new be6218038cf6 36 seconds ago 632MB
测试容器
[root@localhost apache]# docker run -d -p 802:80 --name apache-test --restart=always
apache:new
a254060199388e86c6f65231c96bb1ba900df06e0349ad67480a9f67664a4e85
[root@localhost apache]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a25406019938 apache:new "/usr/local/apache/b…" 7 seconds ago Up 7 seconds 0.0.0.0:802->80/tcp, :::802->80/tcp apache-test
3.3.8、Dockerfile构建Tomcat镜像
建立工作目录
[root@localhost ~]# mkdir tomcat
[root@localhost ~]# cd tomcat/
[root@localhost tomcat]# tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost tomcat]# tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost tomcat]# ls
apache-tomcat-8.5.40 apache-tomcat-8.5.40.tar.gz jdk1.8.0_191 jdk-8u191-linux-x64.tar.gz
编写Dockerfile文件
[root@localhost tomcat]# vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux <crushlinux@163.com>
ADD jdk1.8.0_191 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-8.5.40 /usr/local/tomcat
RUN chmod 755 /usr/local/tomcat/bin/startup.sh
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
构建镜像
[root@localhost tomcat]# docker build -t tomcat:new .
[+] Building 5.8s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 655B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> CACHED [1/5] FROM docker.io/library/centos:7 0.0s
=> [internal] load build context 2.9s
=> => transferring context: 410.87MB 2.9s
=> [2/5] ADD jdk1.8.0_191 /usr/local/java 0.9s
=> [3/5] ADD apache-tomcat-8.5.40 /usr/local/tomcat 0.1s
=> [4/5] RUN chmod 755 /usr/local/tomcat/bin/startup.sh 0.2s
=> [5/5] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 0.3s
=> exporting to image 1.3s
=> => exporting layers 1.3s
=> => writing image sha256:c5c9b935446d83580c018dab9e006c80fea98527aec5ca7a645fe7f65e6c7d59 0.0s
=> => naming to docker.io/library/tomcat:new 0.0s
[root@localhost tomcat]# docker images tomcat:new
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat new c5c9b935446d 24 seconds ago 1e+03MB
测试容器
[root@localhost tomcat]# docker run -d -p 8080:8080 --name tomcat-test --restart=always tomcat:new
4fd46fee151ddfaac8f4d5df621cf661e7256ee7d4eb9d3c3059c3d3e58ca406
3.3.9、Dockerfile构建MySQL镜像
建立工作目录
[root@localhost ~]# mkdir mysql
[root@localhost ~]# cd mysql/
编写Dockerfile文件
[root@docker mysql]# vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux <crushlinux@163.com>
#安装mariadb数据库
RUN yum install -y mariadb mariadb-server mariadb-devel
#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
#让容器支持中文
ENV LC_ALL en_US.UTF-8
#初始化数据库
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh && /root/db_init.sh
#导出端口
EXPOSE 3306
#设置默认启动命令
CMD ["mysqld_safe"]
[root@localhost mysql]# cat db_init.sh
#!/bin/bash
mysql_install_db --user=mysql
sleep 3
mysqld_safe &
sleep 3
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"
构建镜像
[root@localhost mysql]# docker build -t mysql:new .
[+] Building 75.6s (9/9) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 546B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 15.3s
=> CACHED [1/4] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 451B 0.0s
=> [2/4] RUN yum install -y mariadb mariadb-server mariadb-devel 50.3s
=> [3/4] ADD db_init.sh /root/db_init.sh 0.1s
=> [4/4] RUN chmod 775 /root/db_init.sh && /root/db_init.sh 8.4s
=> exporting to image 1.2s
=> => exporting layers 1.2s
=> => writing image sha256:a85df087507fc901c1ebc986024362ff0a44dc00a32621cc0dc3b 0.0s
=> => naming to docker.io/library/mysql:new 0.0s
[root@localhost mysql]# docker run -d -p 3306:3306 --name mysql --restart=always mysql:new
760e048d28415fa333d589e55583609725c99665cfb83768c7c3a79d34d7f172
测试容器
[root@localhost mysql]# yum -y install mariadb mariadb-devel
[root@localhost mysql]# mysql -h 192.168.2.118 -u root -P 3306 -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
3.3.10、Dockerfile构建LNMP镜像
建立工作目录
[root@localhost ~]# mkdir lnmp/
[root@localhost ~]# cd lnmp/
[root@localhost lnmp]#
编写Dockerfile文件
[root@docker lnmp]# vim Dockerfile
#基础镜像
FROM centos:7
#维护该镜像的用户信息
MAINTAINER Crushlinux <crushlinux@163.com>
#配置Nginx 的YUM 源
RUN rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#安装nginx
RUN yum -y install nginx
#安装mysql 和php
RUN rpm --rebuilddb && yum -y install mariadb-devel mariadb-server mariadb php php-fpm
#修改nginx 配置文件,使之支持PHP
ADD default.conf /etc/nginx/conf.d/default.conf
#修改php-fpm 配置文件允许nginx 访问
RUN sed -i '/^user/s/apache/nginx/g' /etc/php-fpm.d/www.conf
RUN sed -i '/^group/s/apache/nginx/g' /etc/php-fpm.d/www.conf
#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
#mysql 数据库授权
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh
RUN /root/db_init.sh
#添加测试页面
ADD index.php /usr/share/nginx/html/index.php
#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#分别开启80 端口,443 端口,9000 端口,3360 端口
EXPOSE 80
EXPOSE 9000
EXPOSE 3306
#复制脚本,设置权限,启动容器时启动该脚本
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]
[root@localhost lnmp]# cat db_init.sh
#!/bin/bash
/usr/bin/mysql_install_db --user=mysql
sleep 3
/usr/bin/mysqld_safe &
sleep 3
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"
[root@localhost lnmp]# cat index.php
<?php
phpinfo();
?>
[root@localhost lnmp]# cat default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.php index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
[root@docker lnmp]# vim run.sh
#!/bin/bash
/usr/sbin/nginx && /usr/sbin/php-fpm && /usr/bin/mysqld_safe
构建镜像
[root@localhost lnmp]# docker build -t lnmp:new .
[+] Building 289.5s (19/19) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 1.29kB 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 15.5s
=> CACHED [ 1/14] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be3 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.31kB 0.0s
=> [ 2/14] RUN rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-rel 3.5s
=> [ 3/14] RUN yum -y install nginx 164.5s
=> [ 4/14] RUN rpm --rebuilddb && yum -y install mariadb-devel mariadb-server m 88.3s
=> [ 5/14] ADD default.conf /etc/nginx/conf.d/default.conf 0.1s
=> [ 6/14] RUN sed -i '/^user/s/apache/nginx/g' /etc/php-fpm.d/www.conf 1.4s
=> [ 7/14] RUN sed -i '/^group/s/apache/nginx/g' /etc/php-fpm.d/www.conf 1.5s
=> [ 8/14] ADD db_init.sh /root/db_init.sh 0.1s
=> [ 9/14] RUN chmod 775 /root/db_init.sh 1.3s
=> [10/14] RUN /root/db_init.sh 8.7s
=> [11/14] ADD index.php /usr/share/nginx/html/index.php 0.1s
=> [12/14] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 1.3s
=> [13/14] ADD run.sh /run.sh 0.1s
=> [14/14] RUN chmod 775 /run.sh 1.3s
=> exporting to image 1.5s
=> => exporting layers 1.5s
=> => writing image sha256:6a083184147765140594482eb4daca6bfa30be76673ce9f40850f 0.0s
=> => naming to docker.io/library/lnmp:new 0.0s
[root@localhost lnmp]# docker images lnmp
REPOSITORY TAG IMAGE ID CREATED SIZE
lnmp new 6a0831841477 43 seconds ago 905MB
测试容器
[root@localhost lnmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
760e048d2841 mysql:new "mysqld_safe" 24 minutes ago Up 24 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql
a25406019938 apache:new "/usr/local/apache/b…" 6 hours ago Up 6 hours 0.0.0.0:802->80/tcp, :::802->80/tcp apache-test
[root@localhost lnmp]# docker rm -f $(docker ps -qa)
760e048d2841
a25406019938
[root@localhost lnmp]# docker run -d -p 80:80 -p 3306:3306 -p 9000:9000 --name lnmp --restart=always lnmp:new
36462df3f0c37621da19fc6c37127bf2bebdf6836bde16cd9c66abaf2b31f6ff
3.3.11、Dockerfile构建Redis镜像
建立工作目录
[root@localhost ~]# mkdir redis
[root@localhost ~]# cd redis/
[root@localhost redis]#
编写Dockerfile文件
[root@docker redis]# vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux <crushlinux@163.com>
RUN yum -y install epel-release && yum -y install redis
RUN sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
RUN sed -i -e 's@protected-mode yes@protected-mode no@g' /etc/redis.conf
RUN echo "requirepass 123456" >> /etc/redis.conf
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 6379
CMD [ "/usr/bin/redis-server","/etc/redis.conf"]
构建镜像
[root@localhost redis]# docker build -t redis:new .
[+] Building 564.3s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 522B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 17.3s
=> CACHED [1/6] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b 0.0s
=> [2/6] RUN yum -y install epel-release && yum -y install redis 541.6s
=> [3/6] RUN sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf 1.6s
=> [4/6] RUN sed -i -e 's@protected-mode yes@protected-mode no@g' /etc/redis.con 0.9s
=> [5/6] RUN echo "requirepass 123456" >> /etc/redis.conf 1.0s
=> [6/6] RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 1.0s
=> exporting to image 0.7s
=> => exporting layers 0.7s
=> => writing image sha256:36158da4a29d214375266e4fc5f02397e1bca9b06ef25cc0d8b7f 0.0s
=> => naming to docker.io/library/redis:new 0.0s
[root@localhost redis]# docker images redis:new
REPOSITORY TAG IMAGE ID CREATED SIZE
redis new 36158da4a29d 47 seconds ago 459MB
测试容器
[root@localhost redis]# docker run -d -p 6379:6379 --name redis --restart=always redis:n
ew
8b4175bc1627d50c3dd814168efcdbb3e7fea0d63835a8ea994f99addb53f2d7
[root@localhost redis]# yum -y install redis
[root@localhost redis]# redis-cli -h localhost -a 123456
localhost:6379> exit
[root@localhost redis]#