本文基于腾讯云云服务器CVM系统工具配置文章的基础上,在腾讯云云服务器(CentOS系统)上基于镜像文件Dockerfile制作 Nginx 镜像。
本教程的示例代码: nginx-1.21.6-image
Nginx config
nginx.conf
Docker Nginx镜像里使用的 Nginx 配置:
代码语言:conf nginxConfig/nginx.conf复制#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
安装包。
tar -zxvf nginx-1.21.6.tar.gz
ENTRYPOINT 启动脚本
ENTRYPOINT 启动脚本放置 scripts
目录下面。
scripts/run.sh
代码语言:sh scripts/run.sh复制#!/bin/bash
# 启动 nginx 服务
/usr/local/services/nginx/sbin/nginx
# run the command given as arguments from CMD
exec "$@"
Dockerfile
Dockerfile
代码语言:shell复制# Nginx 服务镜像
# - https://hub.docker.com/_/centos?tab=tags&page=1
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"]
当前目录结构:
构建镜像
代码语言:shell复制备注: 在Dockerfile路径下执行docker build
docker build -t nginx-demo .
代码语言:shell复制docker images
运行镜像
代码语言:shell复制docker run --name nginx-demo -p 80:80 -d nginx-demo /usr/sbin/init
检查运行的容器:
代码语言:shell复制docker ps -a
curl http://localhost
进入容器镜像终端:
代码语言:shell复制docker exec -it nginx-demo /bin/sh
Docker Hub
将镜像推送到 Docker Hub 上。
创建好 Docker Hub 账号
在终端登录 Docker Hub 账号
代码语言:shell复制docker login
打 tag
代码语言:shell复制# 注意: xxxxxxx 改为自己的 Docker Hub 账号
docker image tag nginx-demo:latest xxxxxxx/nginx-demo:1.0.0
docker image tag nginx-demo:latest xxxxxxx/nginx-demo:latest
推送到 Docker Hub
代码语言:shell复制# 推送 1.0.0 版本镜像
docker push luqiangzeng/nginx-demo:1.0.0
# 推送 latest 版本镜像
docker push luqiangzeng/nginx-demo:latest
归档存储
代码语言:shell复制docker save luqiangzeng/nginx-demo:1.0.0 | gzip > ./nginx-demo.1.0.0.tgz
载入本地归档存储的镜像文件镜像
代码语言:shell复制docker load -i ./nginx-demo.1.0.0.tgz
运行镜像:
代码语言:shell复制docker run --name nginx-demo -p 80:80 -d luqiangzeng/nginx-demo:1.0.0 /usr/sbin/init
停止运行容器container:
代码语言:shell复制docker stop <CONTAINER ID>
docker stop 49029f581e06
删除运行容器container:
代码语言:shell复制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
)上:
# 创建 /home/coder/app/nginx-demo/html 文件夹
mkdir -p /home/coder/app/nginx-demo/html
2、在本机创建 ./html/index.html
文件:
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
代码语言:yaml复制version: "2"
networks:
default:
driver: bridge
services:
nginx-demo:
restart: always
# 镜像
image: luqiangzeng/nginx-demo:1.0.0
container_name: nginx-demo
# 将容器中Nginx根目录(`/opt/app/nginx`) 挂载至本机的目录上
volumes:
- /home/coder/app/nginx-demo/html:/opt/app/nginx
ports:
- "80:80"
# 若开启 https 可以映射443端口
# - "443:443"
command: /usr/sbin/init
4、执行 docker-compose up 命令来启动并运行整个应用程序:
代码语言:shell复制# https://docs.docker.com/engine/reference/commandline/compose_up/
docker-compose up
# 如果你想在后台执行该服务可以加上 -d 参数:
# Detached mode: Run containers in the background
docker-compose up -d
<!-- ![](https://my-data-1253513412.cos.ap-guangzhou.myzijiebao.com/docs/2021/0212/WX20220212-154602@2x.png) -->
5、相关指令
代码语言:shell复制# 查看 docker 日志
docker-compose logs -f nginx-demo
# 进入容器container终端
docker-compose exec nginx-demo bash
# 停止 nginx-demo 容器container
docker-compose down
# 重建和更新容器
docker-compose up -d --no-deps --build nginx-demo
进入容器container终端,查看 Nginx 根目录文件: docker-compose exec nginx-demo bash