Deploy an app with Docker Compose on a server
Deploying with Docker Compose means putting compose.yaml and a .env file on the server and running docker compose up -d so the services start and stay running.
- Updated Nov 27, 2026
- Markdown
Run your app on a server with docker compose up -d, with secrets in an env file and the port private.
- A server with Docker installed
- SSH access and an image in a registry
To deploy an app with Docker Compose, put a compose.yaml and a .env file on the server and run docker compose up -d. Compose pulls the image, creates the containers and starts them in the background.
This assumes Docker is installed on the server. See Install Docker on Ubuntu.
How it works#
The server holds only two files, compose.yaml and .env. The image is built elsewhere (on your machine or in CI) and pushed to a registry, and the server pulls it. That keeps the server small and makes every deploy the same two commands. A reverse proxy such as nginx or Caddy on the same host serves the public traffic over HTTPS and forwards it to the app.
docker compose up -d compares the file with what is running. Services that do not exist are created. Services whose configuration or image changed are recreated. Services that did not change are left alone. -d detaches, so the command returns and the containers keep running. With restart: unless-stopped, Docker’s daemon starts them again after a reboot, as long as the Docker service itself starts at boot.
Worked example: you push app:1.0.1 to the registry. On the server you edit the tag in compose.yaml, run docker compose pull to download it, and docker compose up -d. Compose stops the old container, starts a new one from the new image, and keeps the same .env and volumes. The public proxy keeps its config, and the port stays private.
Step 1: Prepare the compose file#
services:
app:
image: ghcr.io/example/app:1.0.0
restart: unless-stopped
env_file: .env
ports:
- "127.0.0.1:3000:3000"restart: unless-stopped brings the app back after a crash or reboot. env_file: .env loads variables from the file next to it. The 127.0.0.1: prefix keeps the port private, so the reverse proxy is the only public entry. Without it, Docker publishes the port on all interfaces, and its firewall rules can bypass tools like ufw.
What you should see: nothing yet, since this is a local file. Validate it with docker compose config, which prints the resolved file or an error.
Step 2: Copy it to the server#
ssh user@203.0.113.10 'mkdir -p ~/app'
scp compose.yaml user@203.0.113.10:~/app/mkdir -p creates the folder and does nothing if it exists. scp copies the file over SSH.
What you should see: a progress line for compose.yaml with its size.
Step 3: Add secrets in .env#
On the server, create ~/app/.env:
DATABASE_URL=postgres://USER:PASSWORD@DBHOST:5432/DBNAME
SECRET_KEY=replace-meRestrict it with chmod 600 .env. Never commit it. See Environment variables and secrets.
What you should see: ls -l .env shows -rw-------, so only your user can read it.
Step 4: Start the stack#
cd ~/app
docker compose up -dWhat you should see: lines like Container app-app-1 Started. On the first run you also see the image being pulled. The prompt returns after the container starts.
Check it worked#
docker compose ps
docker compose logs --tail 30 app
curl -I http://127.0.0.1:3000ps should show the service as running (or healthy if you set a healthcheck). The logs should show the app starting without errors. curl should return an HTTP status line from your app, such as HTTP/1.1 200 OK.
Update later#
docker compose pull
docker compose up -dCompose recreates only the services whose image changed. Pin a version tag rather than latest, so you always know what is running and can roll back by putting the old tag back.
Edge cases and gotchas#
- Unprefixed ports bypass the firewall. A mapping like
"3000:3000"publishes on all interfaces, and Docker’s own rules can let traffic through even whenufwsays the port is closed. Use127.0.0.1:3000:3000behind a proxy. - Changing
.envdoes nothing until recreate. Variables are read when the container is created. Rundocker compose up -dafter editing. - Quotes and spaces in
.env. Values are read literally by Compose, and quoting rules differ from a shell. Test withdocker compose configto see the resolved values. - Wrong architecture. An image built for amd64 will not run on an ARM server and prints an
exec format error. Build for the server’s architecture. - Private registries. A private image needs
docker loginon the server, or the pull fails with an authorization error. down -vdeletes volumes.docker compose downremoves containers and networks only. Adding-valso deletes named volumes, including database data.- Project name changes volume names. Compose prefixes names with the folder name. Moving the folder can make a new, empty volume and look like data loss.
- Low-memory servers. A big image pull plus a start can exhaust RAM and kill processes. Check
free -hand consider swap.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
The "VAR" variable is not set. Defaulting to a blank string. | A name used in compose.yaml is missing from .env | Add it to .env |
Bind for 127.0.0.1:3000 failed: port is already allocated | Another process or container uses the port | Find it with sudo ss -tlnp and change one port |
pull access denied or manifest unknown | Wrong image name or tag, or not logged in | Check the tag, run docker login |
exec format error in the logs | Image built for another CPU architecture | Build for the server’s architecture |
| Site returns 502 | Proxy cannot reach the app | See Fix 502 Bad Gateway |
Container is Restarting | App crashes at start | Read docker compose logs app |
permission denied while trying to connect to the Docker daemon socket | User is not in the docker group | Use sudo, or add the user to the group and log in again |
App ignores new .env values | Container not recreated | docker compose up -d again |
| Data gone after a redeploy | Data was in the container, not a volume | Mount a volume, see Docker volumes |
Real-world scenario#
You deploy a small web app on a fresh VPS. The first up -d works, but the site through nginx shows 502. You run docker compose ps and see the app Up, then curl -I http://127.0.0.1:3000 fails with Connection reset by peer. The app binds to 127.0.0.1 inside the container. You set its host variable to 0.0.0.0 in .env, run docker compose up -d, and the curl returns a 200.
A week later you ship version 1.0.1. You change the tag, run docker compose pull and docker compose up -d. The new version has a bug, so you put 1.0.0 back and run the same two commands. The rollback takes under a minute because the old image is still on the server.
Quick reference#
| Task | Command |
|---|---|
| Validate and show resolved file | docker compose config |
| Start or update | docker compose up -d |
| Download new images | docker compose pull |
| Status | docker compose ps |
| Logs | docker compose logs -f --tail 100 app |
| Stop, keep data | docker compose down |
| Stop and delete volumes | docker compose down -v (destructive) |
Security and safety notes#
- Keep
.envat mode 600 and out of git. - Publish app ports on
127.0.0.1only when a proxy fronts them. docker compose down -vdeletes volumes. Back up first, see Back up a Postgres database in Docker.
Related lessons#
- Docker Compose basics: the file format used here
- Environment variables and secrets: what goes in
.env - Publish container ports safely: why the 127.0.0.1 prefix matters
- Deploy a Docker Compose app, self-hosted: a longer walkthrough
Next#
Stop logs from filling the disk: Limit Docker log size.
Check what you learned
Frequently asked questions
Should the app port be open to the internet?
Not when a reverse proxy serves it. Bind the port to 127.0.0.1 so only the proxy on the same machine can reach it.
Do I commit the .env file?
No. Keep it on the server only and add it to .gitignore. Commit an example file with empty values instead.
Why does docker compose up say the image was not found?
The tag may be wrong, or the registry is private and the server is not logged in. Check the tag with docker pull and run docker login if needed.
Why does my app still use old settings after I edit .env?
Compose reads .env when it creates the container. Run docker compose up -d again so the container is recreated with the new values.
Sources
Keep going: guides and tutorials
- Zero Downtime Deploy for Docker Apps, With Rollback How a Docker deploy waits for readiness before cutover, holds the previous release for rollback, rolls back on its own, and how to rehearse it.
- Roll Back a Failed Docker Deploy Without Downtime Ship a broken release on purpose, watch your users stay on the old version, then roll back with one command. Real commands, real output, self-hosted.
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.