skip to Main Content

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,

2

Answers


    1. Find containers by docker ps -a and check logs of pyonol8_1-3 containers by docker logs <container>. 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.
    Login or Signup to reply.
  1. 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: '3.8'
    services:
      pg13ondeb_1:
        image: pg13ondeb
        ports:
          - "5432:5432"
    
    networks:
      default:
        name: app_net  # defaults to <directoryname>_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: '3.8'
    services:
      pg13ondeb_1:
        image: pg13ondeb
        ports:
          - "5432:5432"
    
      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.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search