首先需要你的nginx包

一、空目录下新建 dockerfie

mkdir -p /root/Desktop/docker/docker_demo/
cd /root/Desktop/docker/docker_demo/
vim Dockerfile
[root@foundation11 docker_demo]# ls
Dockerfile  nginx-1.14.2.tar.gz

二:编写 Dockerfile 文件,如下:

首先

dockerfile写nginx镜像_nginx

vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://XXXXX.aliyuncs.com"]
}

systemctl daemon-reload
systemctl restart docker
docker pull centos
docker run centos


 

FROM centos
MAINTAINER mhanzaipeng 493096871@qq.com
ADD nginx-1.14.2.tar.gz /usr/local/
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel 
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/nginx-1.14.2
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
EXPOSE 80

 

三:

docker build -f Dockerfile -t mynginx .

值得注意的是,这句代码最后还有一个小点,是指定 dockerfile 在当前目录中。而且镜像名称不可以有大写字符。

Sending build context to Docker daemon  1.019MB
Step 1/9 : FROM centos
 ---> 1e1148e4cc2c
Step 2/9 : MAINTAINER mhanzaipeng 493096871@qq.com
 ---> Using cache
 ---> 63be306ac10d
Step 3/9 : ADD nginx-1.14.2.tar.gz /usr/local/
 ---> Using cache
 ---> 609de1b6fb75
Step 4/9 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
 ---> Using cache
 ---> 64c189836b41
Step 5/9 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
 ---> Using cache
 ---> 77d78e8ef616
Step 6/9 : RUN useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 93e44631d687
Step 7/9 : WORKDIR /usr/local/nginx-1.14.2
 ---> Using cache
 ---> 8a8f1c78c756
Step 8/9 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
 ---> Using cache
 ---> a6fb2f9a1c95
Step 9/9 : EXPOSE 80
 ---> Using cache
 ---> 69e9f07fbbc9
Successfully built 69e9f07fbbc9
Successfully tagged centos_nginx:v1

 

四:

docker build -t centos_nginx:v1 .

docker images

docker run -d centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

docker ps -l

docker run -d -p80:80 centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

[root@foundation11 docker_demo]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
34a12800a81a        centos_nginx:v1     "/usr/local/nginx/sb…"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   gifted_galileo

[root@foundation11 docker_demo]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_nginx        v1                  69e9f07fbbc9        14 hours ago        511MB
mynginx             latest              69e9f07fbbc9        14 hours ago        511MB
<none>              <none>              2c4253c6cad5        14 hours ago        202MB
hello-world         latest              fce289e99eb9        2 months ago        1.84kB
centos              latest              1e1148e4cc2c        3 months ago        202MB
game2048            latest              19299002fdbe        2 years ago         55.5MB

dockerfile写nginx镜像_nginx_02

[root@foundation11 docker_demo]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
34a12800a81a        centos_nginx:v1     "/usr/local/nginx/sb…"   10 minutes ago      Up 10 minutes       0.0.0.0:80->80/tcp   gifted_galileo
[root@foundation11 docker_demo]# docker port  34a12800a81a
80/tcp -> 0.0.0.0:80

 

现在添加环境变量和CMD

FROM centos
MAINTAINER mhanzaipeng 493096871@qq.com
ADD nginx-1.14.2.tar.gz /usr/local/
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/nginx-1.14.2
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
EXPOSE 80
ENV PATH /usr/local/nginx/sbin:$PATH
CMD /bin/sh -c 'nginx -g "daemon off;"'

 

[root@foundation11 docker_demo]# docker build -t centos_nginx:v2 .
Sending build context to Docker daemon  1.019MB
Step 1/11 : FROM centos
 ---> 1e1148e4cc2c
Step 2/11 : MAINTAINER mhanzaipeng 493096871@qq.com
 ---> Using cache
 ---> 63be306ac10d
Step 3/11 : ADD nginx-1.14.2.tar.gz /usr/local/
 ---> Using cache
 ---> 609de1b6fb75
