简介

本文介绍使用Docker安装Nginx的方法。

需求:使用Docker部署Nginx并成功访问.html静态文件。

实例1(Docker外部配置Nginx)

1.获取Nginx镜像

  1. 搜索镜像
docker search nginx
  1. 可以发现有很多镜像。第一个镜像是官方的,我们选择一个版本:1.23.3
  1. 拉取镜像
docker pull nginx:1.23.3
  1. 查看镜像
docker images
REPOSITORY      TAG           IMAGE ID            CREATED             SIZE
nginx           1.23.3        3964ce7b8458        2 weeks ago         142MB

2.拷贝配置到外部

  1. 创建外部配置目录(放到:/home/docker-config/nginx下边)
mkdir -p /home/docker-config/nginx/
mkdir -p /home/docker-config/nginx/conf.d
mkdir -p /home/docker-config/nginx/logs
mkdir -p /home/docker-config/nginx/share
  1. 创建并启动nginx容器(以生成默认的配置文件)
  1. 创建nginx容器
docker run --name nginx1.23.3 -P -d nginx:1.23.3
  1. 查看容器的id
  1. docker ps
CONTAINER ID  IMAGE         COMMAND                 CREATED       STATUS         PORTS  NAMES
793431c0c200  nginx:1.23.3  "/docker-entrypoint.…"  24 hours ago  Up 29 minutes  xxx    nginx1.23.3
  1. 将内部配置文件拷贝到外部
docker cp 7934:/etc/nginx/nginx.conf /root/docker-config/nginx/nginx.conf
docker cp 7934:/etc/nginx/conf.d/ /root/docker-config/nginx/conf.d/
docker cp 7934:/var/log/nginx/ /root/docker-config/nginx/logs/
docker cp 7934:/usr/share/nginx/html/ /root/docker-config/nginx/share/html

3.修改配置文件

简介

假设:我们想通过10000端口来访问index.html

原来的配置文件(nginx.conf)

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
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;
}

修改之后(nginx.conf)

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;

	server{
			listen 10000;
			server_name _;
			root /usr/share/nginx/html/;
			index index.html;
	}
}

注意

所有的配置路径都是docker内部的路径,不要写:/home/docker-config/xxx这种。我们后边会通过路径映射的方式启动,docker会自动从外部路径获得数据。

4.关闭并删除原先的nginx容器

关闭

docker stop nginx1.23.3

删除

docker rm nginx1.23.3

5.以外部配置启动容器

命令

sudo docker run --name nginx1.23.3 -d -p 10000:10000 \
-v /home/docker-config/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/docker-config/nginx/conf.d:/etc/nginx/conf.d \
-v /home/docker-config/nginx/logs:/var/log/nginx \
-v /home/docker-config/nginx/share/html:/usr/share/nginx/html \
nginx:1.23.3

说明

这样通过创建映射的方式启动,当我们用docker exec -it xxx /bin/bash 进去查看配置时可以发现,它所有配置跟外部配置是一样的。

这里,我猜测,有以下两种可能。

  1. docker先把外部配置拷贝到内部容器,然后启动
  2. docker在我们访问内部配置的时候,实际访问的还是外部的配置,它相当于一个快捷方式一样的一个链接。

6.测试

访问:ip:10000

实例2(Docker内部配置Nginx)

1.获取Nginx镜像

跟上边一样。

2.创建并启动容器

  1. 创建nginx容器
  1. docker run --name nginx1.23.3 -p 10000:10000 -d nginx:1.23.3
  1. 关闭:docker stop nginx1.23.3

3.修改配置文件

简介

假设:我们想通过10000端口来访问index.html

原来的配置文件(nginx.conf)

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
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;
}

修改之后(nginx.conf)

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;

	server{
			listen 10000;
			server_name _;
			root /usr/share/nginx/html/;
			index index.html;
	}
}

4.再次启动

docker start nginx1.23.3