Kye
Kye

Reputation: 4495

Running docker-compose up with Python's 'fabric'

I am using Docker (+ Docker Compose). All docker-compose interaction occurs via the Python 'fabric' package (v1).

Example:

def runserver():
    local('docker-compose up')

and:

$ fab runserver

Everything behaves normally until I attempt to ^C out of a running docker-compose up:

  1. docker-compose appears to receive the ^C (SIGINT?) signal as it starts stopping my containers - e.g.:
Stopping celery-export ... done
Stopping celery        ...

However during the container stopping process (sometimes as long as 10 seconds if a container doesn't respond to signals properly), I can press enter / return and see / interact with my shell (as if the process has ended).

Though at this stage the containers haven't finished stopping yet (there's not a done next to each Stopping ... line). It's as if I've prematurely been given access to my shell, which I can freely use. If a late-finishing container eventually stops (usually after 10 seconds), it'll draw the done line over what I'm currently doing in my terminal.

Example:

Stopping celery-export ... done
Stopping celery        ...
Stopping redis         ...

$ uptime 
10:54  up 1 day, 17:22, 2 users, load averages: 1.73 1.94 1.92
Stopping celery        ... done
Stopping redis         ... done

This does not occur when I call docker-compose up directly (outside of fabric) so I suspect that it's something to do with fabric wrapping the execution of the command.

The expected behaviour being that I cannot access my shell until after the container stopping process finishes.

Forgive my lack of proper terminology for describing this issue, and if this'd be more appropriate on Superuser instead of SO.

Upvotes: 4

Views: 2246

Answers (4)

Kye
Kye

Reputation: 4495

Fabric 1.x has split into two projects, 'Invoke' (inv) is the replacement package for running local commands, and what is relevant here.

Invoke has made drastic improvements in this area, and no longer has this issue. It is relatively easy to migrate from Fabric 1.x to Invoke. I do not see the potential for any fix in Fabric 1.x. My solution was to use Invoke instead.

Upvotes: 0

kichik
kichik

Reputation: 34734

As others have mentioned, using Ctrl+C seems to be killing Fabric in your case. The SSH command still runs in the background and you're getting its output to the console until it finishes.

Instead of doing it this way, I suggest you use docker-compose up -d so Fabric finishes once all the containers are up. If you want to tail the logs, you should add another Fabric command that will execute docker-compose logs. When you Ctrl+C that one, it should end quickly and not run in the background like up. When you want to stop the containers, use docker-compose stop.

Upvotes: 1

user212514
user212514

Reputation: 3130

I use docker-compose up (outside of Fabric) while testing. Often when I ctrl+C not all of the images are terminated before I'm returned to the command prompt. In fact, Redis often stays up and running in the background. Rerunning docker-compose up even results in the display of the Redis messages accumulated since the ctrl+C.

In my case, a suitable workaround to make sure everything is "dead", I use docker kill $(docker ps -q). That kills any running images.

Upvotes: -1

ozlevka
ozlevka

Reputation: 2156

I would like to answer, my answer it's my vision of your problem:

  1. fabric run a command by configuration and stop.
  2. docker-compose up command: Builds, (re)creates, starts, and attaches to containers for a service. When a keyword is attach. When you run:
docker-compose up

Then the last operation is attach to the container (service). For example next docker-compose.yaml:

version: "3.6"


services:
  nginx:
    image: nginx:latest
    ports:
      - "8080:80"

if you run docker-compose up you'll see next output:

Starting fabric_nginx_1 ... done
Attaching to fabric_nginx_1

When Attaching to fabric_nginx_1 is successfully ended attachment to nginx server. Ctrl-C or SIGINT sent to this console will stop nginx process and container (service).

For run process/container/service as a daemon, you should run docker-compose detached and use -d/--detach switch.

docker-compose up -d

Then your service will run as a service. For stop, you will should use:

  1. docker-compose stop or
  2. docker-compose kill or
  3. docker-compose down

Conclusion: In your case, all work as designed. Fabric runs a docker-compose command and stop. docker-compose create containers, run them and stop. The container behavior is by his own content packed into container.

Upvotes: 2

Related Questions