一、基本流程
1、目录准备
我喜欢把文件放在/data下。
# 进入根目录的data目录
cd /data# 创建nginx目录
mkdir nginx# 进入nginx目录
cd nginx# 创建conf目录
mkdir conf# 进入conf目录
cd conf# 创建conf.d目录
mkdir conf.d
2、文件准备
(1)nginx.conf
文件存放位置:/data/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;
}
(2)default.conf
文件存放位置:/data/nginx/conf/conf.d/
文件内容:
server {listen 80;listen [::]:80;server_name localhost;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}#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 /usr/share/nginx/html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}
}
3、查找镜像
docker search nginx
解释:
docker search 是查找镜像的意思。
nginx 是镜像的名称。默认查找最新版本,如果要指定版本,就是 镜像名称:版本号 这样的格式。如 nginx:3.0.1。
4、拉取镜像
docker pull nginx
解释:
docker pull 是拉取镜像的意思。
5、创建容器
包括端口映射、目录挂载。
# 先来到/data/nginx目录下
cd /data/nginx# 创建容器
docker run -id --name=nginx \
-p 80:80 \
-v $PWD/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
docker run:创建容器
-id:让容器在后台运行,并且输出容器的 ID。
--name=容器名称:指定容器名称。
-p:服务器端口映射到容器端口。前者是服务器端口,后者是容器端口。
-v:目录挂载。容器外的xxx目录/文件挂载到容器内的yyy目录/文件。如 $PWD/conf/conf.d:/etc/conf.d 的意思就是当前目录下的 /conf/conf.d 目录,挂载到容器内的 /etc/conf.d 目录。
nginx:镜像名称,这里镜像就是nginx。有多个重名镜像,可指定版本号来区分。
6、查看运行的容器
docker ps
7、index.html
文件位置:/data/nginx/html/
文件内容:
<h1 style="text-align:center;" >hello!</h1>
<h1 style="text-align:center;">It is under development, please be patient</h1>
8、访问服务器ip或者域名
到这里应该就可以了。
二、问题
1、关于 -id 和 -it
(1)-it
-
-i
是--interactive
的缩写,它的作用是让容器的标准输入保持打开状态,即便没有连接到终端。 -
-t
是--tty
的缩写,此选项会为容器分配一个伪终端。
(2)-id
-
-i
含义与上面相同,即保持容器的标准输入打开。 -
-d
是--detach
的缩写,它会让容器在后台运行,并且输出容器的 ID。
(3)小总结
-it 适用于需要与容器进行交互式操作的场景,例如调试容器、在容器内执行命令等。
-id 适用于让容器在后台持续运行的场景,例如运行 Web 服务器、数据库服务等。
2、关于 docker run 报错
(1)容器名称是否重复
(2)端口是否冲突
(3)基本配置是否存在
3、关于 conf、html、logs
(1)conf:配置目录
(2)html:项目目录
(3)logs:日志目录
4、关于default.conf
不是必须手动配置。如果最初准备目录和文件的时候,只准备了 /data/nginx/conf/nginx.conf ,其实这也可以了,但是这个时候有三个地方需要注意。
nginx.conf 需要做一点修改。
docker run 的时候命令需要做一点修改。
(1)修改文件 nginx.conf
.....http {.....# include /etc/nginx/conf.d/*.conf; 这个不要了,换成下面这个include /etc/conf.d/*.conf;
}
(2)修改命令 docker run
docker run -id --name=nginx \
-p 80:80 \
# -v $PWD/conf/conf.d:/etc/nginx/conf.d \ 这个不要了,换成下面这个
-v $PWD/conf/conf.d:/etc/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
(3)拷贝文件 default.conf
docker exec -it nginx /bin/bashcp /etc/nginx/conf.d/default.conf /etc/conf.d/
意思是:进入容器内部,拷贝 default.conf 目录到 /etc/conf.d/ 目录下。