本次初步探讨下Docker镜像的使用,以此为示例,方便后续操作
- 安装Docker
此处省略。
- 下载Nginx镜像
docker pull nginx
- 执行Nginx镜像
映射目录
mkdir -p /test/nginx/logs
mkdir -p /opt/nginx/logs
test配置
docker run -p 8000:80 --name nginx-test -d nginx:latest
查看配置
docker exec nginx-test ls /etc/nginx
复制配置
docker cp -a nginx-test:/etc/nginx/ /test/nginx/
docker cp -a nginx-test:/usr/share/nginx/html/ /test/nginx/
docker cp -a nginx-test:/etc/nginx/ /opt/nginx/
docker cp -a nginx-test:/usr/share/nginx/html/ /opt/nginx/
删除test
docker stop nginx-test
docker rm nginx-test
新建容器
docker run -p 8000:80 --restart always --name nginx-test -v /opt/nginx/html/:/usr/share/nginx/html/ -v /opt/nginx/nginx/:/etc/nginx/ -v /opt/nginx/logs:/var/log/nginx -d nginx:latest
参数解释:
--name:容器名称
-d:表示在终端下面继续运行
-p:表示此处用到了端口映射
Docker宿主端口:8000
Docker镜像端口:80
Docker镜像名称:nginx
--restart=always 无论退出状态是如何都重启容器
=no 容器退出时不重启容器
=on-failure:10 允许重启的最大次数
此时通过Docker宿主端口可正常实现访问。
容器测试
docker exec -it nginx-test /bin/bash
成功进去,则视为操作成功
代理配置
cd /opt/nginx/nginx
默认配置
vi conf.d/default.conf
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; #} }
通过以上配置可知:
a:默认端口80,路径指向了/usr/share/nginx/html,对应的映射目录为/opt/nginx/www
b:配置文件中的目录路径应为容器内真实的路径
端口配置
在前面的口令中主机使用的是8000端口,因此为了主机能访问容器内代理断点,应在宿主机中开放8000端口
firewall-cmd --zone=public--add-port=8000/tcp --permanent
systemctl restart firewalld
删除测试
rm -rf /test
代理配置
省略
主要事项
1.test配置主要是为了copy容器示例里面,默认配置文件;
2.代理配置生效文件位于映射的主机目录里面
至此,配置结束,可正常使用