How to get services' container ids in cluster using docker compose? - docker

I've started multiple container services using docker-compose with following command -
docker-compose up -d --scale cluster-service=3
This starts three container services -
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87b60bd2554c xxx.xxx.xxx.xxx:5000/cluster-service:1.1.1 "java -cp cluster-se…" 7 minutes ago Up 7 minutes 0.0.0.0:9077->47100/tcp, 0.0.0.0:9076->47500/tcp testcluser_cluster-service_1
b631a656f8ae xxx.xxx.xxx.xxx:5000/cluster-service:1.1.1 "java -cp cluster-se…" 7 minutes ago Up 7 minutes 0.0.0.0:9075->47100/tcp, 0.0.0.0:9074->47500/tcp testcluser_cluster-service_2
f3aeb9541fe9 xxx.xxx.xxx.xxx:5000/cluster-service:1.1.1 "java -cp cluster-se…" 7 minutes ago Up 7 minutes 0.0.0.0:9073->47100/tcp, 0.0.0.0:9072->47500/tcp testcluser_cluster-service_3
In Jenkins pipeline, using shell script, I want to get container ids in the cluster. How can I achieve this with shell script?

Utilise awk to filter on the name and so:
docker ps | awk '$NF~/cluster_service/ { print $1 }'
Check to see if the last space separated field ($NF) matches "cluster-service" If it does, print the container ID (field 1)

Actually using -q option in docker ps command, it only prints container ids:
docker ps -q
Or to get container id of some specific name use:
docker ps -q --filter=name=xxx.xxx.xxx.xxx

Related

docker container exist but does not run with container ID

I have created a container locally. Then, I run the following command:
docker ps -a
output is:
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc6f4d50931 airflow "/bin/zsh" 17 hours ago Exited (137) 21 minutes ago xenodochial_mclaren
Then I try to run the container with the following command, it create a new container with same IMAGE but different container ID instead opening the container with this image name which exist.
docker run -p 8080:8080 -it airflow /bin/zsh/
The output of docker images command is:
REPOSITORY TAG IMAGE ID CREATED SIZE
airflow airflow 63e2e36735a6 46 hours ago 704MB
airflow latest 63e2e36735a6 46 hours ago 704MB
docker/getting-started latest 083d7564d904 6 weeks ago 28MB
Why is this creating new containers?
If you run docker run ... you spin out a new cointainer from the image.
The status of your container is Exited as you can check from the docker ps -a output.
If you want to start again the same container, you can try docker start abc6f4d50931.

I don't know how to do the same thing pull, create and start instead of docker run -dti

