侧边栏壁纸
博主头像
勤奋的冬枣博主等级

只有把抱怨环境的情绪,化为上进的力量,才是成功的保证!

  • 累计撰写 273 篇文章
  • 累计创建 448 个标签
  • 累计收到 116 条评论
标签搜索

目 录CONTENT

文章目录

Docker 使用Dockerfile构建nginx镜像(十四)

勤奋的冬枣
2020-02-20 / 0 评论 / 0 点赞 / 1,493 阅读 / 0 字

上面两篇文章,详细说明了使用dockerfile创建镜像的原理
《Docker Dockerfile指令说明(十二)》
《Docker Dockerfile创建镜像说明(十三)》

本片为实际操作案例。基于centos创建nginx镜像

1. 下载镜像

docker pull centos

2. 创建文件目录

mkdir -p /www/nginx

3. 创建并编辑Dockerfile文件

cd /www/nginx
vim Dockerfile

添加在Dockerfile以下内容

FROM centos:latest                                                    # 选择centos镜像
MAINTAINER by dongzao                                                 # 维护信息人dongzao
RUN yum install -y wget pcre-devel net-tools gcc zlib zlib-devel make openssl-devel   # 安装nginx依赖
RUN useradd -M -u 40 -s /sbin/nologin nginx                           # 创建nginx用户 
RUN wget http://nginx.org/download/nginx-1.16.1.tar.gz                # 下载nginx文件
RUN tar zxvf nginx-1.16.1.tar.gz
WORKDIR nginx-1.16.1                                                  # 设置工作目录
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_realip_module&& make && make install     # 编译安装nginx
RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/                    # 启动Nginx服务
RUN /usr/local/nginx/sbin/nginx
EXPOSE 80                                                             # 开放http端口                   
EXPOSE 443                                                            # 开放https端口
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf              # 关闭守护进程
WORKDIR /opt/nginx                                                    # 创建目录/www/nginx
ADD run.sh /run.sh                                                    # 将宿主机中的run.sh添加到容器中  开机自启
RUN chmod 755 /run.sh                                                 # 开机自启
CMD ["/run.sh"]                                                       # 开机自启

image.png

注意:为什么要关闭守护进程(daemon off)
docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker 容器pid挂了,那么docker容器便会直接退出

Docker未执行自定义的CMD之前,nginx的pid是1,执行到CMD之后,nginx就在后台运行,bash或sh脚本的pid变成了1。如果启动命令单纯是以后台守护(daemon)模式启动nginx,那么启动命令会立即结束,容器也会立即结束。

4. 编辑 run.sh

run.sh作用在于nginx开机自启

vim run.sh
!/bin/bash
/usr/local/nginx/sbin/nginx

image.png

5. 创建镜像

docker build -t mynginx:centos .

不要忘记“.”,这个小点!!!

[root@docker nginx]# docker build -t mynginx .
Sending build context to Docker daemon  4.096kB
Step 1/17 : FROM centos
 ---> 470671670cac
Step 2/17 : MAINTAINER by dongzao
 ---> Running in 2e96eba377dd
Removing intermediate container 2e96eba377dd
 ---> 88a28035b432
Step 3/17 : RUN yum install -y wget pcre-devel net-tools gcc zlib zlib-devel make openssl-devel
 ---> Running in 71db754b44d1
CentOS-8 - AppStream                            2.7 MB/s | 6.4 MB     00:02    
CentOS-8 - Base                                  78 kB/s | 5.0 MB     01:04    
CentOS-8 - Extras                               3.3 kB/s | 2.1 kB     00:00    
Package zlib-1.2.11-10.el8.x86_64 is already installed.
Dependencies resolved.

----省略-----

Installed:
  gcc-8.3.1-4.5.el8.x86_64                                                      
  wget-1.19.5-8.el8_1.1.x86_64                                                  
  make-1:4.2.1-9.el8.x86_64                                                     
  net-tools-2.0-0.51.20160912git.el8.x86_64                                     
  openssl-devel-1:1.1.1c-2.el8.x86_64                                           
  pcre-devel-8.42-4.el8.x86_64                                                  
  zlib-devel-1.2.11-10.el8.x86_64                                               
  cpp-8.3.1-4.5.el8.x86_64                                                      
  isl-0.16.1-6.el8.x86_64                                                       
  libmpc-1.0.2-9.el8.x86_64                                                     
  glibc-devel-2.28-72.el8.x86_64                                                
  glibc-headers-2.28-72.el8.x86_64                                              
  kernel-headers-4.18.0-147.5.1.el8_1.x86_64                                    
  keyutils-libs-devel-1.5.10-6.el8.x86_64                                       
  krb5-devel-1.17-9.el8.x86_64                                                  
  libcom_err-devel-1.44.6-3.el8.x86_64                                          
  libgomp-8.3.1-4.5.el8.x86_64                                                  
  libkadm5-1.17-9.el8.x86_64                                                    
  libpkgconf-1.4.2-1.el8.x86_64                                                 
  libpsl-0.20.2-5.el8.x86_64                                                    
  libselinux-devel-2.9-2.1.el8.x86_64                                           
  libsepol-devel-2.9-1.el8.x86_64                                               
  libverto-devel-0.3.0-5.el8.x86_64                                             
  libxcrypt-devel-4.1.1-4.el8.x86_64                                            
  pcre-cpp-8.42-4.el8.x86_64                                                    
  pcre-utf16-8.42-4.el8.x86_64                                                  
  pcre-utf32-8.42-4.el8.x86_64                                                  
  pcre2-devel-10.32-1.el8.x86_64                                                
  pcre2-utf16-10.32-1.el8.x86_64                                                
  pcre2-utf32-10.32-1.el8.x86_64                                                
  pkgconf-1.4.2-1.el8.x86_64                                                    
  pkgconf-m4-1.4.2-1.el8.noarch                                                 
  pkgconf-pkg-config-1.4.2-1.el8.x86_64                                         
  publicsuffix-list-dafsa-20180723-1.el8.noarch                                 

