使用 docker 注册表共享我的多图像容器化应用程序:连接多个图像存储库时遇到问题

Using docker registry to share my multi-image containerized app: Encountering Problems connecting multiple image repos

我的本地 docker 容器应用程序由 4 个图像组成,React、Django、nginx、postgres。它在本地运行完美,(当然我需要在本地计算机上手动转到 127.1.1.0 才能查看应用程序)

I want other people to be able to view this app as well, if they have docker installed on their local computer, and so, using the steps in the docker documentation, I pushed all four images to docker hub.

我对文档的其余部分感到困惑,因为当我尝试使用本地 docker 图像名称的示例时,如下所示, docker run -d -p 3000:8000 --restart=always --name myport myport_nginx ,

只拉取了nginx镜像服务,可以单独启动,其余服务均未调用

因此,我的问题是:我如何定义这四个图像,以便它们按照它们在我本地计算机上的方式连接,这样任何拥有本地 docker 的人都可以提取我的存储库并检查我的通过在他的本地计算机上转到 127.0.0.1 来安装应用程序。我知道 dockerbub 与 github 类似,这应该是直截了当的,但不知何故我的头脑把它弄糊涂了,我想澄清一下。

下面是 dockercompose.yml 文件,用于在第一个实例中构建应用程序。我知道可以修改我的 .yaml,以便一个图像可以调用并启动所有其他三个图像,如果可能或好的做法,我希望得到帮助。

version: "3.7"

services:
  django:
    
    build:
      context: ./backend
      dockerfile: Dockerfile 
    volumes:
      - django_static_volume:/usr/src/app/static
    expose:
      - 8000
    env_file:
      - ./backend/.env
    command: gunicorn mainapp.wsgi:application --bind 0.0.0.0:8000
    ports:
       - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./postgres/.env
  react:
    
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        - API_SERVER=${ENV_API_SERVER}   
    volumes:
      - react_static_volume:/usr/src/app/build/static
    expose:
      - 3000
    env_file:
      - .env
    command: serve -s build -l 3000
    depends_on:
      - django

  nginx:
    restart: always
    
    build: ./nginx
    volumes:
      - django_static_volume:/usr/src/app/django_files/static
      - react_static_volume:/usr/src/app/react_files/static
    ports:
      - 80:80
    depends_on:
      - react
      - django

volumes:
  postgres_data:
  django_static_volume:
  react_static_volume:

命令应该是docker-compose updocker run 只会启动一个容器。 Docker Compose up official docs

让我们专注于一项服务。您需要对所有这些进行类似的更改。

  react:
    build:
      context: ./frontend
      dockerfile: Dockerfile

这会在您的 local 系统上构建 frontend 目录。您需要将其替换为指向您的 Docker 中心图像的 image: 行。

      args:
        - API_SERVER=${ENV_API_SERVER}

这会在特定的 URL 中编译为您推送到 Docker Hub 的图像。其他消费者可能无法使用它。如果您的 nginx 反向代理容器已经在同一主机和不同 URL 路径上为 React 应用程序和后端 API 提供服务,您可以使用 path-only URL 喜欢 /api 并避免配置它。

    volumes:
      - react_static_volume:/usr/src/app/build/static

这会将应用程序中的静态文件替换为命名卷的内容。如果您在本地以某种方式修改了它,您的其他消费者将不会拥有它;如果更新映像,命名的卷将保持不变并且映像中的更新文件将不可见。我建议删除这样的命名卷。 (您的 nginx 容器需要 COPY 静态文件到它的图像中。)

    expose:
      - 3000

这没有任何作用,可以安全地删除。

    env_file:
      - .env

.env 文件仅存在于您的本地系统中。您可以分发该文件或将其转换为内联 environment: 变量,或者根据设置的不同,使用 Dockerfile ENV 说明修复它们。

    command: serve -s build -l 3000

这会覆盖图像的 CMD,因此没有必要。

    depends_on:
      - django

这很好。

清理完这一切后,对于这项服务,我希望得到更像这样的服务:

  react:
    image: 2fresh/frontend
    depends_on:
      - django

您可能会发现在 docker-compose.yml 旁边放置一个 docker-compose.override.yml 文件很有帮助。这提供了您只需要进行本地开发的定义,例如如何 build: 图像。这两个文件将被合并,build:image: 一起告诉 Compose 为构建的图像使用什么名称。

# docker-compose.override.yml
version: '3.8'
services:
  react:
    build: ./frontend
    # all other settings in main docker-compose.yml file