when doing docker compose, is it possible to keep previous docker composition - docker

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.

Related

What is the purpose of prepending the folder name to container name in docker compose

I have my docker compose file in /documents folder and when I use docker-compose up, it appends the folder name to the service name (myappv1).
documents_myappv1_1
What is the purpose of this?
Why can't it be just myappv1_1
Docker Compose uses project name to let users deploy multiple isolated environments on a single host. Project name will use the name of the folder containing your docker-compose.yml by default.
You can change the container naming behavior by:
Using -p option such as docker-compose -p project_name up which will cause containers to be named like project_name_myappv1_1
Use container_name option to specify a custom container name which will remain the same even on different deployments
As per doc:
Compose uses a project name to isolate environments from each other.
You can make use of this project name in several different contexts:
on a dev host, to create multiple copies of a single environment, such as when you want to run a stable copy for each feature branch of a project
on a CI server, to keep builds from interfering with each other, you can set the project name to a unique build number
on a shared host or dev host, to prevent different projects, which may use the same service names, from interfering with each other
The default project name is the basename of the project directory. You can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.
Docker Compose uses the default naming scheme <project>_<service>_<index>
to isolate environments, so you can run multiple instances of the same compose-file on one host.
It is explained in the Docker Compose Docs.
But you can override the container name, as explained also in the Compose docs
container_name: my-web-container

docker compose command down and up does not create fresh image and container

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

How can I reuse a Docker container as a service?

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.

docker compose version 2 recreates container when it should not

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

Define Docker container volume bindings in a configuration file?

Is there a way to define all the volume bindings either in the Dockerfile or another configuration file that I am not aware of?
Since volume bindings are used when you create a container, you can't define them in the Dockerfile (which is used to build your Docker image, not the container).
If you want a way to define the volume bindings without having to type them every time, you have the following options:
Create a script that runs the docker command and includes all of the volume options.
If you want to run more than one container, you can also use Docker Compose and define the volume bindings in the docker-compose.yaml file: https://docs.docker.com/compose/compose-file/#/volumes-volumedriver
Out of the two, I prefer Docker Compose, since it includes lots of other cool functionality, e.g. allowing you to define the port bindings, having links between containers, etc. You can do all of that in a script as well, but as soon as you use more than one container at a time for the same application (e.g. a web server container talking to a database container), Docker Compose makes a lot of sense, since you have the configuration in one place, and you can start/stop all of your containers with one single command.

Resources