通过实例讲解Docker:Dockerfile构建镜像创建Nginx服务
前一篇文章讲了用Docker普通方法创建Tomcat服务,大家对Docker容器已经有了一定的认识。 Docker更好的地方是可以完全定制化自己的镜像和容器。
我们可以通过创建Dockerfile文件定制化自己的镜像。 Dockerfile文件支持的指令大概有十三个: FROM MAINTAINER RUN CMD EXPOSE ENV ADD COPY ENTRYPOINT VOLUME USER WORKDIR
通过这十三个命令,我们就可以构建我们自己的镜像了。
首先,创建一个目录,用于存放Dockerfile文件和制作镜像需要的一些文件。
# mkdir nginx
# cd nginx
# vim Dockerfile
FROM centos:7
MAINTAINER itbing xxxxx@qq.com
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
ADD nginx-1.14.2.tar.gz /tmp
RUN cd /tmp/nginx-1.14.2 && ./configure --prefix=/usr/local/nginx && make -j 2 && make install
RUN rm -rf /tmp/nginx-1.14.2
COPY nginx.conf /usr/local/nginx/conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD [“./sbin/nginx“, “-g“, “daemon off;“]
FROM centos:7:以centos7为基础源的镜像。FROM一定是第一句。要部署任何应用,必须要先有一个基础环境才能运行。基础操作系统还可以是Ubuntu/Debian/RHEL等等。根据你要部署的应用选择合适的基础源镜像。 MAINTAINER itbing xxxxx@qq.com:指定镜像制作者的用户名和邮箱地址。 RUN:要在创建镜像时执行的命令。 ADD:添加本地文件进镜像中。添加压缩文件时,会自动解压。COPY命令也是添加本地文件进镜像中,但不会解压压缩文件。 WORKDIR:指定当前工作的目录。后面执行命令时,都是在这个工作目录中。 EXPOSE:容器对外映射出来的端口。 CMD ["./sbin/nginx", “-g”, “daemon off;"]:在容器内开启应用的命令。CMD只能有一个,如果有多个的话,以最后一个为准。
创建好Dockerfile文件后,还需要把Dockerfile文件用到的文件跟Dockerfile文件放在同一个目录下。
最终目录内容:
# ls
Dockerfile nginx-1.14.2.tar.gz nginx.conf
文件准备齐全后,就可以使用docker image build来创建出一个镜像了。
docker image build -t 名称:tag -f Dockerfile名称 .
注意:最后有一个“.",表示当前目录。
进入到存放Dockerfile文件的目录内
# cd nginx
# docker image build -t nginx:v1 -f Dockerfile .
Step 1/10 : FROM centos:7
7: Pulling from library/centos
d8d02d457314: Pull complete
Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb
Status: Downloaded newer image for centos:7
--->; 67fa590cfc1c
Step 2/10 : MAINTAINER itbing xxxxx@qq.com
--->; Running in 1cf2e4ea263c
Removing intermediate container 1cf2e4ea263c
--->; e7160a3c6fe7
Step 3/10 : RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
--->; Running in 9c3b0596fbf2
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.163.com
Resolving Dependencies
-->; Running transaction check
--->; Package gcc.x86_64 0:4.8.5-39.el7 will be installed
-->; Processing Dependency: libgomp = 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64
-->; Processing Dependency: cpp = 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64
-->; Processing Dependency: libgcc >;= 4.8.5-39.el7 for package: gcc-4.8.5-39.el7.x86_64
-->; Processing Dependency: glibc-devel >;= 2.2.90-12 for package: gcc-4.8.5-39.el7.x86_64
-->; Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.5-39.el7.x86_64
-->; Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.5-39.el7.x86_64
-->; Processing Dependency: libgomp.so.1()(64bit) for package: gcc-4.8.5-39.el7.x86_64
--->; Package gcc-c++.x86_64 0:4.8.5-39.el7 will be installed
-->; Processing Dependency: libstdc++-devel = 4.8.5-39.el7 for package: gcc-c++-4.8.5-39.el7.x86_64
-->; Processing Dependency: libstdc++ = 4.8.5-39.el7 for package: gcc-c++-4.8.5-39.el7.x86_64
--->; Package make.x86_64 1:3.82-24.el7 will be installed
--->; Package openssl-devel.x86_64 1:1.0.2k-19.el7 will be installed
-->; Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-19.el7 for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
-->; Processing Dependency: zlib-devel(x86-64) for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
-->; Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-19.el7.x86_64
--->; Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
-->; Running transaction check
--->; Package cpp.x86_64 0:4.8.5-39.el7 will be installed
--->; Package glibc-devel.x86_64 0:2.17-292.el7 will be installed
-->; Processing Dependency: glibc-headers = 2.17-292.el7 for package: glibc-devel-2.17-292.el7.x86_64
-->; Processing Dependency: glibc = 2.17-292.el7 for package: glibc-devel-2.17-292.el7.x86_64
-->; Processing Dependency: glibc-headers for package: glibc-devel-2.17-292.el7.x86_64
--->; Package krb5-devel.x86_64 0:1.15.1-37.el7_7.2 will be installed
-->; Processing Dependency: libkadm5(x86-64) = 1.15.1-37.el7_7.2 for package: krb5-devel-1.15.1-37.el7_7.2.x86_64
-->; Processing Dependency: krb5-libs(x86-64) = 1.15.1-37.el7_7.2 for package: krb5-devel-1.15.1-37.el7_7.2.x86_64
-->; Processing Dependency: libverto-devel for package: krb5-devel-1.15.1-37.el7_7.2.x86_64
-->; Processing Dependency: libselinux-devel for package: krb5-devel-1.15.1-37.el7_7.2.x86_64
-->; Processing Dependency: libcom_err-devel for package: krb5-devel-1.15.1-37.el7_7.2.x86_64
-->; Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.15.1-37.el7_7.2.x86_64
--->; Package libgcc.x86_64 0:4.8.5-36.el7_6.2 will be updated
--->; Package libgcc.x86_64 0:4.8.5-39.el7 will be an update
--->; Package libgomp.x86_64 0:4.8.5-39.el7 will be installed
--->; Package libmpc.x86_64 0:1.0.1-3.el7 will be installed
--->; Package libstdc++.x86_64 0:4.8.5-36.el7_6.2 will be updated
--->; Package libstdc++.x86_64 0:4.8.5-39.el7 will be an update
--->; Package libstdc++-devel.x86_64 0:4.8.5-39.el7 will be installed
--->; Package mpfr.x86_64 0:3.1.1-4.el7 will be installed
--->; Package openssl-libs.x86_64 1:1.0.2k-16.el7_6.1 will be updated
--->; Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be an update
--->; Package zlib-devel.x86_64 0:1.2.7-18.el7 will be installed
-->; Running transaction check
--->; Package glibc.x86_64 0:2.17-260.el7_6.6 will be updated
-->; Processing Dependency: glibc = 2.17-260.el7_6.6 for package: glibc-common-2.17-260.el7_6.6.x86_64
--->; Package glibc.x86_64 0:2.17-292.el7 will be an update
--->; Package glibc-headers.x86_64 0:2.17-292.el7 will be installed
-->; Processing Dependency: kernel-headers >;= 2.2.1 for package: glibc-headers-2.17-292.el7.x86_64
-->; Processing Dependency: kernel-headers for package: glibc-headers-2.17-292.el7.x86_64
--->; Package keyutils-libs-devel.x86_64 0:1.5.8-3.el7 will be installed
--->; Package krb5-libs.x86_64 0:1.15.1-37.el7_6 will be updated
--->; Package krb5-libs.x86_64 0:1.15.1-37.el7_7.2 will be an update
--->; Package libcom_err-devel.x86_64 0:1.42.9-16.el7 will be installed
-->; Processing Dependency: libcom_err(x86-64) = 1.42.9-16.el7 for package: libcom_err-devel-1.42.9-16.el7.x86_64
--->; Package libkadm5.x86_64 0:1.15.1-37.el7_7.2 will be installed
--->; Package libselinux-devel.x86_64 0:2.5-14.1.el7 will be installed
-->; Processing Dependency: libsepol-devel(x86-64) >;= 2.5-10 for package: libselinux-devel-2.5-14.1.el7.x86_64
-->; Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.5-14.1.el7.x86_64
--->; Package libverto-devel.x86_64 0:0.2.5-4.el7 will be installed
-->; Running transaction check
--->; Package glibc-common.x86_64 0:2.17-260.el7_6.6 will be updated
--->; Package glibc-common.x86_64 0:2.17-292.el7 will be an update
--->; Package kernel-headers.x86_64 0:3.10.0-1062.1.1.el7 will be installed
--->; Package libcom_err.x86_64 0:1.42.9-13.el7 will be updated
--->; Package libcom_err.x86_64 0:1.42.9-16.el7 will be an update
--->; Package libsepol-devel.x86_64 0:2.5-10.el7 will be installed
-->; Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
gcc x86_64 4.8.5-39.el7 base 16 M
gcc-c++ x86_64 4.8.5-39.el7 base 7.2 M
make x86_64 1:3.82-24.el7 base 421 k
openssl-devel x86_64 1:1.0.2k-19.el7 base 1.5 M
pcre-devel x86_64 8.32-17.el7 base 480 k
Installing for dependencies:
cpp x86_64 4.8.5-39.el7 base 5.9 M
glibc-devel x86_64 2.17-292.el7 base 1.1 M
glibc-headers x86_64 2.17-292.el7 base 687 k
kernel-headers x86_64 3.10.0-1062.1.1.el7 updates 8.7 M
keyutils-libs-devel x86_64 1.5.8-3.el7 base 37 k
krb5-devel x86_64 1.15.1-37.el7_7.2 updates 272 k
libcom_err-devel x86_64 1.42.9-16.el7 base 32 k
libgomp x86_64 4.8.5-39.el7 base 158 k
libkadm5 x86_64 1.15.1-37.el7_7.2 updates 178 k
libmpc x86_64 1.0.1-3.el7 base 51 k
libselinux-devel x86_64 2.5-14.1.el7 base 187 k
libsepol-devel x86_64 2.5-10.el7 base 77 k
libstdc++-devel x86_64 4.8.5-39.el7 base 1.5 M
libverto-devel x86_64 0.2.5-4.el7 base 12 k
mpfr x86_64 3.1.1-4.el7 base 203 k
zlib-devel x86_64 1.2.7-18.el7 base 50 k
Updating for dependencies:
glibc x86_64 2.17-292.el7 base 3.6 M
glibc-common x86_64 2.17-292.el7 base 11 M
krb5-libs x86_64 1.15.1-37.el7_7.2 updates 805 k
libcom_err x86_64 1.42.9-16.el7 base 41 k
libgcc x86_64 4.8.5-39.el7 base 102 k
libstdc++ x86_64 4.8.5-39.el7 base 305 k
openssl-libs x86_64 1:1.0.2k-19.el7 base 1.2 M
Transaction Summary
================================================================================
Install 5 Packages (+16 Dependent packages)
Upgrade ( 7 Dependent packages)
Total download size: 62 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/base/packages/gcc-c++-4.8.5-39.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for gcc-c++-4.8.5-39.el7.x86_64.rpm is not installed
Public key for krb5-devel-1.15.1-37.el7_7.2.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total 3.6 MB/s | 62 MB 00:17
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : “CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>;“
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : libgcc-4.8.5-39.el7.x86_64 1/35
Updating : glibc-2.17-292.el7.x86_64 2/35
warning: /etc/nsswitch.conf created as /etc/nsswitch.conf.rpmnew
Updating : glibc-common-2.17-292.el7.x86_64 3/35
Updating : libcom_err-1.42.9-16.el7.x86_64 4/35
Installing : mpfr-3.1.1-4.el7.x86_64 5/35
Installing : libmpc-1.0.1-3.el7.x86_64 6/35
Updating : krb5-libs-1.15.1-37.el7_7.2.x86_64 7/35
Updating : 1:openssl-libs-1.0.2k-19.el7.x86_64 8/35
Updating : libstdc++-4.8.5-39.el7.x86_64 9/35
Installing : libstdc++-devel-4.8.5-39.el7.x86_64 10/35
Installing : libkadm5-1.15.1-37.el7_7.2.x86_64 11/35
Installing : cpp-4.8.5-39.el7.x86_64 12/35
Installing : libcom_err-devel-1.42.9-16.el7.x86_64 13/35
Installing : libgomp-4.8.5-39.el7.x86_64 14/35
Installing : kernel-headers-3.10.0-1062.1.1.el7.x86_64 15/35
Installing : glibc-headers-2.17-292.el7.x86_64 16/35
Installing : glibc-devel-2.17-292.el7.x86_64 17/35
Installing : gcc-4.8.5-39.el7.x86_64 18/35
Installing : zlib-devel-1.2.7-18.el7.x86_64 19/35
Installing : libverto-devel-0.2.5-4.el7.x86_64 20/35
Installing : libsepol-devel-2.5-10.el7.x86_64 21/35
Installing : pcre-devel-8.32-17.el7.x86_64 22/35
Installing : libselinux-devel-2.5-14.1.el7.x86_64 23/35
Installing : keyutils-libs-devel-1.5.8-3.el7.x86_64 24/35
Installing : krb5-devel-1.15.1-37.el7_7.2.x86_64 25/35
Installing : 1:openssl-devel-1.0.2k-19.el7.x86_64 26/35
Installing : gcc-c++-4.8.5-39.el7.x86_64 27/35
Installing : 1:make-3.82-24.el7.x86_64 28/35
Cleanup : 1:openssl-libs-1.0.2k-16.el7_6.1.x86_64 29/35
Cleanup : krb5-libs-1.15.1-37.el7_6.x86_64 30/35
Cleanup : libstdc++-4.8.5-36.el7_6.2.x86_64 31/35
Cleanup : libcom_err-1.42.9-13.el7.x86_64 32/35
Cleanup : glibc-common-2.17-260.el7_6.6.x86_64 33/35
Cleanup : glibc-2.17-260.el7_6.6.x86_64 34/35
Cleanup : libgcc-4.8.5-36.el7_6.2.x86_64 35/35
Verifying : keyutils-libs-devel-1.5.8-3.el7.x86_64 1/35
Verifying : krb5-devel-1.15.1-37.el7_7.2.x86_64 2/35
Verifying : libcom_err-devel-1.42.9-16.el7.x86_64 3/35
Verifying : pcre-devel-8.32-17.el7.x86_64 4/35
Verifying : gcc-4.8.5-39.el7.x86_64 5/35
Verifying : libstdc++-4.8.5-39.el7.x86_64 6/35
Verifying : libsepol-devel-2.5-10.el7.x86_64 7/35
Verifying : libverto-devel-0.2.5-4.el7.x86_64 8/35
Verifying : zlib-devel-1.2.7-18.el7.x86_64 9/35
Verifying : libgomp-4.8.5-39.el7.x86_64 10/35
Verifying : 1:make-3.82-24.el7.x86_64 11/35
Verifying : glibc-common-2.17-292.el7.x86_64 12/35
Verifying : libstdc++-devel-4.8.5-39.el7.x86_64 13/35
Verifying : libgcc-4.8.5-39.el7.x86_64 14/35
Verifying : krb5-libs-1.15.1-37.el7_7.2.x86_64 15/35
Verifying : gcc-c++-4.8.5-39.el7.x86_64 16/35
Verifying : 1:openssl-devel-1.0.2k-19.el7.x86_64 17/35
Verifying : libmpc-1.0.1-3.el7.x86_64 18/35
Verifying : libcom_err-1.42.9-16.el7.x86_64 19/35
Verifying : glibc-2.17-292.el7.x86_64 20/35
Verifying : 1:openssl-libs-1.0.2k-19.el7.x86_64 21/35
Verifying : kernel-headers-3.10.0-1062.1.1.el7.x86_64 22/35
Verifying : glibc-devel-2.17-292.el7.x86_64 23/35
Verifying : mpfr-3.1.1-4.el7.x86_64 24/35
Verifying : libkadm5-1.15.1-37.el7_7.2.x86_64 25/35
Verifying : libselinux-devel-2.5-14.1.el7.x86_64 26/35
Verifying : cpp-4.8.5-39.el7.x86_64 27/35
Verifying : glibc-headers-2.17-292.el7.x86_64 28/35
Verifying : libcom_err-1.42.9-13.el7.x86_64 29/35
Verifying : glibc-2.17-260.el7_6.6.x86_64 30/35
Verifying : krb5-libs-1.15.1-37.el7_6.x86_64 31/35
Verifying : libgcc-4.8.5-36.el7_6.2.x86_64 32/35
Verifying : libstdc++-4.8.5-36.el7_6.2.x86_64 33/35
Verifying : 1:openssl-libs-1.0.2k-16.el7_6.1.x86_64 34/35
Verifying : glibc-common-2.17-260.el7_6.6.x86_64 35/35
Installed:
gcc.x86_64 0:4.8.5-39.el7 gcc-c++.x86_64 0:4.8.5-39.el7
make.x86_64 1:3.82-24.el7 openssl-devel.x86_64 1:1.0.2k-19.el7
pcre-devel.x86_64 0:8.32-17.el7
Dependency Installed:
cpp.x86_64 0:4.8.5-39.el7
glibc-devel.x86_64 0:2.17-292.el7
glibc-headers.x86_64 0:2.17-292.el7
kernel-headers.x86_64 0:3.10.0-1062.1.1.el7
keyutils-libs-devel.x86_64 0:1.5.8-3.el7
krb5-devel.x86_64 0:1.15.1-37.el7_7.2
libcom_err-devel.x86_64 0:1.42.9-16.el7
libgomp.x86_64 0:4.8.5-39.el7
libkadm5.x86_64 0:1.15.1-37.el7_7.2
libmpc.x86_64 0:1.0.1-3.el7
libselinux-devel.x86_64 0:2.5-14.1.el7
libsepol-devel.x86_64 0:2.5-10.el7
libstdc++-devel.x86_64 0:4.8.5-39.el7
libverto-devel.x86_64 0:0.2.5-4.el7
mpfr.x86_64 0:3.1.1-4.el7
zlib-devel.x86_64 0:1.2.7-18.el7
Dependency Updated:
glibc.x86_64 0:2.17-292.el7 glibc-common.x86_64 0:2.17-292.el7
krb5-libs.x86_64 0:1.15.1-37.el7_7.2 libcom_err.x86_64 0:1.42.9-16.el7
libgcc.x86_64 0:4.8.5-39.el7 libstdc++.x86_64 0:4.8.5-39.el7
openssl-libs.x86_64 1:1.0.2k-19.el7
Complete!
Removing intermediate container 9c3b0596fbf2
--->; 35cd3ea89970
Step 4/10 : ADD nginx-1.14.2.tar.gz /tmp
--->; 8a4f605b8ce5
Step 5/10 : RUN cd /tmp/nginx-1.14.2 && ./configure --prefix=/usr/local/nginx && make -j 2 && make install
--->; Running in 88115d79a556
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
.........(省略nginx编译过程).................
test -d \'/usr/local/nginx/logs\' \\
|| mkdir -p \'/usr/local/nginx/logs\'
make[1]: Leaving directory `/tmp/nginx-1.14.2\'
Removing intermediate container 88115d79a556
--->; fd23e067dc28
Step 6/10 : RUN rm -rf /tmp/nginx-1.14.2
--->; Running in 3cdcbec35f30
Removing intermediate container 3cdcbec35f30
--->; beb13f564035
Step 7/10 : COPY nginx.conf /usr/local/nginx/conf
--->; 764231df92e9
Step 8/10 : WORKDIR /usr/local/nginx
--->; Running in 43f45b9bedc9
Removing intermediate container 43f45b9bedc9
--->; 853a1b6989f4
Step 9/10 : EXPOSE 80
--->; Running in 8eeb8fa0aaf4
Removing intermediate container 8eeb8fa0aaf4
--->; fdb91e8a1a74
Step 10/10 : CMD [“./sbin/nginx“, “-g“, “daemon off;“]
--->; Running in 2904a157d7fc
Removing intermediate container 2904a157d7fc
--->; 5dd2ed73d762
Successfully built 5dd2ed73d762
Successfully tagged nginx:v1
从上面的过程中可以看出,先是下载centos7的镜像,然后在centos7镜像的基础上使用yum安装相关软件,安装完软件,把nginx-1.14.2.tar.gz包添加进镜像,然后开始编译nginx,编译完,把nginx.conf复制进了镜像中,最后是添加进启动nginx的命令。
创建完成后,可以使用docker images进行查看。
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1 5dd2ed73d762 11 minutes ago 400MB
centos 7 67fa590cfc1c 4 weeks ago 202MB
hello-world latest fce289e99eb9 8 months ago 1.84kB
其中nginx(ID:5dd2ed73d762)这个就是我们使用Dockerfile创建出来的新镜像。 由于在创建nginx这个镜像时,引用了centos7这个镜像,所以还多出一个centos(ID: 67fa590cfc1c)的镜像。
现在我们可以运行这个镜像
# docker run --name nginx -p 805:80 -d nginx:v1
–name:给运行的容器取名叫nginx -p:本地端口805映射内部端口80(由于我本地80端口已经被用,所以换成了805) nginx:v1:镜像名称如果不是最新版latest的,记得带上tag,要不然会找不到。
在浏览器打开http://IP地址:805,如果出现以下画面,说明Nginx容器运行成功。