docker里安装nginx容器、同时配置多个nginx容器

时间:2024-04-04 16:53:44

本篇文章主要介绍在Linux(Centons7)下安装docker的中安装nginx容器,以下分为大概步骤和详细步骤,也会把一些遇到的坑和解决方案经验分享给大家,nginx容器安装其实就是下载启动即可,配置多个nginx需要额外修改即可

Docker的三大核心概念:镜像、容器、仓库

镜像:类似虚拟机的镜像、用俗话说就是安装文件。

容器:类似一个轻量级的沙箱,容器是从镜像创建应用运行实例,

可以将其启动、开始、停止、删除、而这些容器都是相互隔离、互不可见的。

仓库:类似代码仓库,是Docker集中存放镜像文件的场所。
 

简要步骤:

①、下载nginx容器

②、检查容器、进入容器、查看容器

③、拷贝配置文件配置多个容器

④、创建配置文件、修改配置文件

⑤、修改nginx端口

⑥、启动nginx容器

⑦、访问nginx容器

⑧、建立软连接(由于默认安装的路径内存不够)

⑨、docker常用命令介绍
 

推荐博客:

1、docker官网安装步骤

https://docs.docker.com/install/linux/docker-ce/centos/

2、Docker是什么?可以用Docker做什么?开发者可以使用Docker做什么?个人对Docker的理解?

https://blog.csdn.net/zjh_746140129/article/details/82667491

3、linux安装docker、Centons7下安装docker

https://blog.csdn.net/zjh_746140129/article/details/89285447

 

详细步骤:

一、下载nginx容器

命令解释:80是宿主机端口80是nginx容器ip,这个时候外部访问nginx是用服务器ip+80访问,这个容器名字就是nginx

docker run —name nginx -p 80:80 -d nginx:1.15.8

docker里安装nginx容器、同时配置多个nginx容器

 

 

二、检查容器、进入容器、查看容器

1、检查容器

docker images

docker里安装nginx容器、同时配置多个nginx容器 

2、进入容器

docker exec -it 容器id bin/bash

3、查看启动的docker容器

docker ps

docker里安装nginx容器、同时配置多个nginx容器

 

三、拷贝配置文件配置多个容器

1、进入容器拷贝配置文件

docker exec -it 容器id bin/bash

docker里安装nginx容器、同时配置多个nginx容器 

四、创建配置文件、修改配置文件

1、创建第二个nginx的配置文件修改端口

touch default.conf

vi default.conf 

 docker里安装nginx容器、同时配置多个nginx容器

五、修改nginx端口

创建端口81的容器,使用外部配置文件

81是宿主机端口80是nginx容器ip

这个时候外部访问nginx是用服务器ip+81/80访问,虽然-p后面都是81:80 80:80,但是这2个80在docker里面是相互隔离的

docker run --name nginx-static -p 80:80 -v /home/docker_resource/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf -v /home/nginx/html:/usr/share/nginx/html -d nginx:1.15.8

docker run --name nginx-static -p 81:80 -v /home/docker_resource/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf -v /home/nginx/html:/usr/share/nginx/html -d nginx:1.15.8

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #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;
    #}
}

docker里安装nginx容器、同时配置多个nginx容器
 

六、启动nginx容器

docker里安装nginx容器、同时配置多个nginx容器

 

七、访问nginx容器

docker里安装nginx容器、同时配置多个nginx容器

 

好了,docker里安装nginx容器教程就到这里,如果读者在过程中有问题,评论即可,第一时间回复。