安装并定制 Nginx

1、查阅对应的官方文档,首先下载镜像文件:

[spider@izwz9d74k4cznxtxjeeur9z local]$ sudo docker pull nginx
[sudo] password for spider:
Sorry, try again.
[sudo] password for spider:
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
177e7ef0df69: Already exists
ea57c53235df: Pull complete
bbdb1fbd4a86: Pull complete
Digest: sha256:b543f6d0983fbc25b9874e22f4fe257a567111da96fd1d8f1b44315f1236398c
Status: Downloaded newer image for docker.io/nginx:latest

2、定制 Nginx

1、从容器中拷贝一份默认配置

首先在自己的主机上创建目录/etc/docker/nginx用于存放定制 nginx 的配置文件:

[spider@izwz9d74k4cznxtxjeeur9z ~]$ sudo mkdir -p /etc/docker/nginx

接着新运行一个容器,并从其中拷贝出nginx.conf:

[spider@izwz9d74k4cznxtxjeeur9z ~]$ sudo docker run --name tmp-nginx -d nginx
bb807eb140b85570258f834043560b37c16a655821a120273d49cc9369e45067
[spider@izwz9d74k4cznxtxjeeur9z ~]$ sudo docker cp tmp-nginx:/etc/nginx/nginx.conf /etc/docker/nginx/nginx.conf

将临时创建的容器删除:

[spider@izwz9d74k4cznxtxjeeur9z ~]$ sudo docker rm -f tmp-nginx
[sudo] password for spider:
tmp-nginx

2、使用定制的 nginx.conf 启动容器

[spider@izwz9d74k4cznxtxjeeur9z ~]$ sudo docker run --name mynginx -v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
607b0f8096011829dd4cdea89270a7707bea003fac08e7516559713fb838de1e
[spider@izwz9d74k4cznxtxjeeur9z ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
607b0f809601 nginx "nginx -g 'daemon ..." 12 seconds ago Up 11 seconds 80/tcp mynginx

到这一步,就算定制完了,如果我们要修改Nginx 服务器的配置,只需修改我们主机上的nginx.conf,然后重新启动容器就可以了。

05-20 03:42