使用docker compose一次性部署所有容器

我们通过docker部署了nginx,mongo,parse-server和parse-dashboard,但如果每次都需要这么一个个的部署,略微有点麻烦,这时候,我们的docker compose上场了。

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

至于如何编写docker compose的yaml文件,具体的请参考官方文档 docker compose docs

这边给出最终我们一次性配置的yaml文件内容

services:
  nginx:
    image: nginx
    container_name: nginx
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./nginx/conf.crt:/etc/nginx/conf.crt:ro
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./logs/nginx:/var/log/nginx
      - ./nginx/html:/usr/share/nginx/html
    ports:
      - "80:80"
      - "443:443"
    links:
      - parse-server
      - parse-dashboard      
    networks:
      - internal-network
  mongo:
    image: mongo
    container_name: mongo
    volumes:
      - ./mongo/data/db:/data/db
      - ./mongo/config/mongod.conf:/etc/mongo/mongod.conf
      - ./mongo/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
      - ./logs/mongo:/var/log/mongodb
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=mongoadmin
      - MONGO_INITDB_ROOT_PASSWORD=mongoadminpassword
      - MONGO_INITDB_DATABASE=parse_server_database
    networks:
      - internal-network
    command: --config /etc/mongo/mongod.conf
  parse-server:
    image: parseplatform/parse-server
    container_name: parse-server
    volumes:
      - ./parse-server/config:/parse-server/config
      - ./parse-server/cloud:/parse-server/cloud
      - ./logs/parse-server:/parse-server/logs
    ports:
      - "1337:1337"  
    depends_on:
      - mongo
    command: /parse-server/config/config.json
    networks:
      - internal-network  
  parse-dashboard:
    image: parseplatform/parse-dashboard:4.1.4
    container_name: parse-dashboard
    volumes:
      - ./parse-dashboard/config/parse-dashboard-config.json:/src/Parse-Dashboard/parse-dashboard-config.json
    ports:
      - "4040:4040"  
    depends_on:
      - parse-server
    command: --allowInsecureHTTP --mountPath /parse-dashboard
    networks:
      - internal-network
networks:
  internal-network:
nginx的配置文件的修正

通过Https访问Parse Server和Parse Dashboard文章中,在配置反向代理时,我们使用的是parse-server和parse-dashboard容器的内部IP地址,这就有不确定性,每次容器重启IP都有可能变化,因此需要实时的去修改nginx的配置文件并重启。
使用docker compose,则可以避免这个问题,此时,我们可以使用服务名直接进行访问。

server {
    listen       80;
    server_name  localhost; # 换成自己的host
    
    # http请求重定向到https上
    rewrite  ^(.*)$  https://${server_name}$1  permanent;
    #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;
    #}
    #eeee
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

# 增加https部分设置
server {
   listen  443 ssl;
   server_name  localhost; # 换成自己的host

   ssl_certificate  /etc/nginx/conf.crt/localhost.crt;             #指定证书位置(通过目录挂载到容器内部)
   ssl_certificate_key  /etc/nginx/conf.crt/localhost.key;         #指定私钥位置(通过目录挂载到容器内部)

   location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location /parse/ {
        proxy_pass http://parse-server:1337/parse/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
    }

    location /parse-dashboard/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://parse-dashboard:4040/parse-dashboard/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
}

你可能感兴趣的:(使用docker compose一次性部署所有容器)