13

When we run docker-compose up-d command to run dockers using docker-compose.yml file, it starts building images or pulling images from the registry. We can see each and every step of this command on the terminal.

I am trying to run this command from a python script. The command starts successfully but after the command, I do not have any idea of how much the process has been completed. Is there any way I can monitor the status of docker-compose up -d command so that script can let the user (who is using the script) know how much the process has completed or if the docker-compose command has failed due to some reasons.?

Thanks

CODE:

from pexpect import pxssh

session = pxssh.pxssh()
if not session.login(ip_address,<USERNAME>, <PASSWORD>):
    print("SSH session failed on login")
    print(str(session))
else:
    print("SSH session login successfull")
    session.sendline("sudo docker-compose up -d")
    session.prompt()
    resp = session.before
    print(resp)
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • `-d` is used to run containers in the background. This will not show all of the logs. Can you share your Python Script? – arshovon Feb 14 '18 at 09:31
  • AFAIK you can only observe the logs to check progress (apart from checking if the containers are already up). – shad0w_wa1k3r Feb 14 '18 at 09:45

4 Answers4

7

You can view docker compose logs with following ways

  1. Use docker compose up -d to start all services in detached mode (-d) (you won't see any logs in detached mode)
  2. Use docker compose logs -f -t to attach yourself to the logs of all running services, whereas -f means you follow the log output and the -t option gives you nice timestamps (Docs)

credit

EDIT: Docker Compose is now available as part of the core Docker CLI. docker-compose is still supported for now but most documentation I have seen now refers to docker compose as standard. See https://docs.docker.com/compose/#compose-v2-and-the-new-docker-compose-command for more.

jamesmhaley
  • 44,484
  • 11
  • 36
  • 49
Muhammad Hassan
  • 14,086
  • 7
  • 32
  • 54
  • `docker-compose logs -f -t` is helpful when the `docker-compose up -d` command ran successfully and we need to monitor the logs. What if `docker-compose up -d` showed any error. How to debug in that case.? – S Andrew Feb 14 '18 at 11:48
  • `docker-compose up -d` show error when ran directly on terminal in case it fails. You are running it with python script. How do you running shell commands? – Muhammad Hassan Feb 14 '18 at 12:18
  • I have added the code which I am using. I am using pexpect python module to connect and run ssh commands. The problem is if I run `sudo apt-get install ` command it runs normally and return everything in variable `resp` but when I am running `sudo docker-compose up -d`, it shows me few things only and then exits. – S Andrew Feb 14 '18 at 12:26
4

I think should use the command docker-compose top, could check the result, It shoul not be empty when the container is running. If the containers is stop or exit or Create, it should return empty

张馆长
  • 1,321
  • 10
  • 11
2

What I do to debug small issues is to run:

docker-compose up {service_name} 

This way I get to see the output for an individual service. If the service has a dependency you can always start multiple services like so:

docker-compose up {service_name1} {service_name2}

Additionally I use:

docker-compose logs -f -t {service_name1}

To see the logs of an already running service or alternatively:

docker logs -t -f {container_name}

Notice that the command above needs the container name and not the service name

This way you can make sure service by service that everything works as expected and then you can launch them all in detached mode as suggested in the other answers

lloiacono
  • 4,714
  • 2
  • 30
  • 46
1

If you need a programmatic way with bash, this is the fastest implementation:

  • sleep 2 seconds
  • check the container was up several seconds ago => Means you've just successfully deployed it

docker ps will look like:

a6f088b1567e   lc_fe_isr-app   "docker-entrypoint.s…"   2 seconds ago   Up 2 seconds   0.0.0.0:10001->3000/tcp   lc_fe_isr-app-1
#!/bin/bash

#
# Check if the a single container was started successfully
#
CONTAINER_NAME="lc_fe_isr-app-1"
sleep 2
docker ps | grep $CONTAINER_NAME
UP_SECONDS_AGO=`docker ps | grep $CONTAINER_NAME | grep ' seconds'`
echo $UP_SECONDS_AGO

if [ -n "$UP_SECONDS_AGO" ]
then
    echo "Deploy successfully"
else
    echo "Deploy FAILED"
    exit 1
fi
Neo.Mxn0
  • 953
  • 2
  • 8
  • 25