Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep container alive and linked using docker-compose

I want to use docker-compose to compose together php and several databases (orientdb, neo4j, etc). Then get into the php container and use the shell to execute commands.

Individually, all of my container work swimmingly, and when I compose them together, they all run. However, I cannot for the life of me figure out how to keep the php container alive so I can get into it for testing.

For simplicity, I'll just use a single database: orient-db.

My docker-compose.yml file:

version: '2'
services:
  php:
    build: .
    links:
       - orientdb

  orientdb:
    image: orientdb:latest
    environment:
      ORIENTDB_ROOT_PASSWORD: rootpwd
    ports:
       - "2424:2424"
       - "2480:2480"

My "php" Dockerfile:

FROM php:5.6-cli
ADD . /spider
WORKDIR /spider
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
RUN composer install --prefer-source --no-interaction

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini

I have tried (among other things):

  • docker-compose up in one terminal and then docker attach in another
  • enabling tty and stdin_open in my compose file
  • using a /bin/bash command
  • variations of CMD exec vendor/bin/phpunit -D FOREGROUND

And some references I've tried: - How to Keep Docker Container Running After Starting Services? - https://github.com/docker/compose/issues/1926 - https://github.com/docker/compose/issues/423

Any help would really be appreciated.

Thank you.

like image 463
Apollo Avatar asked May 10 '16 21:05

Apollo


People also ask

How do I keep my docker containers alive?

The simplest way to keep the container running is to pass a command that never ends. We can use never-ending commands in any of the following ways: ENTRYPOINT or CMD directive in the Dockerfile. Overriding ENTRYPOINT or CMD in the docker run command.

How do you stop a container from exiting docker compose?

In order to prevent the docker container from exiting immediately after creation, tty should be set to true in the docker-compose. yml file. tty: true in docker-compose corresponds to the docker run -it . With tty: true for the service, the created by docker-compose up -d container status is Up .

Does docker compose automatically restart container?

Like the restart Docker command, Docker Compose includes the restart property to restart containers automatically.

Should I use docker compose down or stop?

The docker-compose stop command will stop your containers, but it won't remove them. The docker-compose down command will stop your containers, but it also removes the stopped containers as well as any networks that were created. You can take down 1 step further and add the -v flag to remove all volumes too.


2 Answers

Had a similar issue with running a zsh shell on docker container running with compose, it would close with exit code 0 immediately after start.

@Spock's comment below the last answer is actually the key, at least for what I need.

Set your docker-compose command for the image to be:

command: tail -f /dev/null

This keeps the process alive but also allows it to shutdown gracefully.

like image 149
SeedyROM Avatar answered Oct 19 '22 04:10

SeedyROM


Join entrypoint so that the process does not exit

version: "3.7"
services:
    debug-srv:
        container_name: "debug-srv"
        image: "alpine:latest"
        entrypoint: ["tail", "-f", "/dev/null"]
        networks:
            debug-net:
networks:
    debug-net:
        name: "debug-net"
like image 35
lupguo Avatar answered Oct 19 '22 03:10

lupguo