1、搜索镜像:docker search nginx
2、下载镜像:docker pull docker.io/nginx
3、运行容器:
docker run \
-p 80:80 \
--name nginx \
--privileged=true \
-v /home/www:/www \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/conf/vhost:/etc/nginx/vhost \
-v /home/wwwlogs:/wwwlogs \
-d nginx
-
-p 80:80:将容器的80端口映射到主机的80端口
-
--name nginx:将容器命名为nginx
-
-v /home/www:/www:将主机中的目录/home/www挂载到容器的/www
-
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将主机中目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf
-
-v /home/nginx/conf/vhost:/etc/nginx/vhost:将主机中目录vhost挂载到容器的/etc/nginx/vhost
-
-v /home/wwwlogs:/wwwlogs:将主机中目录/home/wwwlogs挂载到容器的/wwwlogs
我的两个配置文件:
nginx.conf:
user root;
worker_processes 1;error_log /wwwlogs/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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"';access_log /wwwlogs/access.log main;
sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;#gzip on;
client_max_body_size 100M;
client_body_buffer_size 10M;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;# location / {
# root html;
# index index.html index.htm;
# }
#}
include /etc/nginx/vhost/*.conf;
server_names_hash_bucket_size 512;
}
vhost/default.conf:
server {
listen 80;
server_name www.itoak.cn;
root /www;
}
注意:由于配置文件是从主机中复制到容器中的,所以配置文件中的路径都是对应容器中的路径,明白这点对于程序正确的运行很重要。