Docker容器构建Nginx服务

关于Docker的简介安装可以参考:Docker-容器简介及安装-云野 (yunyeblog.com)

一.拉取Nginx镜像

1. 在Docker中拉取Nginx镜像

[root@server ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
af107e978371: Pull complete 
336ba1f05c3e: Pull complete 
8c37d2ff6efa: Pull complete 
51d6357098de: Pull complete 
782f1ecce57d: Pull complete 
5e99d351b073: Pull complete 
7b73345df136: Pull complete 
Digest: sha256:2bdc49f2f8ae8d8dc50ed00f2ee56d00385c6f8bc8a8b320d0a294d9e3b49026
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

2. 查看拉取的镜像

[root@server ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d453dd892d93 2 months ago 187MB

二.在容器运行Nginx服务

[root@server ~]# docker run -d -p 80:80 --name nginx nginx
2e0d56cdafafd91a0263696c1ccdc03703f70273ef9d9620132c8e75a36aefb8
[root@server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e0d56cdafaf nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx

浏览器访问测试http://IP:80

三.  在宿主机创建对应的Nginx映射文件目录

[root@server ~]# mkdir /root/nginx/conf/   ##存放nginx的相关配置文件
[root@server ~]# mkdir /root/nginx/log/   ##存放nginx的相关日志文件
[root@server ~]# mkdir /root/nginx/www/  ##存放nginx相关静态资源文件

四. 复制容器里面Nginx的相关文件到上面创建的映射文件目录里,然后进行修改

[root@server nginx]# docker ps
CONTAINER ID IMAGE COMMAND                CREATED        STATUS       PORTS                             NAMES
2e0d56cdafaf nginx "/docker-entrypoint.…" 25 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@server nginx]# docker cp 2e0d56cdafaf:/etc/nginx/nginx.conf /root/nginx/conf 
Successfully copied 2.56kB to /root/nginx/conf
[root@server nginx]# docker cp 2e0d56cdafaf:/etc/nginx/conf.d/ /root/nginx/conf/conf.d 
Successfully copied 3.58kB to /root/nginx/conf/conf.d
[root@server nginx]# docker cp 2e0d56cdafaf:/etc/nginx/nginx.conf /root/nginx/nginx.conf
Successfully copied 2.56kB to /root/nginx/nginx.conf
[root@server nginx]# docker cp 2e0d56cdafaf:/usr/share/nginx/html /root/nginx/www/
Successfully copied 4.1kB to /root/nginx/www/

##2e0d56cdafaf是Docker运行Nginx时的容器ID,也可以换成运行容器时创建的容器名称nginx

修改复制出来的nginx配置文件
[root@server ~]# vim nginx/www/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!_9001</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>当前nginx服务来源于Docker</em></p>
</body>
</html>

五. 删除上面创建的nginx容器, 指定挂载修改后的Nginx配置文件来重新创建Nginx

##删除容器
[root@server ~]# docker stop nginx
romantic_wilson
[root@server ~]# docker rm nginx
romantic_wilson

##指定挂载修改后的Nginx配置文件,重新创建Nginx容器
[root@server www]# docker run \
> -p 9001:80 \
> -v /root/nginx/conf/nginx.conf/:/etc/nginx/nginx.conf \
> -v /root/nginx/conf/conf.d/:/etc/nginx/conf.d \
> -v /root/nginx/log/:/var/log/nginx \
> -v /root/nginx/www/html/:/usr/share/nginx/html \
> -d --restart=always \
> nginx:latest
dd16a6c3070e775111d1d8466c108741c670910316c7e8aca9a4cc5b288b6545

–name nginx         启动容器的名字
-d                  后台运行
-p 9001:80          端口映射
-v                  指定挂载宿主机的Nginx配置文件
nginx:latest        拉取的Nginx镜像

六. 浏览器访问测试IP:9001

七. 访问后发现,虽然访问成功,但是有中文乱码的问题

解决办法:修改Ngnix映射的配置文件nginx.conf。将nginx.conf中server层和访问路径location设置utf-8编码格式。

[root@server ~]# vim nginx/conf/conf.d/default.conf
1 server {
2     listen 80;
3     listen [::]:80;
4     server_name localhost;
5 
6     charset utf-8;   ##设置utf-8编码格式
7 
8     #access_log /var/log/nginx/host.access.log main;
9 
10    location / {
11        root /usr/share/nginx/html;
12        index index.html index.htm;
13        charset utf-8;  ##设置utf-8编码格式
14    }
15 ........

清除缓存重新访问IP:9001

部署思路:

拉取Nginx镜像==>运行Nginx容器==>创建映射目录==>复制Nginx容器里面的Nginx配置文件到创建的映射目录里面==>修改复制出来的Nginx配置文件==>停用删除原来创建的容器==>指定挂载原来修改后的配置文件,重新创建容器==>浏览器测试访问==>出现中文乱码==>修改指定的配置文件,编码方式改为utf-8==>删除缓存,重新访问测试==>访问成功

 

 

 

 


云野 » Docker容器构建Nginx服务

发表回复