Step 4/11 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
 ---> Using cache
 ---> 64c189836b41
Step 5/11 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
 ---> Using cache
 ---> 77d78e8ef616
Step 6/11 : RUN useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 93e44631d687
Step 7/11 : WORKDIR /usr/local/nginx-1.14.2
 ---> Using cache
 ---> 8a8f1c78c756
Step 8/11 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
 ---> Using cache
 ---> a6fb2f9a1c95
Step 9/11 : EXPOSE 80
 ---> Using cache
 ---> 69e9f07fbbc9
Step 10/11 : ENV PATH /usr/local/nginx/sbin:$PATH
 ---> Running in 1f06181c61ee
Removing intermediate container 1f06181c61ee
 ---> 7e50a802cb7c
Step 11/11 : CMD /bin/sh -c 'nginx -g "daemon off;"'
 ---> Running in 8d0fb83fff98
Removing intermediate container 8d0fb83fff98
 ---> aa2854f27a81
Successfully built aa2854f27a81
Successfully tagged centos_nginx:v2

[root@foundation11 docker_demo]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_nginx        v2                  aa2854f27a81        28 seconds ago      511MB
centos_nginx        v1                  69e9f07fbbc9        14 hours ago        511MB

[root@foundation11 docker_demo]# docker run -d -p82:80 centos_nginx:v2
57b36e5249b597361db841a000cf57564195e8f1815c3c9a8e0a7f24992e915a

[root@foundation11 docker_demo]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
57b36e5249b5        centos_nginx:v2     "/bin/sh -c '/bin/sh…"   15 seconds ago      Up 12 seconds       0.0.0.0:82->80/tcp   sharp_minsky
[root@foundation11 docker_demo]# docker port 57b36e5249b5
80/tcp -> 0.0.0.0:82

dockerfile写nginx镜像_docker_03

 

使用ENTRYPOINT  CMD

后三行改为

ENV PATH /usr/local/nginx/sbin:$PATH
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]

[root@foundation11 docker_demo]# docker build -t centos_nginx:v3 .
Sending build context to Docker daemon  1.019MB
Step 1/12 : FROM centos
 ---> 1e1148e4cc2c
Step 2/12 : MAINTAINER mhanzaipeng 493096871@qq.com
 ---> Using cache
 ---> 63be306ac10d
Step 3/12 : ADD nginx-1.14.2.tar.gz /usr/local/
 ---> Using cache
 ---> 609de1b6fb75
Step 4/12 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
 ---> Using cache
 ---> 64c189836b41
Step 5/12 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
 ---> Using cache
 ---> 77d78e8ef616
Step 6/12 : RUN useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 93e44631d687
Step 7/12 : WORKDIR /usr/local/nginx-1.14.2
 ---> Using cache
 ---> 8a8f1c78c756
Step 8/12 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
 ---> Using cache
 ---> a6fb2f9a1c95
Step 9/12 : EXPOSE 80
 ---> Using cache
 ---> 69e9f07fbbc9
Step 10/12 : ENV PATH /usr/local/nginx/sbin:$PATH
 ---> Using cache
 ---> 7e50a802cb7c
Step 11/12 : ENTRYPOINT ["nginx"]
 ---> Running in 0188e07ffda7
Removing intermediate container 0188e07ffda7
 ---> dff588e6bb15
Step 12/12 : CMD ["-g","daemon off;"]
 ---> Running in 2d30b94af5ab
Removing intermediate container 2d30b94af5ab
 ---> a98a09959ecb
Successfully built a98a09959ecb
Successfully tagged centos_nginx:v3

[root@foundation11 docker_demo]# docker run -d -p83:80 centos_nginx:v3
2ca8764b86b8a0f95b8059ba024f27bb8c25cdea839ee54312a1093c25770d53

[root@foundation11 docker_demo]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
2ca8764b86b8        centos_nginx:v3     "nginx -g 'daemon of…"   15 seconds ago      Up 12 seconds       0.0.0.0:83->80/tcp  

