r/docker • u/Mr_LA • Mar 19 '24
The Ultimate Docker Compose Cheat Sheet
Hello Docker Community!
As a university lecturer, I know the importance of sharing knowledge. That’s why I’m excited to share a comprehensive Docker Compose Cheat Sheet that I’ve created. It is available as PDF and PNG. It’s completely free, with no email signup or any other requirements.
This Cheat Sheet is part of my detailed blog post about Docker Compose. Here is the link: https://devopscycle.com/blog/the-ultimate-docker-compose-cheat-sheet/ It walks through the essentials of managing multi container application with Docker Compose. You will also learn about accessing running containers and how to persist data. The post also includes a public GitHub repository. It comes with all the source code, available for anyone to use and learn from. It is an excellent entry point for an internal hackathon at your company or university.
If you like the blog post, the Docker Compose Cheat Sheet or the GitHub repository, I would appreciate it if you share it. Your feedback and experiences are always welcome. Let’s keep learning and growing together!
Cheers,
Mr_LA
5
u/potyl Mar 19 '24
Thanks for sharing.
I think that this snippet is missing up:
# if you want to start all containers in a background process
# add the flag --detach
$ docker compose --detachAs in docker compose up --detach. The --detach flag is part of the up command.
2
2
4
u/FizzedInMyPantz Mar 19 '24
Great post.
One thing to look at, I believe the condition in depends_on has been deprecated since after version 2. And in the example you use there, you explicitly define version 3.
Also, you may not need to call version at all anymore, and that should also allow you to use the condition in depends_on if I’m not mistaken, since version is now determined by the context of the rest of the docker-compose.yaml file.
2
u/Mr_LA Mar 19 '24
Thank you for your feedback.
I think you are refering to the `depends_on` beeing ignored in a docker swarm setup, right? `depends_on` itself can be used: https://docs.docker.com/compose/compose-file/compose-file-v3/#depends_on
Yes the version itself is optional and only informative: https://docs.docker.com/compose/compose-file/04-version-and-name/
2
u/FizzedInMyPantz Mar 19 '24
Sorry for the confusion, yes, depends_on can still be used. But I’m referring to “condition” within depends_on. “depends_on” starting in version 3, ignores “condition”, as far as I understand it.
1
u/Mr_LA Mar 19 '24
Can you give me a link to the source of this information? As far as I understand from the docs: https://docs.docker.com/compose/compose-file/05-services/#depends_on conditions should work fine in a docker-compose.yml.
1
u/FizzedInMyPantz Mar 20 '24
Interestingly, I'm struggling to find an official source, but I swear I remember this being a problem years ago. Maybe it is supported again. The best I can find are several posts by people stating the same as me, and sourcing/quoting documentation, but then the links they source/quote no longer contain the mentioned quote. So perhaps I'm mistaken.
https://github.com/tiangolo/full-stack-fastapi-template/issues/273
1
u/kolloid Mar 20 '24
I believe, docker removed
conditionin version 3, and I even remember reading that they'll never add that feature back, but then they've returned it back silently.I use
conditionwith in my version 3 configs and it's working just fine. I was unable to find any info in which compose version they reinstated the support for conditions, but it works with any recent docker compose version.
3
u/AFCMS Mar 20 '24
Nice job!
Small thing I would change: docker-compose.yml is no longer the recommended file name, should be just compose.yml
1
2
2
u/Pokeputin Mar 19 '24
Great post! Would also love to know more about building the image, we currently build the image in github actions, and I couldn't find a way to layer cache well like in buildx out of the box action.
1
u/Mr_LA Mar 19 '24
Thank you for your feedback. Can you give me a more detailed overview of what you are currently doing and what you are trying to achieve, so that I can get a better overview of the situation?
1
u/Pokeputin Mar 19 '24
Just a simple docker compose with a server container and a database container, I build the server, run tests, and push to ECR, The build isn't too slow but it rebuilds the server container each time while the buildx action (in another project) has layer caching out of the box which reduces the time to mere seconds.
1
u/ergonet Mar 21 '24
Is the build order the same on both projects? I’m trying to discard a problem with how the image gets rebuilt (i believe that a change in a “lower layer” invalidates the cache for the rest of the upper layers)
1
u/Pokeputin Mar 21 '24
The projects have different images, but I tested building a dummy image with docker compose which should be 100% cacheable, but it looks like docker compose doesn't do layer caching out of the box
2
u/mk_gecko Mar 19 '24
You don't distinguish between keywords "services" and variable names that can be anything like "client", "database" etc. This makes it so confusing that I don't think it would be that useful for anyone.
No one actually NEEDS all three services and they don't have to be called "client, database, server".
Use one colour for reserved words and another for labels that the user can change.
I'll send you something about how "config" works.
There are two types of volumes, but you don't distinguish between them. It's really important to know the difference.
2
u/Mr_LA Mar 19 '24
Thank you for your feedback.
This is an example application, and covers one scenario. Yes you are right, you can name your service however you want. I will make that more clear in the article.
If you go to the sections about volumes, I write about named and mounted volumes.
I would like to gain more insights into how configs work. Please send me the information.
3
u/mk_gecko Mar 19 '24 edited Mar 19 '24
Here you go. Let me know if you have any questions. The docker documentation on "configs" is appalling. I had to try various scenarios to find out how it actually works. I haven't done anything with "secrets" yet.
Using "configs" in docker-compose.yml
Top level element:
configs: app_config: file: ./config/
The top level element is mandatory, just like with volumes. What it does is take the file/folder location on the host and map it to a name. In this example, the folder ./config/ is given the name "app_config".
In a service element:
services: webserv: configs: - source: app_config target: /config/
The configs section here takes the name defined by the top level element and copies the contents to the folder in the docker container.
So "app_config" is now copied to /config/ in the container. The leading "-" is needed for YAML syntax (to start a list)
Why bother with this?
If you use COPY (in Dockerfile) then you have to rebuild the image each time the config changes:
FROM nginx:1.13.6 COPY nginx.conf /etc/nginx/nginx.conf
Problem: the image needs to be rebuilt if the configuration changes.
docker-compose COPIES "configs" into the container when it creates the container, and the image does not need rebuilding.
Test code to see how it works:
Here's the file structure:
> ls -lR .: total 12 drwxrwxr-x 2 adam adam 4096 Jan 31 12:06 config -rw-rw-r-- 1 adam adam 183 Jan 31 12:09 docker-compose.yml -rw-rw-r-- 1 adam adam 17 Jan 31 11:59 ggg ./config: total 8 -rw-rw-r-- 1 adam adam 42 Jan 31 12:06 www -rw-rw-r-- 1 adam adam 33 Jan 31 11:47 yyyHere's docker-compose.yml which you can mess around with. Check inside the container to see what the contents of /mmm are.
>cat docker-compose.yml version: '3' name: aaa services: bbb: image: httpd configs: - source: xxx target: mmm configs: xxx: file: ./config/www1
u/mk_gecko Mar 20 '24
And this will copy the whole folder (both the files www and yyy)
>cat docker-compose.yml version: '3' name: aaa services: bbb: image: httpd configs: - source: xxx target: /config/ configs: xxx: file: ./config/Note that
WORKDIRis /usr/local/apache2 so you have to docd /to see the copied config files.1
u/kolloid Mar 20 '24
Is there any difference from bind mount volumes? I always used mounted folder/file for such cases.
2
u/mk_gecko Mar 20 '24
Actually, no. I don't see any difference at all. I suppose that someone wanted to distinguish between where code is stored (eg. src file) and where credentials are stored. This way you can have the same code base, but multiple credentials. Of course, you could just use a separate bind mount for this.
The next thing they did is to implement "secrets", which seems to be just like configs, but encrypted. However, I've not experimented with it.1
u/mk_gecko Mar 19 '24
You know, upon further consideration, this cheat sheet would not be meant for people who don't already know docker and docker-compose. If it's a review of existing knowledge (or a brief summary of things that they don't know), then it's fine. You just need to have one colour for reserved words.
3
u/oldominion Mar 19 '24
This is awesome, I am doing my final exam project and wanted to use Docker for it so this comes at the perfect time. Thank you very much!
1
2
1
u/basudip Mar 19 '24
any love for macvlan? like how to set IP address, DNS of a macvlan network via compose? currently I am setting the IP/DNS from Portainer.
1
u/colonelmattyman Mar 19 '24
This is pretty good. I mean I just use stacks in Portainer to manage my two docker instances, but the chest sheet will come in handy. I'll probably end up moving it to markup on my bookstack server (hosted on Docker).
1
1
u/technicaldebt23 Mar 20 '24
It amaze me that my old domain (devopscycle.com) turn to something amazing. kudos!
1
u/Scepterus Apr 14 '24
This is for the old compose spec, the new compose sepc V2 ditched the version on top and made changes to networking and stuff.
-3
u/root_switch Mar 19 '24
You posted this 120 days ago. https://www.reddit.com/r/docker/s/XvnIrE29qe
7
u/Mr_LA Mar 19 '24
Thank you for your concerns.
No, the post you are referencing is about The Ultimate Docker Cheat Sheet.
This post is about The Ultimate Docker COMPOSE Cheat Sheet.
11
u/thunderbong Mar 19 '24
Seriously awesome! Bookmarked.
I feel you should add 'include' as well. I got to know of this recently and it's been very helpful.
https://docs.docker.com/compose/multiple-compose-files/include/