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 anothertty
and stdin_open
in my compose file/bin/bash
commandCMD 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.
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.
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 .
Like the restart Docker command, Docker Compose includes the restart property to restart containers automatically.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With