本文基于腾讯云云服务器CVM系统工具配置文章的基础上,在腾讯云云服务器(CentOS系统)上基于镜像文件Dockerfile制作 Nginx 镜像。
本教程的示例代码: nginx-1.21.6-image
Nginx config
nginx.conf
Docker Nginx镜像里使用的 Nginx 配置:
nginxConfig/nginx.conf1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #user nobody; worker_processes 2;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server { listen 80; server_name localhost;
#charset koi8-r; #access_log logs/host.access.log main;
location / { root /opt/app/nginx; index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html;
location = /50x.html { root html; } } }
|
Nginx 静态页面
Nginx镜像构建的时候,将 dist 目录下面的静态文件 COPY 进 Nginx 服务的根目录(/opt/app/nginx)里。

Nginx 安装包
Nginx 官网
在官网里下载 Nginx 安装包

Nginx 安装包放置 packages 目录下面。
解压 packages/nginx-1.21.6.tar.gz 安装包。
1
| tar -zxvf nginx-1.21.6.tar.gz
|
ENTRYPOINT 启动脚本
ENTRYPOINT 启动脚本放置 scripts 目录下面。
scripts/run.sh
scripts/run.sh1 2 3 4 5 6 7
| #!/bin/bash
/usr/local/services/nginx/sbin/nginx
exec "$@"
|
Dockerfile
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
FROM centos:centos7.9.2009
WORKDIR /opt/app
LABEL maintainer="luqiangzeng@gmail.com"
ENV NGINX_PACKAGE_PATH /usr/local/services/nginx-1.21.6 ENV NGINX_PATH /usr/local/services/nginx
COPY ./packages/nginx-1.21.6 ${NGINX_PACKAGE_PATH}
COPY ./scripts/ /opt/app/scripts/
ADD ./dist/index.html ./nginx/index.html
RUN useradd -M -s /sbin/nologin nginx && \ yum install -y gcc gcc-c++ openssl openssl-devel make libssl-dev libpcre3 libpcre3-dev pcre-devel && \ cd ${NGINX_PACKAGE_PATH} && \ ./configure --prefix=/usr/local/services/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module && \ make && make install
COPY ./nginxConfig/nginx.conf ${NGINX_PATH}/conf/nginx.conf
ENTRYPOINT ["/opt/app/scripts/run.sh"]
|
当前目录结构:

构建镜像
备注: 在Dockerfile路径下执行docker build
1
| docker build -t nginx-demo .
|



运行镜像
1
| docker run --name nginx-demo -p 80:80 -d nginx-demo /usr/sbin/init
|
检查运行的容器:
1 2
| docker ps -a curl http://localhost
|

进入容器镜像终端:
1
| docker exec -it nginx-demo /bin/sh
|

Docker Hub
将镜像推送到 Docker Hub 上。
创建好 Docker Hub 账号

在终端登录 Docker Hub 账号

打 tag
1 2 3
| docker image tag nginx-demo:latest xxxxxxx/nginx-demo:1.0.0 docker image tag nginx-demo:latest xxxxxxx/nginx-demo:latest
|

推送到 Docker Hub
1 2 3 4
| docker push luqiangzeng/nginx-demo:1.0.0
docker push luqiangzeng/nginx-demo:latest
|



归档存储
1
| docker save luqiangzeng/nginx-demo:1.0.0 | gzip > ./nginx-demo.1.0.0.tgz
|

载入本地归档存储的镜像文件镜像
1
| docker load -i ./nginx-demo.1.0.0.tgz
|

运行镜像:
1
| docker run --name nginx-demo -p 80:80 -d luqiangzeng/nginx-demo:1.0.0 /usr/sbin/init
|

停止运行容器container:
1 2
| docker stop <CONTAINER ID> docker stop 49029f581e06
|
删除运行容器container:
1 2
| docker rm -f <CONTAINER ID> docker rm -f 49029f581e06
|

通过 VS Code 端口转发在本地 mac 机器访问


Docker Compose
Docker Compose安装
1、将容器中Nginx根目录(/opt/app/nginx) 挂载至本机的目录(/home/coder/app/nginx-demo/html)上:
1 2
| mkdir -p /home/coder/app/nginx-demo/html
|

2、在本机创建 ./html/index.html 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| cat > ./html/index.html <<EOF # 开始 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1>test html/index.html</h1> </body> </html> EOF
|

3、docker-compose.yml 的配置:
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| version: "2" networks: default: driver: bridge
services: nginx-demo: restart: always image: luqiangzeng/nginx-demo:1.0.0 container_name: nginx-demo volumes: - /home/coder/app/nginx-demo/html:/opt/app/nginx ports: - "80:80" command: /usr/sbin/init
|
4、执行 docker-compose up 命令来启动并运行整个应用程序:
1 2 3 4 5 6
| docker-compose up
docker-compose up -d
|

5、相关指令
1 2 3 4 5 6 7 8 9 10 11
| docker-compose logs -f nginx-demo
docker-compose exec nginx-demo bash
docker-compose down
docker-compose up -d --no-deps --build nginx-demo
|
进入容器container终端,查看 Nginx 根目录文件: docker-compose exec nginx-demo bash
