TL;DR: Create a .env file beside compose.yaml with LAB_MESSAGE=serverlab-ready, then rerun docker compose config to confirm Compose resolves the variable.
The problem
On Ubuntu 24.04, Docker Compose may print this warning when a variable used in your Compose file has no value:
The "LAB_MESSAGE" variable is not set. Defaulting to a blank string.
The warning means Compose found ${LAB_MESSAGE} during interpolation but could not find a value for it. In this harmless lab, the value becomes blank in the rendered configuration until we supply it.
This walkthrough reproduces the warning in a fresh temporary directory, adds a .env file, and checks both the rendered command and the demo output. It requires Docker Engine, Compose V2, and permission to access the running daemon.
Why it happens
Compose uses values from the shell environment and a project .env file for interpolation. In this example, the Compose file includes ${LAB_MESSAGE} in the demo service's command. If that variable is absent from both sources, Compose substitutes a blank string and prints a warning.
The docker compose config command renders the Compose configuration so you can inspect the result before running the service. The .env file here supplies a value for Compose interpolation. That is separate from configuring environment variables inside a container: this demo passes the resolved message as a command argument to echo.
The lab clears the relevant shell and Compose overrides first, so an existing shell value or alternate Compose settings do not hide the missing-variable behavior. The temporary directory keeps the files for this demo together. Any IP addresses, process IDs, or session numbers shown in examples elsewhere are placeholders; none are needed for this lab.
Step-by-step fix
- Create a fresh temporary lab directory and move into it:
LAB_DIR=$(mktemp -d "$HOME/serverlab-compose.XXXXXX")
cd "$LAB_DIR"
- Clear the variable and Compose overrides so the missing value can be reproduced:
unset LAB_MESSAGE COMPOSE_FILE COMPOSE_PROJECT_NAME
unset COMPOSE_ENV_FILES COMPOSE_DISABLE_ENV_FILE
- Set the Compose filename and write the service definition. The command argument uses
${LAB_MESSAGE}so Compose has a value to resolve:
C=compose.yaml
printf '%s\n' 'services:' ' demo:' > "$C"
printf '%s\n' ' image: alpine:3.20' >> "$C"
printf '%s' ' command: ' >> "$C"
printf '%s\n' '["echo", "${LAB_MESSAGE}"]' >> "$C"
- Render the configuration before defining the variable:
docker compose config
Compose reports that LAB_MESSAGE is not set and defaults it to a blank string. This is the expected warning for the reproduction.
- Create
.envbesidecompose.yamlwith the harmless demo value:
printf '%s\n' 'LAB_MESSAGE=serverlab-ready' | tee .env
The file contains exactly:
LAB_MESSAGE=serverlab-ready
- Render the configuration again:
docker compose config
The resolved service command should now contain serverlab-ready.
- Run the demo:
docker compose run --rm demo
How to verify
Check the second configuration rendering. The resolved command should include echo followed by serverlab-ready, rather than a blank value. Then check the output from the demo run: it should print serverlab-ready. Together, the rendered command and matching output confirm that Compose interpolated the value and passed it to the command.
The Compose file created in this lab is:
services:
demo:
image: alpine:3.20
command: ["echo", "${LAB_MESSAGE}"]
The expected excerpts, in order, are:
The "LAB_MESSAGE" variable is not set. Defaulting to a blank string.
LAB_MESSAGE=serverlab-ready
command:
- echo
- serverlab-ready
serverlab-ready
The first line is the warning from the initial config render. The next line is printed when .env is created. The command excerpt is from the later config render, and the final line is the demo output. Warning prefixes can vary by Compose version; pull and creation messages are omitted.
Common mistakes
- Putting
.envsomewhere other than beside the project’scompose.yamlcan prevent this simple project-level setup from supplying the interpolation value. - Expecting
.envto automatically set a variable inside the container confuses Compose interpolation with container environment configuration. In this demo, the resolved value is passed as a command argument. - Checking only the initial warning misses the point of the fix. Render the configuration again after creating
.env, then run the demo and compare its output. - Compose must be able to access the running Docker daemon for the demo run to work.
If you want a clean Ubuntu box to practice on, PowerVPS is a reasonable choice: https://powervps.net/?from=32
Watch more fixes on Server Lab: https://www.youtube.com/channel/UCov7rfWAnS4c2wF5E9V1Yag
Top comments (0)