Complete!
Removing intermediate container 71db754b44d1
 ---> c17212d1b24b
Step 4/17 : RUN useradd -M -u 40 -s /sbin/nologin nginx
 ---> Running in 79767e27e837
Removing intermediate container 79767e27e837
 ---> b713872c355b
Step 5/17 : RUN wget http://nginx.org/download/nginx-1.16.1.tar.gz
 ---> Running in 06d359a6e68e
--2020-02-19 15:25:37--  http://nginx.org/download/nginx-1.16.1.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1032630 (1008K) [application/octet-stream]
Saving to: 'nginx-1.16.1.tar.gz'

----省略-----

Removing intermediate container 643c99457706
 ---> 2f472deb6299
Step 7/17 : WORKDIR nginx-1.16.1
 ---> Running in 3b885d76a564
Removing intermediate container 3b885d76a564
 ---> 96c21a679350
Step 8/17 : RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_realip_module&& make && make install
 ---> Running in 9cb0f3112934
checking for OS
 + Linux 3.10.0-1062.9.1.el7.x86_64 x86_64

----省略-----

creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

make -f objs/Makefile
make[1]: Entering directory '/nginx-1.16.1'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c

----省略-----

cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/ngx_modules.o \
	objs/ngx_modules.c
cc -o objs/nginx \
objs/src/core/nginx.o \
objs/src/core/ngx_log.o \

----省略-----

objs/src/http/modules/ngx_http_stub_status_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
	-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/nginx-1.16.1'
make -f objs/Makefile install
make[1]: Entering directory '/nginx-1.16.1'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'

----省略-----

test -d '/usr/local/nginx/logs' \
	|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/nginx-1.16.1'
Removing intermediate container 9cb0f3112934
 ---> b7454ec1a85d
Step 9/17 : RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
 ---> Running in 97cb999013a3
Removing intermediate container 97cb999013a3
 ---> 2a3b9eb0e1b8
Step 10/17 : RUN /usr/local/nginx/sbin/nginx
 ---> Running in f7f14b1ca65b
Removing intermediate container f7f14b1ca65b
 ---> bc9e0894230a
Step 11/17 : EXPOSE 80
 ---> Running in 75fbe5511c7b
Removing intermediate container 75fbe5511c7b
 ---> f404437c2bf0
Step 12/17 : EXPOSE 443
 ---> Running in dec91ffa66e2
Removing intermediate container dec91ffa66e2
 ---> 078f8188b385
Step 13/17 : RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
 ---> Running in dcbdb33988b8
Removing intermediate container dcbdb33988b8
 ---> 9578bf8f1806
Step 14/17 : WORKDIR /opt/nginx
 ---> Running in 39656b0357a9
Removing intermediate container 39656b0357a9
 ---> a62527b43a94
Step 15/17 : ADD run.sh /run.sh
 ---> e209bf6bbd1b
Step 16/17 : RUN chmod 755 /run.sh
 ---> Running in e338aa79d47f
Removing intermediate container e338aa79d47f
 ---> aef4c73c8b86
Step 17/17 : CMD ["/run.sh"]
 ---> Running in 78f0456548cd
Removing intermediate container 78f0456548cd
 ---> 0b775c9bceaf
Successfully built 0b775c9bceaf
Successfully tagged mynginx:latest

6. 启动容器进行测试

docker run -itd -p 80:80 --name nginx_1 nginx:centos        # 启动容器,-p 指定宿主机80端口映射容器中nginx服务80端口

docker ps      //查看运行的容器  

image.png

7. 进入容器

docker exec -it nginx_1 bash			

image.png
image.png

8. 验证网页

宿主IP地址:192.168.10.148,直接访问即可!

image.png

illust_77713339_20191115_083346_看图王.jpg

0

评论区