1、查找nginx镜像
[root@VM_0_4_centos ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 8157 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1296 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 530 [OK]
2、拉取镜像
[root@VM_0_4_centos ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
Digest: sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
3、查看镜像列表
[root@VM_0_4_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 73acd1f0cfad 5 days ago 108.6 MB
docker.io/mysql latest 5195076672a7 5 days ago 371.4 MB
4、创建nginx挂载文件
cd /usr/local/software
mkdir nginx
cd nginx
mkdir www logs conf
cd conf
vi nginx.conf
下面是nginx.conf的配置文件
#user nginx;
worker_processes 1;
#error_log /usr/local/nginx/logs/error.log crit;
#pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by thisprocess.
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
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"';
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
# proxy_temp_path /dataCache/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为5GB。
# proxy_cache_path /dataCache/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;
server {
listen 80;
server_name *.*.*.*;
charset utf-8;
# location ^~ /images/ {
# alias /images/;
#}
location ^~ /imgs/ {
alias /files/imgs/;
}
location ^~ /docs/ {
alias /files/docs/;
}
location / {
proxy_pass http://*.*.*.*:8761;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
}
}
5、启动容器
docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d nginx
6、查看容器是否启动
[root@VM_0_4_centos conf]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bc48fb9b696 nginx "nginx -g 'daemon off" 17 minutes ago Exited (1) 14 minutes ago mynginx
7、发现容器状态是Exited,打开日志查看
[root@VM_0_4_centos log]# docker logs -f 1bc48fb9b696
2018/03/19 16:13:13 [emerg] 1#1: mkdir() "/dataCache/proxy_temp_dir" failed (2: No such file or directory)
nginx: [emerg] mkdir() "/dataCache/proxy_temp_dir" failed (2: No such file or directory)
2018/03/19 16:16:40 [emerg] 1#1: mkdir() "/dataCache/proxy_temp_dir" failed (2: No such file or directory)
nginx: [emerg] mkdir() "/dataCache/proxy_temp_dir" failed (2: No such file or directory)
2018/03/19 16:31:01 [emerg] 1#1: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
nginx: [emerg] open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
8、根据日志报错解决问题
vi /usr/local/software/nginx/conf/nginx.conf (上面贴出的配置文件代码,已经是修改后的)
9、重启容器(访问页面成功)
[root@VM_0_4_centos conf]# docker start mynginx
mynginx
[root@VM_0_4_centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bc48fb9b696 nginx "nginx -g 'daemon off" 48 minutes ago Up 28 minutes 0.0.0.0:80->80/tcp mynginx