- 拉取nginx docker镜像
sudo docker pull nginx
- 创建以下挂载目录及文件
用户目录下:conf html logs
conf: conf.d nginx.conf
html: index.html
conf.d: default.conf
- nginx.conf添加文件内容
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;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
- default.conf添加如下内容
server {
listen 80;
listen [::]:80;
server_name localhost;
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;
}
}
- index.html添加内容
echo "Hello, Jason!" > ~/nginx/html/index.html
- 启动nginx服务,–network选项用于指定容器在启动时所连接的网络。Docker 提供了多种网络模式,每种模式适用于不同的场景。Docker 容器默认使用 桥接模式(Bridge Network)。桥接模式是 Docker 的默认网络模式,它会在宿主机上创建一个名为 docker0 的虚拟网桥,容器通过这个网桥与外部通信。指定HOST容器与宿主机共享同一个网络栈,容器直接使用宿主机的网络接口和 IP 地址。
sudo docker run -d --name nginx --network host -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v ~/nginx/conf/conf.d:/etc/nginx/conf.d -v ~/nginx/logs:/var/log/nginx -v ~/nginx/html:/usr/share/nginx/html nginx
- 查看服务启动状态
sudo docker ps