使用 Docker-Compose 进行多容器应用程序管理

发布日期:2026-07-02 12:39:02   来源 : 杭州电子商务研究院    浏览量 :18
杭州电子商务研究院 发布日期:2026-07-02 12:39:02  
18

介绍

使用 Docker 对应用程序进行容器化是 DevOps 社区中一种快速发展的做法。随着应用程序变得越来越复杂,管理它们需要更多的技能和稳定性。

一种简单的管理模式是关注点分离。系统设计者可以创建独立但相互依赖的逻辑层。使用 Docker,可以将应用程序分离到多个容器中。

本指南将探讨多容器应用程序以及如何使用Docker Compose来管理这些容器的运行。

假设您至少具有 Django 应用和 Docker 的初级知识。可在此处找到介绍 Docker 的指南。

Docker 组成

Docker Compose 是一款用于定义和运行任何类型的多容器应用程序的工具。首先,需要安装 Docker Compose。要安装,请运行以下命令

      pip install docker-compose
    

Docker Compose 指令以 YAML 格式编写,并托管在通常名为docker-compose.yml的文件中。常见的 Docker Compose 命令包括:

  • docker-compose up --build为docker-compose.yml文件中定义的每个服务构建镜像,并按照预定义的顺序运行容器。

  • docker-compose down --volumes删除由你的撰写脚本构建的所有容器以及任何存储卷。

  • docker-compose ps列出所有当前活动的容器。

  • docker-compose stop停止当前活动容器的运行。

要进一步探索更多 Docker Compose 命令,请遵循本指南

示例脚本

对于此示例脚本,假设您正在为 Django 微服务应用程序设计一个 Docker Compose 文件,该文件将部署到生产环境中并使用 Docker 进行管理。

设置涉及三个容器:运行 Django 应用程序的应用程序容器、运行数据库的 Postgres 容器以及为应用程序提供服务的NGINX容器。

还有静态存储卷,用于存储数据和静态文件。由于有多个容器在运行,因此它们应该有一个启动顺序。Docker Compose 使用depends_on指令解决了这个问题。该指令通知Docker Compose哪些容器应该在其他容器执行之前开始运行。

在下面的示例脚本中,我们把之前讨论过的常见 Docker Compose 指令付诸实践。

      version: '3.7'

services:
  nginx: # service name
    build: ./nginx # location of the dockerfile to build the image for this service
    ports: # host:container- Bind the container port(80) to the host port(1339) Any traffic from the host via port 1339 will go to the docker container via port 80 
      - 1339:80 
    volumes: # define location where static files will live
      - static_volume:/home/app/microservice/static
    depends_on:
      - web # web should be up and running for nginx to start
    restart: "on-failure" # restart nginx container if it fails
  web:
    build: . #build the image for the web service from the dockerfile in parent directory
    # issue commands to the application n the container
    command: sh -c "python manage.py makemigrations &&
                    python manage.py migrate &&
                    python manage.py collectstatic &&
                    gunicorn django_microservice.wsgi:application --bind 0.0.0.0:${APP_PORT}"
    volumes:
      - .:/microservice:rw # map data and files from parent directory in host to microservice directory in docker containe
      - static_volume:/home/app/microservice/static
    env_file: # set the location and name of the env file to use when building the containers
      - .env
    image: django_microservice # image name

    expose:
      - ${APP_PORT} # internally expose the given port to other containers within the docker network
    restart: "on-failure"
    depends_on:
      - db # web will only start if db is up and running
  db: # service name
    image: postgres:11-alpine # base image from dockerhub
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
      - postgres_data:/var/lib/postgresql/data/ # define where the postgres data will live within the postgres container
    environment: # set environment variables from the .env file set using the env_file directive
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
      - PGPORT=${DB_PORT}
      - POSTGRES_USER=${POSTGRES_USER}
    restart: "on-failure" # restart db service if it fails


volumes:
  postgres_data: # setup storage volume for data held by the Postgres db
  static_volume: # setup storage volume for static files such as css files and images.
    

结论

本指南中涵盖的技能对于任何组织中的 DevOps 和全栈开发人员职位都至关重要。Docker 生态系统中多容器单应用管理之上的下一个级别是使用Docker SwarmKubernetes等技术进行多应用或微服务管理。

这些被称为容器编排工具。它们用于管理大型应用程序的生命周期和运行,这些应用程序被设计为几个单独的容器化组件,称为微服务。要了解有关如何使用容器编排工具管理基于微服务的应用程序的更多信息,请遵循本指南

以上内容来自杭州电子商务研究院推送
关注
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据