根据正在运行的容器在docker-compose.yml中创建服务 [英] Create service in docker-compose.yml base on running container

查看:73
本文介绍了根据正在运行的容器在docker-compose.yml中创建服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我内部有一个泊坞窗和多个服务.但是,并非每个正在运行的容器都在 docker-compose.yml 中.例如,这是我的 docker-compose.yml 文件.

I have a docker and multiple services inside. But not every running container is in docker-compose.yml. For example here is my docker-compose.yml file.

version: '3'
services:
  db:
    image: postgres:latest
    environment:
      - POSTGRES_PASSWORD=test
      - POSTGRES_USER=test
      - POSTGRES_DB=test
    volumes:
      - ./db_data:/var/lib/postgresql/data

和运行中的容器

testtesttest        plugin-test:latest          "/entrypoint.sh"         11 months ago        Up 25 hours         0.0.0.0:3002->80/tcp   dockerfiles_plugin-test_1

现在,我想使用此容器将服务添加到 docker-compose.yml 文件.

Now I would like to add service with this container to docker-compose.yml file.

我知道我可以运行 docker inspect testtesttest 来查看详细信息,但是在 docker-compose.yml 文件中重新创建服务的最佳方法是什么?

I know I can run docker inspect testtesttest to see details but what is the best way to recreate a service in docker-compose.yml file?

推荐答案

您在 docker-compose.yml 中定义的每个服务都对应于docker-compose将启动和管理的容器.您不能添加现有容器",到一个撰写文件.

Every service you define in your docker-compose.yml corresponds to a container that docker-compose will start and manage. You can't "add an existing container" to a compose file.

如果您希望您的撰写服务与现有容器进行交互,则可以肯定地做到这一点.最简单的机制是确保撰写容器与现有容器位于同一网络上.例如,假设您有一个名为 mynetwork 的现有网络(使用 docker network create mynetwork 创建),则可以像这样启动您的非组合服务:

If you want your compose services to interact with an existing container, you can certainly do that. The easiest mechanism is to ensure that your compose containers are on the same network as the existing container. For example, assuming you have an existing network named mynetwork (created with docker network create mynetwork), you would start your non-compose service like this:

docker run --network mynetwork ...

您将这样编写撰写文件:

And you would write your compose file like this:

version: '3'
services:
  db:
    image: postgres:latest
    environment:
      - POSTGRES_PASSWORD=test
      - POSTGRES_USER=test
      - POSTGRES_DB=test
    volumes:
      - ./db_data:/var/lib/postgresql/data
    networks:
      mynetwork: {}

networks:
  mynetwork:
    external: true

这将 mynetwork 定义为外部网络-即在Docker compose之外创建的网络.通过将组成容器与现有容器放在同一网络上,它们将能够按名称相互引用.也就是说,如果您现有的容器名为 dockerfiles_plugin-test_1 ,则您的数据库将能够使用主机名 dockerfiles_plugin-test_1 连接到它.

This defines mynetwork as an external network -- that is, one that has been created outside of Docker compose. By putting your compose containers on the same network as your existing container, they will be able to refer to eachother by name. That is, if your existing container is named dockerfiles_plugin-test_1, then your db will be able to connect to it using the hostname dockerfiles_plugin-test_1.

如果您想朝另一个方向运行,并且将 dockerfiles_plugin-test_1 连接到数据库,则可能要为数据库容器设置静态主机名(或别名):

If you want to go in the other direction and have the dockerfiles_plugin-test_1 connect to the database, you probably want to set a static hostname (or alias) for the database container:

version: '3'
services:
  db:
    image: postgres:latest
    environment:
      - POSTGRES_PASSWORD=test
      - POSTGRES_USER=test
      - POSTGRES_DB=test
    volumes:
      - ./db_data:/var/lib/postgresql/data
    networks:
      mynetwork:
        aliases:
          - db

networks:
  mynetwork:
    external: true

这为 db 容器提供了网络 mynetwork 上的主机名 db .

This gives the db container the hostname db on network mynetwork.

这篇关于根据正在运行的容器在docker-compose.yml中创建服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