如何在前台和后台混合环境中为Docker Compose编写yml文件

huangapple go评论58阅读模式
英文:

How to write yml file for docker compose in a mixed environment of foreground and background

问题

I can somehow handle a docker engine but cannot handle a docker compose.
I have two docker images which are pg13ondeb and pyonol8. One is originally from the postgresql13 image, and the other is from OracleLinux8, which has my Python script to write data into the pg13 database.
I confirmed that two containers, which are started with these images, were able to co-work, meaning they could INSERT INTO SQL. The script is executed manually from the terminal.
The two containers were created with the following docker commands:

docker run --name pg13_1 -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d pg13ondeb
docker run --name client1 -it pyonol8 /bin/bash

(As you can see, the first command uses the -d option, and the second command does not.)

Now, I want to create one pg13 server container and two client Python containers using the docker-compose command. To do so, I wrote the following YAML file and executed it:

version: "3"

services:
  pg13ondeb_1:
    image: pg13ondeb
    ports:
      - "5432:5432"
    networks:
      pg_net_1:
        ipv4_address: 172.20.0.2

  pyonol8_1:
    image: pyonol8
    networks:
      pg_net_1:
        ipv4_address: 172.20.0.11

  pyonol8_2:
    image: pyonol8
    networks:
      pg_net_1:
        ipv4_address: 172.20.0.12

networks:
  pg_net_1:
    name: app_net
    driver: bridge
    ipam:
     driver: default
     config:
       - subnet: 172.20.0.0/24

Then I executed this as follows:

docker compose up -d
[+] Running 4/4
⠿ Network app_net Created 0.0s
⠿ Container compose-pyonol8_1-1 Started 0.7s
⠿ Container compose-pyonol8_2-1 Started 0.8s
⠿ Container compose-pg13ondeb_1-1 Started 0.8s

It seems like it is done. I checked it with docker ps -a, and three containers appeared. However, when I used docker ps, only compose-pg13ondeb_1-1 appeared. This means that the other two clients, which were supposed to be started, are not actually running.

My hunch is that the -d option might have caused this. Does anyone know how to resolve this in the YAML file?

Thank you.

英文:

I can somehow handle a docker engine but cannot handle a docker compose.
I have two docker images which is pg13ondeb and pyonol8. The one is originaly from postgresql13 image and the other is from OracleLinux8 which has my python script to write data into pq13 database.
I confirmed that 2 containers which are started with these images were able to co-work, I mean INSERT INTO SQL. The script is executed manualy from terminal.
The two containers are maked with following docker commands.

docker run --name pg13_1 -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d pg13ondeb
docker run --name client1 -it pyonol8 /bin/bash

(As you see, 1st command has -d option and the 2nd command does not.)

Now, I want to create one pg13server container and 2client python containers by docker compose command. In order to do so, I write a following yml file and performed it.

version: "3"

services:
  pg13ondeb_1:
    image: pg13ondeb
    ports:
      - "5432:5432"
    networks:
      pg_net_1:
        ipv4_address: 172.20.0.2

  pyonol8_1:
    image: pyonol8
    networks:
      pg_net_1:
        ipv4_address: 172.20.0.11

  pyonol8_2:
    image: pyonol8
    networks:
      pg_net_1:
        ipv4_address: 172.20.0.12

networks:
  pg_net_1:
    name: app_net
    driver: bridge
    ipam:
     driver: default
     config:
       - subnet: 172.20.0.0/24

Then I executed this as below.

> docker compose up -d
[+] Running 4/4
⠿ Network app_net Created 0.0s
⠿ Container compose-pyonol8_1-1 Started 0.7s
⠿ Container compose-pyonol8_2-1 Started 0.8s
⠿ Container compose-pg13ondeb_1-1 Started 0.8s

It seems like be done. I checked it with dicker ps -a and three containers were appeared. However, when I did it with docker ps, only compose-pg13ondeb_1-1 is appeared. Which means other two clients which are supposed to be started are not actually started.

