How does Docker name repositories? - docker

In a directory called ringer i've created a simple Dockerfile that will install some stuff and mount my app... I have never explicitly set a name.
I then used a docker-compose.yml file to define web and pointed it at the Dockerfile i mentioned above.
When i run docker images i see the following:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ringer_web latest bf9b931e1030 8 minutes ago 938.9 MB
It looks like Docker named my image's repository after the local directory and image name... My question is, how exactly does Docker choose to name an image's repository? (and if applicable, why)

From the docs:
COMPOSE_PROJECT_NAME
Sets the project name, which is prepended to the name of every container started by Compose. Defaults to the basename of the current working directory.
BTW, this is not docker, but docker-compose that is deciding on the name. To change it, set the COMPOSE_PROJECT_NAME before running docker-compose:
COMPOSE_PROJECT_NAME=myprefix docker-compose up

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.

docker-compose.yml file structure and name

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.

Docker container copy files from local path into container

I need to copy my customized keycloak themes into keycloak container to use it like mention here:
https://medium.com/#auscunningham/change-login-theme-in-keycloak-docker-image-55b5fa5ceec4
After identifying my container id: docker container ls and making a list of files like this: docker exec 7e3a420017a8 ls ./keycloak/themes
It returns the list of themes correctly, but using this to copy my files from local to container:
docker cp ./mycustomthem 7e3a420017a8:/keycloak/themes/
or
docker cp ./mycustomthem 7e3a420017a8:./keycloak/themes/
I get the following error:
Error: No such container:path: 7e3a420017a8:/keycloak
I cannot imagine where the error is, since I can list the files into the folder and container, could you help me?
Thank you in advance.
Works on my computer.
docker cp mycustomthem e67f76e8740b:/opt/jboss/keycloak/themes/raincatcher-theme
You have added the wrong path in command add full path /opt/jboss/keycloak/themes/raincatcher-theme.
This seems like a weird way to approach this problem. Why not just have a Dockerfile that uses the Keycloak container as the base image and then copies the theme into the container at build time? Then just run the image you build? This will also be a more stable pattern in the long term if you ever decide to add any plugins or customizations and it provides an easy upgrade path to new versions by just changing the base image in your Dockerfile.
Update according to your new question update:
Try the following:
docker cp ./mycustomthem 7e3a420017a8:/opt/jboss/keycloak/themes/
The correct path in Keycloak is actually /opt/jboss/keycloak/themes/

how does docker map volume names in docker compose file to volumes on system

I am migrating a tvial docker mail server from one system to another. I set this up some time ago, and vaguely remember the steps, but not every detail. I copied my mail data and mail state volumes to the new system, but when I went to run docker on the new system I was confused. The old system shows this in docker compose:
- maildata:/var/mail
- mailstate:/var/mail-state
volumes:
maildata:
driver: local
mailstate:
driver: local
But the volumes have different names:
$ sudo docker volume ls
DRIVER VOLUME NAME
local tvial_docker-mailserver_maildata
local tvial_docker-mailserver_mailstate
If I inspect the running instance it shows that it is bound to the correct volume.
How do I reproduce this on the new system?
EDIT:
I ran the docker container on the new system and the maillog container seems to be constructed using a similar nomenclature:
Creating volume "tvial_docker-mailserver_maillogs" with local driver
So this is no longer a pressing issue, but I reviewed the dockerfile for this container, and I don't see where the name is coming from. Can anyone tell me?
The resources are prefixed with the project(deployment) name.
This can be specified using the -p flag when you run the docker-compose up command. If not specified, the project name defaults to the name of the directory to whom your docker-compose.yaml file belongs to.
Official documentation here: https://docs.docker.com/compose/reference/envvars/#compose_project_name . Relevant extract for the question:
COMPOSE_PROJECT_NAME:
Sets the project name. This value is prepended along with the service name to the container on start up. For example, if your project name is myapp and it includes two services db and web, then Compose starts containers named myapp_db_1 and myapp_web_1 respectively.

Docker commit - does not keep new settings?

I'm trying to create (without a dockerfile) a custom image (apache) which I want to publish to other programmers.
I already have this image :
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
eboraas/apache latest ddf3c3b74564 3 months ago 365MB
Now let's create a container with settings :
docker run -p 8520:80 -v //c/Users/sff/apache:/var/www/html -d eboraas/apache
OK , now all is working and I do see the test page :
BTW - docker ps -a is now :
C:\r>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a1083fded97 eboraas/apache "/usr/sbin/apache2ct…" 2 minutes ago Up 2 minutes 443/tcp, 0.0.0.0:8520->80/tcp eloquent_heisenberg
OK
I was told that I can use docker commit in order to keep the container as a custom new image.
And so I did :
C:\r>docker commit 3a1083fded97 131313222/shared-apache
sha256:14135d48fcf827527d655225908a29b15223317810be9cf22651ae375a87a774
And it did save new image :
C:\r>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
131313222/shared-apache latest 14135d48fcf8 49 seconds ago 365MB
eboraas/apache latest ddf3c3b74564 3 months ago 365MB
But now I expect that running docker run 131313222/shared-apache - will keep(!!) the folder mapping and the port mapping :
But If I run this :
docker run 131313222/shared-apache
I get :
C:\r>docker run 131313222/shared-apache AH00558: apache2: Could not
reliably determine the server's fully qualified domain name, using
172.17.0.2. Set the 'ServerName' directive globally to suppress this message
And obviously the test page doesn't work.
Question:
Why doesn't the commit command ( into a new image) save the port and volume mapping ?
In other words : (without a docker file) - how can I create an image that will include its current settings ? I want developers to start working immediately as if each was writing :
docker run -p 8520:80 -v //c/Users/sff/apache:/var/www/html -d eboraas/apache
Shouldn't the commit do that already ?
I want everyone to see what I see without doing anything.
A Docker image only contains its filesystem and a small amount of metadata like the default command to run and environment variable settings. It doesn't contain any of the other settings you might specify, like port mappings, volume mounts, access to host devices, privileged access to host-level settings, or background mode. You simply can't create an image that defaults to the settings you show in the question.
If your audience is other programmers, I'd suggest writing up the sequence to build the image as a Dockerfile, and adding that Dockerfile and a shell script or Docker Compose YAML file that runs the image to a common source control repository.
In fact, I'd advise you to never use docker commit. The Dockerfile system is fairly straightforward (if you could specify these settings they'd be one line each in the Dockerfile; ENV foo bar replaces docker run -e foo=bar for example) and Dockerfiles can be checked into source control quite easily. When you need to rebuild your image – because a new version of Apache is out with a critical security update, because some of the software you bundled changed – a Dockerfile both gives you a record of what exactly you did to build the image, and a single command to actually do it.

Resources