一、拉取DH上的镜像
docker pull nginx
二、启动Nginx容器
docker run -d --name nginxtest -p 8081:80 nginx
三、安装docker-compose
yum install -y epel-release
yum install -y docker-compose
四、配置宿主机环境
# 创建挂载目录
mkdir -p /opt/nginx/{conf,conf.d,html,logs}
在/opt/nginx/conf/ 目录下创建nginx.conf文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
在/opt/nginx/conf.d/ 目录下创建default.conf 文件
server {
listen 80;
server_name localhost;
# rewrite ^(.*)$ https://www.vhxsl.com permanent;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
在/opt/nginx/html/ 目录下创建index.html
hello
五、配置docker-compose.yml 文件
version: '1'
services:
nginx:
image: nginx:latest
restart: always
container_name: nginx
volumes:
- /opt/nginx/conf/nginx.conf:/etc/nginx/conf/nginx.conf
- /opt/nginx/conf.d:/etc/nginx/conf.d
- /opt/nginx/html:/usr/share/nginx/html
- /opt/nginx/logs/nginx:/var/log/nginx
ports:
- 8081:80
- 查看容器运行状态
docker-compose ps
- 启动、停止容器
docker-compose stop {{Container}}
docker-compose start {{Container}}
- 删除容器
docker-compose rm {{Container}}
运行镜像
docker-compose up -d