Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the exact container name started by docker-compose.yml

We have a docker-compose.yml file to bring up a db2 and a number of other components of our testing framework. Once we bring up the db2 we have to attach to that container and run a set of shell scripts on it to create schema definitions, etc.

Trouble is that docker-compose gives dynamic names to the containers - and they change everytime we start them with 'run'. How do I find the exact name of the db2 container that's running this time?!

Here is an excerpt from our docker-compose.yml:

  db2:
    container_name: "bluecost-db2"
    image: store/ibmcorp/db2_developer_c:11.1.3.3-x86_64
    command: /bin/bash -c "groupadd bluecost -g 1006;adduser bluecost -u $$(id -u)  -g $$(echo $$(id)| grep -oPm1 'gid=\K\w+')&& echo \"bluecost:bluecost\" | chpasswd;usermod -a -G db2iadm1 bluecost;chown -R bluecost:db2iadm1 /home/bluecost/bluecostDB;mkdir /database/data;"
    hostname: db2server
    privileged: true
    ports:
      - 50001:50000
      - 55000:55000
    networks:
      - back-tier
    volumes:
      - ./standalone/linuxScripts/bluecostDB:/home/bluecost/bluecostDB
    restart: "no"
    env_file:
      - ./standalone/db2/.env_list

Here is a shell script that starts db2 and rest of the commands:

docker-compose -f docker-acceptance.yml run -d db2
printf  "Waiting for db2 to warm-up *************************\n\n\n\n"
sleep 340
printf  "Starting to initialize schema **********************\n\n\n\n"
docker exec -d -t bluecost-db2 /bin/bash -c "cp /home/bluecost`/bluecostDB/*.* /home/db2inst;chmod +x /home/mydir/runBlueCostDBScript.sh;chown db2adm:db2adm /home/db2iadm/*.sql;chown db2adm:db2adm1 /home/db2inst/*.sh;mkdir /database/data/bluecostDB;chown -R db2adm:db2adm1 /database/data/bluecostDB;echo ./runBlueCostDBScript.sh | su - db2adm"` 

Notice above that first line in that script runs the db2. That works, the container starts and it warms up, but when the last docker exec command runs, it refers to that container by name 'bluecost-db2' and that container name that's running is sligtly different: bluecost-db2_run1, bluecostdb2_run2, etc

How can I find the exact name to execute? I found this docker ps --format '{{.Image}} {{.Names}}' but that lists all containers that are running, there may be more than one... how do I get just the name of the db2 container that's running?

like image 965
JamesD Avatar asked Jul 08 '18 23:07

JamesD


People also ask

What is the default container name for docker?

When a container is created using Docker, Docker provides a way to name the container using the the --name <container_name> flag. However, if no name is provided by the user while creating/running a Docker container, Docker automatically assigns the container a name.

Does Docker compose start container?

Using the Compose command line tool you can create and start one or more containers for each dependency with a single command ( docker-compose up ). Together, these features provide a convenient way for developers to get started on a project.


1 Answers

You can use docker-compose ps:

$ docker-compose ps
                Name                               Command                   State                       Ports                
------------------------------------------------------------------------------------------------------------------------------
our-project_airflow-postgres_1  docker-entrypoint.sh postgres    Up (healthy)     5432/tcp                                                                                                                             
our-project_airflow_1           /bin/bash -c source initia ...   Exit 0                                                                       
our-project_healthchecker_1     /bin/sh -c ./compute-healt ...   Up (unhealthy)                                       
our-project_local-ingress_1     docker-entrypoint.sh postgres    Up (healthy)     5432/tcp                            
our-project_local-s3_1          /usr/bin/docker-entrypoint ...   Up (healthy)     9000/tcp  

You can restrict this to a service, too:

$ docker-compose ps local-s3
             Name                           Command                  State               Ports         
-------------------------------------------------------------------------------------------------------
gears-orchestration_local-s3_1   /usr/bin/docker-entrypoint ...   Up (healthy)   9000/tcp

So what you want is something along the lines of this:

$ docker-compose ps local-s3 | tail -n +3 | awk '{ print $1 }'
gears-orchestration_local-s3_1

Be aware that like all docker CLI commands, these will aggressively break lines. A non-interactive shell won't have that problem, but in a terminal emulator you may see your commands fail.

like image 149
Raphael Avatar answered Oct 20 '22 03:10

Raphael