My hunch says that the -d oprion resulted this. Does somebody know how to resolve this in yml file?

Thank you,

答案1

得分: 1

  1. 通过 docker ps -a 查找容器,然后使用 docker logs <container> 检查 pyonol8_1-3 容器的日志。如果有错误,请修复它。
  2. 检查您的 pyonol8 入口命令,可能在完成脚本后已经结束并停止了容器。
  3. 添加正确的入口点以完成您的任务。因为容器在容器内的进程完成后会停止。例如,添加一个脚本来将数据写入您的数据库。但是,当您的脚本完成时,您的容器将停止。如果您需要运行容器,您的入口点应该运行持续运行的进程。
英文:
  1. Find containers by docker ps -a and check logs of pyonol8_1-3 containers by docker logs &lt;container&gt;. If there is error fix it.
  2. Check you pyonol8 entrypoint command, may be it finished and conatiner was stoped after finish script.
  3. Add correct entrypoint to complete you task. Because container is stoped when process in container finished. As example add script to write data to your DB. But when your script finished your container will be stoped. If you need running container, your entrypoint should run procces that running all the time.

答案2

得分: 1

根据 services: 标签的提示,Compose 更适用于长时间运行的非交互式容器。如果你同时有长时间运行和临时容器,应该只将长时间运行的容器放入你的 docker-compose.yml 文件中。

version: '3.8'
services:
  pg13ondeb_1:
    image: pg13ondeb
    ports:
      - "5432:5432"

networks:
  default:
    name: app_net  # 默认为 <directoryname>_default

然后,你可以运行连接到该网络的临时容器。

docker run --rm -it --net app_net --name client1 pyonol8 \
  /bin/bash

如果镜像的标准 CMD 实际上有一个长时间运行的进程,而你只是创建了一个带有调试 shell 的临时容器,那么 docker-compose run 将为你提供一个基本基于 Compose 配置的容器,但会忽略 ports: 并运行一些不同的命令。然后,你可以在 Compose 设置中包括这个容器。

version: '3.8'
services:
  pg13ondeb_1:
    image: pg13ondeb
    ports:
      - "5432:5432"

  pyonol8_1:
    image: pyonol8
docker-compose run pyonol8_1 \
  /bin/bash

(在整个过程中,我大大简化了网络配置。Compose 为你提供了一个名为 default 的网络,并且Docker会自动分配IP地址,因此在大多数情况下,你根本不需要在Compose文件中设置 networks:。另请参阅Docker文档中的Compose网络。)

英文:

As hinted by the services: label, Compose is a little more designed around long-running non-interactive containers. If you have a mix of long-running and temporary containers, you should only put the long-running containers in your docker-compose.yml file.

version: &#39;3.8&#39;
services:
  pg13ondeb_1:
    image: pg13ondeb
    ports:
      - &quot;5432:5432&quot;

networks:
  default:
    name: app_net  # defaults to &lt;directoryname&gt;_default

Then you can run your temporary containers connecting to that network explicitly.

docker run --rm -it --net app_net --name client1 pyonol8 \
  /bin/bash

If the image's standard CMD does actually have a long-running process, and you're just creating a second temporary container with a debugging shell, then docker-compose run will give you a container mostly based on Compose configuration, but ignoring ports: and running some different command. You could then include the container in the Compose setup

version: &#39;3.8&#39;
services:
  pg13ondeb_1:
    image: pg13ondeb
    ports:
      - &quot;5432:5432&quot;

  pyonol8_1:
    image: pyonol8
docker-compose run pyonol8_1 \
  /bin/bash

(Throughout this I've significantly simplified the network configuration. Compose provides a network named default for you, and Docker will automatically assign IP addresses, so in most cases you don't need networks: settings in your Compose file at all. Also see Networking in Compose in the Docker documentation.)

huangapple
  • 本文由 发表于 2023年3月7日 15:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定