一、概述
bitnami镜像
https://registry.hub.docker.com/r/bitnami/nginx
准备
目前docker-compose创建nginx脚本不支持直接挂载文件,只能挂载文件夹,通常我们需要挂载nginx.conf文件,所以必须先在宿主机上生成同名文件。
# 先用docker创建nginx,复制对应文件
mkdir -p nginx/conf nginx/conf.d && cd nginx
docker run --name nginx-demo -d nginx
# 复制文件
docker cp nginx-demo:/etc/nginx/nginx.conf ./conf/nginx.conf
docker cp nginx-demo:/etc/nginx/conf.d/default.conf ./conf.d/default.conf
docker cp nginx-demo:/usr/share/nginx/html .
# 删除容器
docker stop nginx-demo && docker rm nginx-demo
否则报错:
[root@VM_80_127_linux nginx]# docker-compose up -d
Starting nginx ... error
ERROR: for nginx Cannot start service nginx: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/opt/software/docker/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount/opt/software/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via/proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type
二、docker-compose
vim docker-compose.yml
version: '3.1'
services:
nginx:
image: nginx
container_name: nginx
environment:
- TZ=Asia/Shanghai
ports:
- 11086:11086
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./logs:/var/log/nginx
- ./html:/etc/nginx/html
三、基础命令
# 启动
docker-compose up -d
# 列表
docker-compose ps
# 日志
docker-compose logs nginx
# 停止
docker-compose down