Changing an environment variable in a running Docker container
Docker can't change a running container's environment, so changing NODE_ENV means recreating the container. How I copied a worker's environment to a file, changed one variable, swapped the containers with a rollback ready, and timed it with the app deploy that changed NODE_ENV.
Contents
We recently moved a Node.js app from Node 14 to Node 24, and as part of that added one line to its Dockerfile:
| |
Our production environment had never set NODE_ENV. Express caches compiled views when it’s production, so we wanted it set, and we knew that setting it would change more than Express. This line decides which queue PDF uploads go to:
| |
When someone uploads a PDF, the app puts a job on a queue, and a separate worker container converts it to slides. With NODE_ENV unset, production had always created process_pdf_development jobs, and the worker on the production server was set up to match:
| |
Once the new image was live, the app would create process_pdf_production jobs, and the worker would ignore them until it was told to look for that type too. So changing the worker’s JOB_TYPE was part of the upgrade plan. The worker was already running, though, and Docker won’t change a running container’s environment.
You have to recreate the container. Dump its current environment to a file, edit the one line, and create a new container from the same image with --env-file. Rename the old container first instead of deleting it, so rolling back takes one command.
Why you can’t just edit it
A container’s environment is part of its configuration, written once when docker run or docker create makes it. None of the commands that look like they might change it actually do:
| Command | What it actually does |
|---|---|
docker update | Changes CPU and memory limits and the restart policy. Nothing else. |
docker exec -e KEY=value ... | Sets the variable for that one exec process. The main process never sees it. |
docker restart | Restarts with the same configuration, old environment included. |
export KEY=value inside a shell | Changes that shell only. |
You’ll find guides that stop the Docker daemon and edit config.v2.json under /var/lib/docker/containers/. It works, but you’re hand-editing Docker’s internal state on a production box, and the result doesn’t match anything you could rebuild. I didn’t do it.
What you do instead is make a new container: same image, same settings, one changed value. Images are meant to be replaced, and nothing important should live in the container itself. Before you recreate one, though, make sure that’s true of yours.
Check what the variable does first
Node.js itself ignores NODE_ENV. Libraries and your own code decide what it means. Express turns on view caching for production. Some logging setups only write to the console in development. Some apps choose a database or config file by it. In our app it’s also part of a queue name, which is why the worker change was in the plan.
So before changing it, search for it, in the app and in anything that talks to the app:
| |
Do the same inside any container you’re about to change. You don’t need the source checked out for that:
| |
While I was on the server, I noticed the job queue container had NODE_ENV=staging, on production. The search showed its code read the variable in one place, to decide whether to log to the console, where staging and production behave the same. I left it alone.
Find out how the container was started
To recreate a container, you need every option it was started with, not just the environment. docker inspect has them all:
Look at:
- Image: use the exact name or ID it shows, not
latest. If the image has been retagged since,latestmay now point at different code. - Cmd and Entrypoint: if these match the image’s defaults, you don’t need to pass them.
- Ports, Mounts, Network: each one needs a matching
-p,-vor--networkflag. - Restart: easy to forget, and then the container doesn’t come back after the next reboot.
- Labels:
com.docker.compose.*labels mean the container belongs to Compose. Skip the rest of this post and use the compose section below.
Also check whether a script or systemd unit creates the container. If one does and you don’t update it, the next person who runs it puts the old value back:
| |
Our worker had no ports or mounts, Restart=always, the default command, and no script creating it.
Copy the environment to a file
Retyping the environment by hand is how you lose a variable or put a typo in a secret. Write it to a file instead and change just the one line:
A few things to know:
.Config.Envincludes variables from the image’s ownENVlines, not just the ones passed atdocker run. Copying them is harmless. I dropPATH, so the image’s own value is used.printlnleaves a blank line at the end. Docker skips blank lines, but filtering them keeps the file tidy for the line count below.--env-filetakesKEY=valuelines exactly as written. Quotes aren’t removed, soKEY="x"sets the value to"x"including the quotes. Values fromdocker inspecthave no quotes, so this only bites if you edit the file by hand.- To add a variable rather than change one, append it:
echo 'NODE_ENV=production' >> app.env. - The file probably contains secrets. Keep it
600, and delete it when you’re done.
Read the file before you go on. It should have the same number of lines as the original environment minus PATH, with only your change different.
Swap the containers, keeping the old one
Renaming first frees the name, and the old container keeps its full configuration, so rolling back is quick. docker update --restart=no matters if the old one had Restart=always. Without it, a Docker restart or a reboot would bring the old container back with the old value, and you’d have two workers fighting over the same queue.
Add whatever else docker inspect showed: -p for ports, -v for mounts, --network for a network. One trap with volumes: an anonymous volume (one with a long hex name, not a name you gave it) belongs to the old container. A new -v /path creates a new, empty one. --volumes-from <container>-old mounts the old container’s volumes into the new one.
Then check it:
The log only tells you it started. Test what the variable controls. For us, that means uploading a PDF in production and watching it turn into slides.
If anything’s wrong, roll back:
Once you’re happy with it, clean up with docker rm <container>-old and rm app.env.
Timing it with a deploy
My worker change and the app change had to go out together. Pending jobs made the order matter:
- Jobs created before the deploy are typed
process_pdf_development. If you switch the worker first, nothing processes them. - Jobs created after the deploy are typed
process_pdf_production. If you switch the worker late, they wait until you do.
So: deploy the app, check that no process_pdf_development jobs are still waiting, then swap the worker. A short wait between the two is fine, since the queue holds the jobs. The job queue’s API, or a quick look at its database, tells you what’s pending.
If you use Docker Compose
Compose does all of this for you. Change the value in compose.yaml (or the .env file it reads):
then run:
| |
Compose sees the configuration has changed and recreates just that service, keeping its named volumes. You don’t get an -old container to fall back on, so to roll back you change the value back and run the same command again.
Looking back
We knew from the start that setting NODE_ENV on production would reach past Express, because the queue name depends on it. Searching the code for the variable is how we knew, and the same search showed that the job queue container’s odd staging value didn’t matter.
Recreating the worker takes more steps than editing one value would. In exchange, the new container’s configuration sits in a file I read before running it, and the old container waits, stopped, until the new one has processed a PDF.