dockerfile写nginx镜像_nginx_04

 

当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;"

而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)

 

CMD的命令修改为了后台

我们知道如果容器内的进程在后台运行那么容器将不会运行

现在利用v5版本镜像启动一个container,但是在启动的时候添加后面的command:

CMD ["-g","daemon on;"]

[root@foundation11 docker_demo]# docker build -t centos_nginx:v4 .
Sending build context to Docker daemon  1.019MB
Step 1/12 : FROM centos
 ---> 1e1148e4cc2c
Step 2/12 : MAINTAINER mhanzaipeng 493096871@qq.com
 ---> Using cache
 ---> 63be306ac10d
Step 3/12 : ADD nginx-1.14.2.tar.gz /usr/local/
 ---> Using cache
 ---> 609de1b6fb75
Step 4/12 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
 ---> Using cache
 ---> 64c189836b41
Step 5/12 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
 ---> Using cache
 ---> 77d78e8ef616
Step 6/12 : RUN useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 93e44631d687
Step 7/12 : WORKDIR /usr/local/nginx-1.14.2
 ---> Using cache
 ---> 8a8f1c78c756
Step 8/12 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
 ---> Using cache
 ---> a6fb2f9a1c95
Step 9/12 : EXPOSE 80
 ---> Using cache
 ---> 69e9f07fbbc9
Step 10/12 : ENV PATH /usr/local/nginx/sbin:$PATH
 ---> Using cache
 ---> 7e50a802cb7c
Step 11/12 : ENTRYPOINT ["nginx"]
 ---> Using cache
 ---> dff588e6bb15
Step 12/12 : CMD ["-g","daemon on;"]
 ---> Running in 5092ba77bbec
Removing intermediate container 5092ba77bbec
 ---> 8639d6fd0c67
Successfully built 8639d6fd0c67
Successfully tagged centos_nginx:v4

[root@foundation11 docker_demo]# docker run -d -p84:80 centos_nginx:v4 -g "daemon off;"
aa9f510c9607699e47a859cac5013f2102e53ed456126f1eaa9e256360161c03

[root@foundation11 docker_demo]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
aa9f510c9607        centos_nginx:v4     "nginx -g 'daemon of…"   11 seconds ago      Up 9 seconds        0.0.0.0:84->80/tcp   adoring_agnesi

dockerfile写nginx镜像_centos_05

可以看见在后面新增了-g "daemon off;",前面说过如果增加了命令那么Dockerfile中的CMD中的命令将不会生效

 

 

添加VOLUME指令

 

FROM centos
MAINTAINER mhanzaipeng 493096871@qq.com
ADD nginx-1.14.2.tar.gz /usr/local/
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
VOLUME ["/data"]
WORKDIR /usr/local/nginx-1.14.2
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
EXPOSE 80
ENV PATH /usr/local/nginx/sbin:$PATH
ENTRYPOINT ["nginx"]
CMD ["-g"]

[root@foundation11 docker_demo]# docker build -t centos_nginx:v5 .
Sending build context to Docker daemon  1.019MB
Step 1/13 : FROM centos
 ---> 1e1148e4cc2c
Step 2/13 : MAINTAINER mhanzaipeng 493096871@qq.com
 ---> Using cache
 ---> 63be306ac10d
Step 3/13 : ADD nginx-1.14.2.tar.gz /usr/local/
 ---> Using cache
 ---> 609de1b6fb75
Step 4/13 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
 ---> Using cache
 ---> 64c189836b41
Step 5/13 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
 ---> Using cache
 ---> 77d78e8ef616
Step 6/13 : RUN useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 93e44631d687
Step 7/13 : VOLUME ["/data"]
 ---> Running in c4c60516082e
Removing intermediate container c4c60516082e
 ---> 6ac8032492ac
Step 8/13 : WORKDIR /usr/local/nginx-1.14.2
 ---> Running in 4d372c400ba5