Using an image from https://hub.docker.com/_/php.
Create and launch a hogehoge-php container with docker run -dti
% docker run -dti --name hogehoge-php php
Then I can see that the hogehoge-php container is up and running.
confirmed:
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c9e7ff1a952 php "docker-php-entrypoi..." 6 seconds ago Up 5 seconds ago hogehoge-php
try with pull, create and start
I read that the docker run is a command that performs docker pull, docker create, and docker start all at once. So I decided to try it out as follows (I didn't do the docker pull because I already have a local php image).
% docker create --name foofoo-php php
c7687c088dda9b71d9380e9ca472afa436ac63785c1d2c195b8a08147f8adec9
% docker start foofoo-php
foofoo-php
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7687c088dda php "docker-php-entrypoi..." 16 seconds ago Exited (0) 3 seconds ago foofoo-php
When I tried as above, I got Exited (0) 3 seconds ago, and the foofoo-php container created by docker create doesn't stand up all the time.
The foofoo-php container made by docker create doesn't stand up(options not available).
% docker start -dti foofoo-php
unknown shorthand flag: 'd' in -dti
See 'docker start --help'.
The i option is available, and I was able to run it, but it doesn't leave the docker running.
% docker start -i foofoo-php
Interactive shell
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7687c088dda php "docker-php-entrypoi..." 3 minutes ago Exited (0) 5 seconds ago foofoo-php
How can I create and start instead of run -dti?
By the way, I tried docker run without -dti
By the way, I tried docker run without -dti.
% docker run --name barbarbar-php php
Interactive shell
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6215c71a13a4 php "docker-php-entrypoi..." 15 seconds ago Exited (0) 15 seconds ago barbar-php
The container was created, but it's not standing up. I thought I should try docker start:
% docker start barbar-php
barbar-php
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6215c71a13a4 php "docker-php-entrypoi…" 33 seconds ago Exited (0) 3 seconds ago barbar-php
but the container doesn't start up.
On the other hand, stop and start a container created by docker run -dti
On the other hand, try to stop and start a container created by docker run -dti, it running up after start.
% docker stop hogehoge-php
hogehoge-php
% docker start hogehoge-php
hogehoge-php
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6215c71a13a4 php "docker-php-entrypoi…" 4 minutes ago Exited (0) About a minute ago barbar-php
c7687c088dda php "docker-php-entrypoi…" 11 minutes ago Exited (0) 8 minutes ago foofoo-php
2c9e7ff1a952 php "docker-php-entrypoi…" 14 minutes ago Up 2 seconds hogehoge-php <----- Here
Behavior Summary
docker run
with dit option
up after a run
up after start
without dit option
not up after a run
not up after a start
docker create
not up after a run
What you are missing is a docker create -t to allocate a pseudo-TTY for you container.
Since the entrypoint for the image you are running is a php shell running at the foreground, the container, when started, figures out that it doesn't have a tty to attach to an exit with 0.
docker pull php
docker create -t --name hogehoge-php php
docker start hogehoge-php

docker, container is not running

docker ps shows instances, but when I try to log in to the instance, it says it's not running?
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaa62ff2df11 monitor_kibana "/usr/local/bin/dumb…" 4 months ago Up 9 days kibana
613dc901f2e1 monitor_elasticsearch-search "/usr/local/bin/dock…" 4 months ago Up 9 days elasticsearch-search
$ docker exec -it eaa62 bash
Error response from daemon: Container eaa62ff2df11547744c5f7cf82cad16bf576820d2a209c4f19f173cca68f5511 is not running
$
Could it be that the container only runs for a very short time? If you use the -a flag in your statement to get only active containers, like so:
docker ps -a
Does it still show up? It could be that it runs and just uses something like ECHO. In that case, because the program run succesfully the container is immediately terminated.
Is this an official image? If so, try to run the container without the -d (for deamon) flag. This should output the run information to terminal and give you some information on what is going on.
sudo docker exec -it eaa62 bash

On OS (CoreOS) restart Docker starts multiple containers

I have my customer Docker container on a CoreOS OS host. I start my container using the docker run command as. follows:
docker run -d --restart always --net=host -p 8080:8080 --log-opt max-size=2mb my_docker_hub_accountname/imagename
And when I reboot the host I always see multiple images being run when I run the following command:
user#coreos-1 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ac46db4a58c 3fa7e73d544d "java -jar /code/kec…" 7 days ago Up Less than a second lucid_aryabhata
da5392c136e9 7d996239c21c "java -jar /code/kec…" 7 days ago Up Less than a second awesome_jackson
15bcc4cfe26b 7d996239c21c "java -jar /code/kec…" 4 weeks ago Up Less than a second fervent_colden
f050f55bea3c 7d996239c21c "java -jar /code/kec…" 4 weeks ago Up Less than a second condescending_poincare
31e00707ddff 7d996239c21c "java -jar /code/kec…" 4 months ago Up Less than a second awesome_curran
user#coreos-1 ~
Is there a way to control so that I only have 1 image running instead of multiple images by the Docker container?
I was able to resolve this in one way.
I searched for all container using command:
docker ps -a
I had to remove all the other containers that were previously run using
docker rm $container_id
Now I only 1 docker image running in a container. I reboot and I only see 1 container running.
hope it helps another docker user.

When should I use Docker's container name?

When I ran docker ps -a, I got
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3be2faeb751 centos:latest touch /var/log/test 2 minutes ago Exited (1) 2 minutes ago insane_kirch6
What is the name, insane_kirch6, for?
You can name your own containers with --name when you use docker run. If you do not provide a name, Docker will generate a random one like the one you have.
Check their documentation for naming at Legacy container links, The importance of naming
And even more importantly, you can run named containers again later with start:
docker start --interactive named-containter
Not only for visibility, but it also can be used as container_id,
in docker commands like start, stop, exec, rm, ...
When you want to run a command in an existing container (running or exited), you will identify the container either by name or container_id.
Examples:
Create a container named qqqq and start a process "sleep" 1 minute, and then exit.
$ docker run --name qqqq ubuntu sleep 60
Run another command in the container qqqq:
$ docker exec qqqq ps -aef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 04:21 ? 00:00:00 sleep 60
root 11 0 3 04:21 ? 00:00:00 ps -aef
Delete the container qqqq:
$ docker rm qqqq
qqqq

Resources