docker-compose.yml file structure and name - docker

I'm using Docker Compose V3 in a project, however when I run
docker compose up -d
in the Docker client app is displayed the name of the folder inside which it's located the docker-compose.yml file...
For this reason, I've looked for something like a container_name but for the whole project, not only for a single container, and I'm unable to find it
Furthermore, if not looking at the examples, there is no reference to a "general structure" of the docker-compose.yml file, something like a DTD for XML
I know that there is no way to create that DTD because at some point, keys and values depends on the containers, but at least for the first 3/4 layer of depth maybe there is a way or it's reported somewhere (somewhere that I'm not able to find)
So my question is, is there an alternative for container_name for the whole docker-compose.yml? Are there any DTD for the docker-compose.yml?

Docker works with services (containers) therefore Docker engine generate a unique ID and a more-readably name for every containers. In default case the 1st part of the container name is the directory name where is the docker-compose.yml file found and the second part is the service name which you defined in the docker-compose.yml file.
As Lucas Bazetto wrote you can override the 1st part of the containers name with the COMPOSE_PROJECT_NAME=example_container_name_prefix option in the .env file.

I solved a similar issue by
docker compose -p {{MY_STACK_NAME}} up -d
to override what it takes from the yaml folder name.
Let me know if it works for you too.
Regards.

Related

Does it matter if the docker compose file is named docker-compose.yml or compose.yml?

Looking at examples for docker compose files I found some named "compose.y(a)ml" instead of "docker compose.yml" or "docker-compose.yml" that I've seen. Does it matter if it's "compose." or "docker composer"?
Awesome Compose repo
I've tried to search for an answer online and tried reading on the docker documentation here but didn't figure it out.
https://docs.docker.com/compose/reference/
Thank you
If the file is named exactly docker-compose.yml, running docker-compose with no other options will find it. Otherwise, you must pass a docker-compose -f option with the alternate name, for every docker-compose command.
Style-wise, I'd recommend putting a file named exactly docker-compose.yml (and possibly a matching docker-compose.override.yml with developer-oriented settings) in your repository's root directory. If you're building a custom image, also put the Dockerfile (again, with exactly that name and capitalization) in the repository root directory.

Can I up or stop resources referring to a specific "docker-compose.yml" individually?

I'm new to the world of containers and specially when it comes to Docker Compose. I'm confused about some concepts and I can't find information about them.
Basically I want to know if I can handle settings in different "docker-compose.yml" files in a isolated manner. I explain better... I would like to know if I can up or stop resources referring to a specific "docker-compose.yml" individually.
PLUS:
To better explain my doubt I'll show you some conjectures about what I'm trying to explain.
It seems to me that it is possible to have multiple configurations for Docker Compose using different ".yml" files like the example below...
EXAMPLE
docker-compose -f docker-compose.a.yml -f docker-compose.b.yml up -d
... and that I can also handle each of these settings individually, such as stopping all the resources referring to a specific docker-compose.yml...
EXAMPLE
docker-compose -f docker-compose.b.yml stop
[Ref(s).: https://gabrieltanner.org/blog/docker-compose#using-multiple-docker-compose-files , https://stackoverflow.com/q/29835905/3223785 , https://stackoverflow.com/questions/42287426/docker-multiple-environments , https://runnable.com/docker/advanced-docker-compose-configuration ]
Yes, it is possible. I'm not exactly sure what you are trying to do, but to be able to manage the services using -f option the way that you described, there shouldn't be a service with the same name on multiple files.
For example, if you have a service called db in docker-compose.a.yml and one other db service in docker-compose.b.yml. The following command will only built one container for db service:
docker-compose -f docker-compose.a.yml -f docker-compose.b.yml up -d
Take a look at -p option. It will make a project with the services isolated inside it. Then you can manage them using following commands with the same docker-compose.yml file:
docker-compose -p foo up -d
docker-compose -p foo stop [service_name]
yes you can.
It is just a matter of preference but i usually create a folder for every project i have, each of them have a unique docker-compose.yml file in it with all its dependencies (frontend / database /redis)
Then to start a specific project i just go inside its folder and run docker-compose up. it then only starts this project without touching others.
you can also type this if you only want to start redis.
docker-compose up redis
All docker-compose subcommands (up, stop, down...) must be executed consuming a docker-compose<.SOME_OPT_VAL>.yml file.
This docker-compose.yml file must be in the folder where the docker-compose command is executed or must be informed via the -f flag. This way, these subcommands will be executed on the "services" (resources) defined in the docker-compose.yml file.
There is also the possibility of defining the service where a certain subcommand will be executed...
MODELS
docker-compose <SUBCOMAMND> <OPT_SERVICE_NAME>
docker-compose -f <DOCKER_COMPOSE_YML> <SUBCOMAMND> <OPT_SERVICE_NAME>
EXAMPLES
docker-compose stop api
docker-compose -f docker-compose.yml stop api

Making a local bind mount in docker-compose

My team is using docker-compose for our project's container.
Previously, while we were learning how Docker works, we were just using docker. In docker, I could instantly see my local changes on my local deployment by attaching a bind mount to the container when I run it on the command line.
Now, using docker-compose, there doesn't seem to be any such option - my workflow is docker-compose up with little opportunity for deviation. I believe I can specify a bind mount as a volume in our docker-compose.yaml. But that's not really what I'm looking for.
I'd like to be able to specify a local, personal, temporary bind mount without having to modify my team's docker-compose.yaml or commit my personal preferences to vc. How can I specify my bind mount from command line, or equivalent?
Looking at the docs, there doesn't seem to be such an option from the commandline.
One way to get similar behavior to what you want is to make a second docker-compose file with the personal options. If you name it docker-compose.override.yaml, it will be picked up automatically. Otherwise, you can use the -f flag to load it, like so
docker-compose -f docker-compose.yaml -f docker-compose.user.yaml up -d
More details for using multiple docker-compose files are documented here (thanks #Sysix).
You could put this filename into gitignore.

Problem with reopening of the project in container

I have ran into this problem when opening the project in container.
Setting up container for folder or workspace: c:\Work\playground\moodle\lms_administrace
Run: docker-compose -f c:\Work\playground\moodle\lms_administrace\docker\docker-compose-dev.yml config --services
app
redis
db
phpmyadmin
Run: docker-compose --project-name docker -f c:\Work\playground\moodle\lms_administrace\docker\docker-compose-dev.yml up -d --build
Creating volume "docker_mysql_data_volume" with default driver
Pulling app (nodejs:)...
ERROR: The image for the service you're trying to recreate has been removed. If you continue, volume data could be lost. Consider backing up your data before continuing.
Continue with the new image? [yN]
The problem is that I cannot press y or N. I know why I'm having this problem - because I have used that docker compose file before and containers and volumes were created with the directory prefix (docker).
There's a way how to change the compose project name through .env file, but it does not work (I put the file in the root directory, in the directory where compose file is, and in the .devcontainer folder). And also there is -p parameter, but the MS GitHub page does not provide any information.
I can probably fix it by renaming everything, but this may be a serious issue since you can't continue in the process ...
Did anybody experienced similar problem and fixed that?
Thanks,
Karel
You probably mistyped service docker image name in docker-compose.yml.
You are trying to pull nodejs image instead of node
Also, there is could be same error with case postgresql and postgres.
I had the same problem,My problem is using the wrong mirror name.

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

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.

Resources