目录
- 一、步骤
- 二、示例
一、步骤
- 1.搜索nginx镜像
- 2.拉取nginx镜像
- 3.创建容器
- 4.测试nginx
二、示例
- 1.搜索nginx镜像
docker search nginx
- 2.拉取nginx镜像
docker pull nginx
- 3.创建容器,设置端口映射、目录映射
# 在root目录下创建nginx目录用于存储nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf/下创建nginx.conf文件,粘贴下面内容
vim 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;
}
- 4.创建容器
切回nginx目录执行
docker run -id --name=my_nginx \
-p 80:80 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
参数说明
-p 80:80:将容器的80端口映射到宿主机的80端口
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:将主机当前目录下的/conf/nginx.conf挂载到容器的/etc/nginx/nginx.conf。配置的目录
-v $PWD/logs:/var/log/nginx:将主机当前目录下的logs目录挂载到容器的/var/log/nginx。日志的目录
- 5.外部访问
进入html目录,创建index.html
cd ~/nginx/html
vim index.html
hello docker nginx