1. 程式人生 > >Docker 安裝並定製 Nginx 伺服器

Docker 安裝並定製 Nginx 伺服器

安裝並定製 Nginx

1、查閱對應的官方文件,首先下載映象檔案:

[[email protected] 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 的配置檔案:

[[email protected] ~]$ sudo mkdir -p /etc/docker/nginx

接著新執行一個容器,並從其中拷貝出nginx.conf:

[[email protected] ~]$ sudo docker run --name tmp-nginx -d nginx
bb807eb140b85570258f834043560b37c16a655821a120273d49cc9369e45067
[[email protected]
~]$ sudo docker cp tmp-nginx:/etc/nginx/nginx.conf /etc/docker/nginx/nginx.conf

將臨時建立的容器刪除:

[[email protected] ~]$ sudo docker rm -f tmp-nginx
[sudo] password for spider: 
tmp-nginx

2、使用定製的 nginx.conf 啟動容器

[[email protected] ~]$ sudo docker run --name mynginx -v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
607b0f8096011829dd4cdea89270a7707bea003fac08e7516559713fb838de1e
[
[email protected]
~]$ 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,然後重新啟動容器就可以了。