If I run docker-compose up, and then after a few seconds I open another terminal window to the same directory and run it again, will I get two separate instances of the container?
Or will the second one attach to the already-running container from the first one?
I can post my docker-compose.yml and Dockerfile if needed.
You can run multiple instances of service in docker-compose via the docker-compose up --scale SERVICE=NUM command documented here.
For example if you have a docker-compose.yml file with 2 services defined, named nginx and mysql and you want to run 3 instances of the nginx container, you would run the following command:
docker-compose up --scale nginx=3
Related
Let's say I have a complex container that I start with the following command:
docker-compose --env-file ./src/.env -f docker-compose.yml -f docker-compose.dev.yml up --build
As you can see, I use a custom .env file and override my docker-compose.yml file with another one. What happens in Docker-Desktop when I restart my container? I ask because those flags don't seem to be reused when I restart my container, at least for my .env file.
The environment variables of your env file are added to the container at creation time (docker compose up). Afterwards, they're assigned to your container and you can inspect them with the docker inspect command. They are not removed at container restart, however they will be lost when you recreate the container without passing the env file again.
I’m starting up a few containers with a docker-compose.yml file. I would like all of them to start in detached mode except for one, which i would like to run attached.
I can pass the detach flag on the command line with
docker-compose up -d
But I wonder if it’s possible to specify this in the yml file?
You could try docker-compose up -d service_name for the dettached ones and just docker-compose up service_name for the other.
Not possible to declare in the compose file as far as I know.
I'm running an application under development with docker-compose.
I have a "web" service running a python Flask web application. This service depends on other ones (database, cache, ...).
I need to run the "web" main service interactively in order to get access to a debugger (ipdb).
I found out that the way to do this would be
docker-compose run --name my-app.web --service-ports web
When I exit this container and try to run it again with the same command I got this error:
ERROR: Cannot create container for service web: Conflict. The container name "/my-app.web" is already in use by container "4fed84779bb02952dedb8493a65bd83b1a6664f066183233e8c8b4dc62291643". You have to remove (or rename) that container to be able to reuse that name.
How can I start again this container without creating a new one ?
Or is it the correct way to create new containers each time I need to start this application ?
Or did I miss something to be able to start one of the service interactively ?
As you're setting a custom name, docker-compose run doesn't remove the container once the execution is completed. To enable this behavior use the option --rm:
docker-compose run --rm --name my-app.web --service-ports web
You can also remove the container manually to be able to run it again:
docker rm my-app.web
This is not necessary if you don't set a custom name.
I have 4 containers that I run using a docker-compose file. one of these containers "orchestrates" the other containers work and therefore calls the other containers for doing some tasks.
My problem is that the output of this "orchestrater" container is always displayed once all the other containers have finished and therefore have finished displayed their own outputs.
For the sake of example, this is how the workflow of the containers can look like:
orchestrater container
Container 2
orchestrater container
Container 3
orchestrater container
Container 4
orchestrater container
Is there a way to enforce that the outputs are displayed in sequence ?
When you run docker-compose in attached mod (without -d flag that run containers in detached mode), you will get output from all containers in real time, and there is no way to range outputs.
Possible solution can be to disable all logs from containers 2,3,4 with settings in docker-compose file:
logging:
driver: none
In this case you will get outputs only for orchestrater container.
Other way to split this containers to different docker-compose files. One will be with orchestrater container and second with other containers. You can run docker-compose with defining files:
docker-compose up -f orchestrater_docker_compose.yml
docker-compose up -f services_docker_compose.yml
Currently, I use Docker Compose to start multiple containers in one shot. I have containers started and running already, but while doing docker-compose up -d, I just want to exclude some containers while taking other containers up or down.
Use the following to exclude specific services:
docker-compose up --scale <service name>=0
Think you have to go the "other way". You can start single containers from your docker-compose.yml via:
docker-compose up -d --no-deps ServiceName
If you're looking to exclude some containers because they are not related to the Composition you might be interested in dobi.
dobi lets you define the images and containers (run resources) used to build and run your application. It also has a compose resource for starting the Compose project.
So using dobi you would only put the containers you want to run together into the docker-compose.yml, and the other containers would be just in the dobi.yml.