I'm using docker compose version 2 and I'm trying to create containers and attach them to a specific custom network. My ultimate goal is to create bundles of applications where each bundle will have a different suffix at the container name and network name. Every container with the same suffix will attach to the network with the same suffix.
I'm creating and running the containers with docker-compose up. The first time everything works great. The second time, now using a different suffix so the container names are different, instead of creating and running the new containes, docker compose will recreate the old containers (that had different names). Everything else is the same, except for the name which I thought would be enough. I thought of using run instead of up but the depends_on is not working and the container_name is not working either.
https://github.com/docker/compose/issues/1347
Versions:
Docker version 1.13.0-rc2, build 1f9b3ef
docker-compose version 1.9.0, build 2585387
well i have done the similar kind of thing but i have used project name insisted of suffix.
you can specify project_name(or prefix) by -p option in docker-compose
docker-compose -p project1 up
It will create all container with project1_container_name_count and network with project1_network_name
now u can launch multiple container in same network (like different container using same DB) and they will be separated by suffix_count
like project1_app1 project1_app2
Related
I have several loose containers, including for example a rabbitmq container, that I am using for development. I have started migrating all of these loose containers to a docker-compose file for easier management and spinning up/down when testing. Unfortunately, there is a lot of configuration in these containers that I would rather not have to spend time setting up again.
As such, I was wondering if it was possible to adopt a docker container into a new docker-compose file.
I tried starting the compose file with just using the same name as the loose container, but I get cannot create container for service rabbitmq: Conflict. The container name "/rabbitmq" is already in use by container "<ID>". You have to remove (or rename) that container to be able to reuse that name
If you expect that when you run "docker compose" it will not start a new container if there is one already running with that name and it will just reuse that, it doesn't work that way.
You should define any configurations that you used when running the stand-alone container in your docker-compose file.
I have already created containers running in my server.
They were created through the command line with docker run.
I believe using docker-compose will result in a more portable containers (I don't need to docker run all containers in a different server, only put the same file).
Is there a way to translate the already created containers to a docker-compose file?
In their website (https://docs.docker.com/compose/gettingstarted/), it is mentioned to use docker-compose migrate_to_labels, but it seems the command is not working in the latest version of docker-compose
I have .net , sql database, kafka docker images and I used docker compose yml file to run them together.
I noticed the command down and up does not create fresh environment.
docker compose docker-compose -f dc-all-sql.yml down
then:
docker-compose -f dc-all-sql.yml up
I managed to have fresh environment by using docker desktop 'rest factory setting' option only.
Is my understanding of these command wrong?
Basically I want to have fresh environment, when I up the system, new docker images downloaded.
By default, the only things removed by down are:
Containers for services defined in the Compose file
Networks defined in the networks section of the Compose file
The default network, if one is used
so the images ar not removed , you could use this along with down:
Options:
--rmi type Remove images. Type must be one of:
'all': Remove all images used by any service.
'local': Remove only images that don't have a
custom tag set by the `image` field.
see this
I already have a running container for both postgres and redis in use for various things. However, I started those from the command line months ago. Now I'm trying to install a new application and the recipe for this involves writing out a docker compose file which includes both postgres and redis as services.
Can the compose file be modified in such a way as to specify the already-running containers? Postgres already does a fine job of siloing any of the data, and I can't imagine that it would be a problem to reuse the running redis.
Should I even reuse them? It occurs to me that I could run multiple containers for both, and I'm not sure there would be any disadvantage to that (other than a cluttered docker ps output).
When I set container_name to the names of the existing containers, I get what I assume is a rather typical error of:
cb7cb3e78dc50b527f71b71b7842e1a1c". You have to remove (or rename) that container to be able to reuse that name.
Followed by a few that compain that the ports are already in use (5432, 6579, etc).
Other answers here on Stackoverflow suggest that if I had originally invoked these services from another compose file with the exact same details, I could do so here as well and it would reuse them. But the command I used to start them was somehow never written to my bash_history, so I'm not even sure of the details (other than name, ports, and restart always).
Are you looking for docker-compose's external_links keyword?
external_links allows you reuse already running containers.
According to docker-compose specification:
This keyword links to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services. external_links follow semantics similar to the legacy option links when specifying both the container name and the link alias (CONTAINER:ALIAS).
And here's the syntax:
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
You can give name for your container. If there is no container with the given name, then it is the first time to run the image. If the named container is found, restart the container.
In this way, you can reuse the container. Here is my sample script.
containerName="IamContainer"
if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerName}\$"; then
docker restart ${containerName}
else
docker run --name ${containerName} -d hello-world
fi
You probably don't want to keep using a container that you don't know how to create. However, the good news is that you should be able to figure out how you can create your container again by inspecting it with the command
$ docker container inspect ID
This will display all settings, the docker-compose specific ones will be under Config.Labels. For container reuse across projects, you'd be interested in the values of com.docker.compose.project and com.docker.compose.service, so that you can pass them to docker-compose --project-name and use them as the service's name in your docker-compose.yaml.
I have a docker composition with 3 contaiers. I would like to keep these 3 containers and recreate another instance of the composition, without reusing, nor deleteing the previous 3 containers. Conceptually, would be like running 2 intances of the same application, where the application is the composition.
Docker compose always tries to reuse the existing containers.
You can specify a different project for your compose file.
docker-compose -p namespace2 up -d
By default, the project name is the name of the directory where your docker-compose is. You could also create a new dir and copy your compose file into it. It's the same as specifying -p. It basically specifies the namespace for the objects in your compose file.