Removing intermediate container 4d372c400ba5
 ---> 0df7c6e2b708
Step 9/13 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
 ---> Running in 516c70d6267b
checking for OS
 + Linux 3.10.0-957.5.1.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-36) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found

Step 10/13 : EXPOSE 80
 ---> Running in 50cd2b5cd8ef
Removing intermediate container 50cd2b5cd8ef
 ---> 1ff21e7cea29
Step 11/13 : ENV PATH /usr/local/nginx/sbin:$PATH
 ---> Running in 72657569b751
Removing intermediate container 72657569b751
 ---> abc25aa91c53
Step 12/13 : ENTRYPOINT ["nginx"]
 ---> Running in 275603efbbcd
Removing intermediate container 275603efbbcd
 ---> 21188cd91eb7
Step 13/13 : CMD ["-g"]
 ---> Running in 76ba50c8c877
Removing intermediate container 76ba50c8c877
 ---> 7e6d6ef4f8af
Successfully built 7e6d6ef4f8af
Successfully tagged centos_nginx:v5

[root@foundation11 docker_demo]# docker run -d -p 85:80 --name=nginx6 centos_nginx:v5 -g "daemon off;"
14ac210262f173cb46ebb6890a8a2c9767b7ff5422001319c113b935b33fd473

dockerfile写nginx镜像_其他_06

 

[root@foundation11 docker_demo]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
14ac210262f1        centos_nginx:v5     "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:85->80/tcp   nginx6

[root@foundation11 docker_demo]# docker exec -it 14ac210262f1 /bin/bash;cd /

[root@14ac210262f1 /]# ll
total 24
-rw-r--r--   1 root root 12076 Dec  5 01:37 anaconda-post.log
lrwxrwxrwx   1 root root     7 Dec  5 01:36 bin -> usr/bin
drwxr-xr-x   2 root root     6 Mar 13 06:10 data

发现存在卷宗data

这个/data挂载的目录对应本机host的这个目录:

[root@foundation11 docker_demo]# touch /var/lib/docker/volumes/5d8e7504fa30a93a0c849d64024e81a0582b4fe9174b44ac5b03d83e26708e08/_data/1

[root@14ac210262f1 data]# pwd
/data
[root@14ac210262f1 data]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 13 06:20 1

就可以创建文件了

 

添加ONBUILD指令:

Dockerfile修改为

ONBUILD VOLUME ["/data"]

docker build -t centos_nginx:v6 .

docker run -d -p 86:80 --name=nginx6 centos_nginx:v6 -g "daemon off;"

docker exec -it nginx6 /bin/bash

发现并没有data

但是如果将Dockerfile改为

 

FROM centos_nginx:v6
MAINTAINER mhanzaipeng 493096871@qq.com

 

[root@foundation11 docker_demo]# docker build -t centos_nginx:v7 .
Sending build context to Docker daemon  1.018MB
Step 1/2 : FROM centos_nginx:v6
# Executing 1 build trigger
 ---> Using cache
 ---> 598e2c230afe
Step 2/2 : MAINTAINER mhanzaipeng 493096871@qq.com
 ---> Using cache
 ---> b9284362c25c
Successfully built b9284362c25c
Successfully tagged centos_nginx:v7

[root@foundation11 docker_demo]# docker run -d -p 87:80 --name=nginx7 centos_nginx:v7 -g "daemon off;"   
14c435edae7761a370bcd70482c2e15a6e07a671e71cec8e39cf1573e4927930

[root@14c435edae77 /]# ll
total 20
-rw-r--r--   1 root root 12076 Dec  5 01:37 anaconda-post.log
lrwxrwxrwx   1 root root     7 Dec  5 01:36 bin -> usr/bin
drwxr-xr-x   2 root root     6 Mar 13 06:54 data

就会存在data

由此可见镜像v7包含了v6 的所有内容,并且增加了ONBUILD的内容