学而实习之 不亦乐乎

在 Docker 部署 Nginx 

2024-03-02 19:28:35

实例:在 Docker 容器中部署 Nginx,并通过外部机器访问 Nginx。

一、查找 Nginx 镜像 

# docker search nginx

二、拉取Nginx镜像 

# docker pull nginx

使用docker images命令可以查看当前操作系统中下载了哪些镜像文件。

# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    12766a6745ee   33 hours ago   142MB
... ...

三、创建容器

在 Docker 中部署 Nginx 时,虽然直接就可以访问,但为了方便管理和文件安全,会用到文件映射,以防文件在重启和删除镜像后丢失。

方式一:(使用镜像生成的目录结构)

1. 通过docker run命令启动一个容器,容器名称为 nginx-test 。

# docker run -d --name nginx-test -p  80:80  nginx
81bb1211144bc0991a789f860458548643c60b5459466c14d0604be9a4ccbfd7
  • -d 表示容器在后台运行
  • --name 为容器起一个名字
  • -p 端口映射,格式为:宿主机端口:容器端口,上文中含义是将容器中的端口80映射到宿主机的端口80,对外提供访问服务。最后一个字段为镜像名称

浏览器访问宿主机,若可以正常访问,说明我们的nginx服务已经启动成功了。
通过docker ps可以查看正在运行中的容器,如下所示:

CONTAINER   ID  IMAGE   COMMAND CREATED STATUS  PORTS   NAMES
ta66bb12138cb   nginx   "/docker-entrypoint.…"  11 minutes ago  Up 11 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx-test

2. 复制容器内文件到宿主机对应目录(当然,也可以自己创建,再进行映射,但复制的文件显然出错的可能小一些,主要是一些有语法错误)

# docker cp nginx:/etc/nginx/conf.d /data/nginx/conf/
# docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.conf
# docker cp nginx:/usr/share/nginx/html/index.html /data/nginx/html/index.html

3. 删除已经创建的容器

# docker rm -f nginx

4. 重新创建并启动容器

# docker run --name nginx \
-p 80:80 \
--restart=always \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/conf/cert:/etc/nginx/cert \
-v /data/nginx/ssl:/etc/nginx/ssl/  \
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /data/nginx/log:/var/log/nginx \
-v /data/nginx/html:/usr/share/nginx/html \
-d nginx

5. 测试

添加并编辑 index.html 进行测试。

方式二:(自定义nginx 目录结构)

1. 在/home目录下创建 nginx 目录用于存储 nginx 数据信息

# mkdir /data/nginx
# cd /data/nginx
# mkdir conf
# cd conf

2. 在 /data/nginx/conf/ 下创建 nginx.conf 文件,并编辑配置

# vim nginx.conf

# nginx.conf文件内容
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

3. 在/data/nginx/conf/conf.d/下创建default.conf文件,粘贴下面内容
# vim default.conf

# 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;
    #}
}

3.部署 nginx 

# docker run -itd --name c_nginx -p 8000:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /data/nginx/logs:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx:latest

参数说明:

  • -p 80:80: 将容器的80端口映射到宿主机的80端口。
  • -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf: 将主机当前目录下的 /conf/nginx.conf
  • 挂载到容器的:/etc/nginx/nginx.conf 配置目录
  • -v /data/nginx/logs:/var/log/nginx: 将主机当前目录下的logs目录挂载到容器的/var/log/nginx 日志目录
  • -v /data/nginx/html:/usr/share/nginx/html: 将主机当前目录下的/html挂载到容器的:/usr/share/nginx/html 资源目录

4. 测试

在网站根目录下创建并编辑 index.html 页面文件。保存后在浏览器页面浏览。