docker安裝配置nginx

1.使用 docker serach nginx 查找nginx鏡像

[root@localhost ~]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        11254               [OK]
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1582                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   706                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   500                                     [OK]
webdevops/php-nginx                                    Nginx with PHP-FPM                              125                                     [OK]
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   95                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      65                                      [OK]
linuxserver/nginx                                      An Nginx container, brought to you by LinuxS…   58
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                                      [OK]
zabbix/zabbix-web-nginx-pgsql                          Zabbix frontend based on Nginx with PostgreS…   31                                      [OK]
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / ARM                     26                                      [OK]
nginx/nginx-ingress                                    NGINX Ingress Controller for Kubernetes         17
schmunk42/nginx-redirect                               A very simple container to redirect HTTP tra…   14                                      [OK]
nginxdemos/hello                                       NGINX webserver that serves a simple page co…   13                                      [OK]
blacklabelops/nginx                                    Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
wodby/drupal-nginx                                     Nginx for Drupal container image                12                                      [OK]
centos/nginx-18-centos7                                Platform for running nginx 1.8 or building n…   10
centos/nginx-112-centos7                               Platform for running nginx 1.12 or building …   7
nginxinc/nginx-unprivileged                            Unprivileged NGINX Dockerfiles                  4
1science/nginx                                         Nginx Docker images that include Consul Temp…   4                                       [OK]
mailu/nginx                                            Mailu nginx frontend                            3                                       [OK]
travix/nginx                                           NGinx reverse proxy                             2                                       [OK]
toccoag/openshift-nginx                                Nginx reverse proxy for Nice running on same…   1                                       [OK]
ansibleplaybookbundle/nginx-apb                        An APB to deploy NGINX                          0                                       [OK]
wodby/nginx                                            Generic nginx                                   0                                       [OK]

 

2.拉取鏡像命令: docker pull  nginx 獲取之後默認tag爲latest

[root@localhost ~]# docker pull nginx
Using default tag: latest

3.查看獲取的鏡像 docker images nginx

[root@localhost ~]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              881bd08c0b08        6 weeks ago         109MB

4.創建nginx容器

命令:docker run --name nginx_test-d -m 1024M  -p 80:80 nginx

(如果要創建多個tomcat容器 一定要修改name和主機端口)

說明:

       --name 容器名稱

       -d   後臺運行

       -m  設置容器使用內存最大值;

       -p   端口映射,格式爲:主機(宿主)端口:容器端口

[root@JaxWan local]# docker run --name jax-nginx -d -m 1024M  -p 80:80 nginx

5.查看運行狀態 docker ps

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d2cec1acd172        redis:latest        "docker-entrypoint.s…"   3 days ago          Up 6 hours          0.0.0.0:6379->6379/tcp   jax_redis
4068c1c383f1        tomcat:7.0.92       "catalina.sh run"        3 weeks ago         Up 6 hours          0.0.0.0:8455->8080/tcp   checktomcat
6f9726681e83        mysql:5.7.19        "docker-entrypoint.s…"   3 weeks ago         Up 6 hours          0.0.0.0:3306->3306/tcp   mysql
5eb927494115        nginx               "nginx -g 'daemon of…"   5 weeks ago         Up 6 hours          0.0.0.0:80->80/tcp       nginx_test
f42ef6cc1aca        tomcat:7.0.92       "catalina.sh run"        5 weeks ago         Up 6 hours          0.0.0.0:8083->8080/tcp   tomcat3

6. 進入nginx容器修改/etc/nginx/nginx.conf配置

命令:docker exec -it 容器名稱(容器ID) /bin/bash 

進入/etc/nginx目錄下,修改nginx.conf文件

[root@localhost ~]# docker exec -it nginx_test /bin/bash
root@5eb927494115:/# cd etc/nginx/
root@5eb927494115:/etc/nginx# vim nginx.conf 

然後在nginx.conf文件添加以下配置:

server {
        listen 80;
        server_name nginx2tomcat1;
       
    location /test {
      proxy_pass http://192.168.29.24:8082;
    }

    }

附上我的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;
    server {
        listen 80;
        server_name nginx2tomcat1;
   	
	location /test {
	  proxy_pass http://192.168.29.24:8082;
	}

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

7.修改完畢後退出重啓nginx

退出命令(只是退出nginx): exit

root@5eb927494115:/etc/nginx# exit
exit
[root@localhost ~]#

重啓nginx:    docker restart 容器名稱(容器ID)

[root@localhost ~]# docker restart nginx_test

8.瀏覽器查看代理是否成功

首先查看192.168.29.24:8082

然後查看192.168.29.24/test

 

9.可以選擇安裝vim編輯器

安裝教程:https://blog.csdn.net/buyaopingbixiazai/article/details/89330848

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章