练习使用docker,记录下具体流程。
拉取 nginx 镜像
1 2
| docker pull nginx:alpine
|
第一次启动 nginx 容器
1
| docker run -d --rm --name nginx nginx
|
--rm会在容器停止时删除,--name nginx 命名容器名为 nginx, nginx表示使用 nginx 镜像
拷贝容器的 nginx 目录到宿主机
1 2
| docker cp nginx:/etc/nginx/ $PWD/conf
|
windows 下需要在管理员模式下进行,否则报A required privilege is not held by the client.权限错误
停止 nginx 容器(自动删除)
第二次启动 nginx 容器, 挂载配置文件,容器会使用宿主机的 nginx 配置
1
| docker run -d -p 80:80 --name nginx -v $PWD/conf:/etc/nginx -v D:/project/:/usr/local/web nginx
|
-p 80:80表示把本机的 80 端口映射到容器的 80 端口(local port:docker port),此时可以打开 localhost:80(或 localhost)看到 nginx 欢迎界面;
-v $PWD/conf:/etc/nginx 表示把容器的配置目录/etc/nginx/nginx.conf 映射到宿主机的 $PWD/conf目录
-v D:/project/:/usr/local/web表示把宿主机的D:/project/目录挂载到容器的/usr/local/web目录
修改宿主机conf/nginx.conf配置
- 作为静态服务器(以单页面应用为例),将
D:/project/project1/dist作为根目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| http { ... server { listen 80; server_name t.test.com;
root /usr/local/web/project1/dist; index index.html index.htm; location / { try_files $uri $uri/ index.html; index index.html; } } ... }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| http { ... server { listen 80; server_name t1.test.com; location / { proxy_pass http://10.0.75.1:8000; } } ... }
|
查看容器在宿主机的 ip:
windows: ipconfig 配合 route print
linux: ip addr show docker0
重启 conf/nginx.conf
1
| docker exec -it nginx service nginx reload
|