Skip to content

Commit

Permalink
feat: add til how to use default values in docker compose yml
Browse files Browse the repository at this point in the history
fixes: #153
  • Loading branch information
hmajid2301 committed Dec 17, 2023
1 parent 29a0364 commit 6630822
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tasks:

new_post:
cmds:
- scripts/add "{{.CLI_ARGS}}"
- new_post "{{.CLI_ARGS}}"

new_talk:
cmds:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: "TIL: How to Use Default Values in docker-compose.yml"
date: 2023-12-30
canonicalURL: https://haseebmajid.dev/posts/2023-12-30-til-how-to-use-default-values-in-docker-compose-yml
tags:
- docker-compose
- bash
series:
- TIL
---

**TIL: How to Use Default Values in docker-compose.yml**

Sometimes we want to use env variables in our docker-compose files like so:

```yml
services:
client:
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/nginx
ports:
- 8000:80
```
Here we are going to use the GitLab CI dependency proxy to pull our Nginx image, so we can speed up our pipelines but
also avoid being rate limited by docker hub. However, when running this locally, we will need to make sure the
`CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX` env variable is set. Which just adds a bit more work, instead we can leverage
some of the special syntax docker-compose provides [^1], which I think it inherits from bash.

Where we can use interpolation to set default values if the env variable is not set:

```yml
services:
client:
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX:-docker.io}/nginx
ports:
- 8000:80
```

In this case, `:-` will use docker.io if the env variable is not set or is empty. There are several other variations
you can find in the footnote below.

[^1]: https://docs.docker.com/compose/environment-variables/env-file/
19 changes: 19 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
pkgs = nixpkgs.legacyPackages.x86_64-linux;
in
pkgs.mkShell {
new_post = pkgs.writeScriptBin "new_post" ''
#!/usr/bin/env bash
TITLE=$(gum input --prompt "Post title:")
USER_DATE=$(gum input --prompt "Date to publish YYYY-MM-DD:")
TITLE_SLUG="$(echo -n "$TITLE" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z | tail -c +2 | head -c -1)"
DATE="$(date +"%F")"
SLUG="$DATE-$TITLE_SLUG"
git checkout -b "$SLUG"
hugo new --kind post-bundle posts/$SLUG
echo "Creating OG for content/posts/$SLUG"
python scripts/og/generate.py content/posts/$SLUG
rm content/posts/$SLUG/images/.gitkeep
'';

generate_og = pkgs.writeScriptBin "generate_og" ''
python ./scripts/og/generate.py
'';
Expand All @@ -19,6 +37,7 @@
hugo
python3
go-task
gum
];
};
};
Expand Down
14 changes: 0 additions & 14 deletions scripts/add

This file was deleted.

0 comments on commit 6630822

Please sign in to comment.