Sub topic 9
Writing a Literal Dollar Sign with $$
This chapter assumes you can read a compose.yaml, know that Compose replaces ${VAR} expressions before it starts containers, and have run docker compose config before.
Why a dollar sign needs escaping
Compose scans the values in your Compose file for $ and treats what follows as a variable to interpolate. That happens on the machine where you run docker compose, before any container exists. If you want the $ to reach the container unchanged, you have to tell Compose to leave it alone.
Write $$ to do that. Compose turns each $$ into one literal $ and does not interpolate the text after it.
services:
app:
image: my-app:latest
command: ["sh", "-c", "echo Price is $$5 and user is $$USER"]Compose passes this command to the container:
sh -c "echo Price is $5 and user is $USER"Then the shell inside the container runs the command. It expands $USER using the container's own environment. It also expands $5, which is a positional parameter. In sh -c, $5 is empty here, so the output shows "Price is and..." with the 5 missing. If you want a literal $5 in the output, the container shell needs its own quoting, for example single quotes around the text.
Two layers of expansion
Two programs can expand a $ in this example, one after the other.
| Layer | Who expands it | When | How to stop it |
|---|---|---|---|
| Compose interpolation | `docker compose` on your machine | When it reads the Compose file | Write `$$` |
| Shell expansion | The shell in the container | When the command runs | Use single quotes or `\$` in the shell command |
Without $$, Compose expands $USER using its own variables, which come from your shell and .env. The container never sees the name USER.
Seeing the difference
Run docker compose config to see what Compose will pass on. Using the file above:
docker compose configThe command line in the output shows $$ still escaped, because config prints Compose syntax. Compose will convert it when it creates the container. To confirm the value inside the container, start the service and read its output with docker compose logs app.
Now compare an unescaped version:
services:
app:
image: my-app:latest
command: ["sh", "-c", "echo user is $USER"]If USER is set in the shell where you run Compose, docker compose config shows its value already in the command. If it is unset, Compose substitutes an empty string and prints a warning that the variable is not set. Both results are surprises when you meant the container to decide.
Where this matters
- Shell commands in
commandorentrypoint: any$VARmeant for the container shell needs$$VAR. - Passwords containing a dollar sign: in a Compose file, write
pa$$word. In a.envfile used for interpolation, a value in single quotes is read literally. - Healthchecks: a test such as
["CMD-SHELL", "pg_isready -U $$POSTGRES_USER"]makes the container's own variable do the work.
services:
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: change-me
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER"]
interval: 5sHere POSTGRES_USER is set in the container's environment. The healthcheck must read it inside the container, so the Compose file uses $$.
Exercise
A service has command: ["sh", "-c", "echo Home is $HOME"]. Your host shell has HOME=/home/dev. The container's HOME is /root. What does the container print, and how do you change the command so it prints the container's value?
Show answer
It prints "Home is /home/dev", because Compose interpolates $HOME from your shell before the container starts. Change the command to echo Home is $$HOME. Compose then passes $HOME to the container shell, which prints "Home is /root".