Chapter 4 · Lesson 13 of 18
Deploy and operate

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.

By the end you will

Run your app on a server with docker compose up -d, with secrets in an env file and the port private.

Time
25 min
Level
Beginner
Check
4 questions
You need
  • 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#

yaml
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#

bash
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:

bash
DATABASE_URL=postgres://USER:PASSWORD@DBHOST:5432/DBNAME
SECRET_KEY=replace-me

Restrict 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#

bash
cd ~/app
docker compose up -d

What 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#

bash
docker compose ps
docker compose logs --tail 30 app
curl -I http://127.0.0.1:3000

ps 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#

bash
docker compose pull
docker compose up -d

Compose 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 when ufw says the port is closed. Use 127.0.0.1:3000:3000 behind a proxy.
  • Changing .env does nothing until recreate. Variables are read when the container is created. Run docker compose up -d after editing.
  • Quotes and spaces in .env. Values are read literally by Compose, and quoting rules differ from a shell. Test with docker compose config to 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 login on the server, or the pull fails with an authorization error.
  • down -v deletes volumes. docker compose down removes containers and networks only. Adding -v also 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 -h and consider swap.

Troubleshooting#

SymptomLikely causeFix
The "VAR" variable is not set. Defaulting to a blank string.A name used in compose.yaml is missing from .envAdd it to .env
Bind for 127.0.0.1:3000 failed: port is already allocatedAnother process or container uses the portFind it with sudo ss -tlnp and change one port
pull access denied or manifest unknownWrong image name or tag, or not logged inCheck the tag, run docker login
exec format error in the logsImage built for another CPU architectureBuild for the server’s architecture
Site returns 502Proxy cannot reach the appSee Fix 502 Bad Gateway
Container is RestartingApp crashes at startRead docker compose logs app
permission denied while trying to connect to the Docker daemon socketUser is not in the docker groupUse sudo, or add the user to the group and log in again
App ignores new .env valuesContainer not recreateddocker compose up -d again
Data gone after a redeployData was in the container, not a volumeMount 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#

TaskCommand
Validate and show resolved filedocker compose config
Start or updatedocker compose up -d
Download new imagesdocker compose pull
Statusdocker compose ps
Logsdocker compose logs -f --tail 100 app
Stop, keep datadocker compose down
Stop and delete volumesdocker compose down -v (destructive)

Security and safety notes#

  • Keep .env at mode 600 and out of git.
  • Publish app ports on 127.0.0.1 only when a proxy fronts them.
  • docker compose down -v deletes volumes. Back up first, see Back up a Postgres database in Docker.

Next#

Stop logs from filling the disk: Limit Docker log size.

Check what you learned4 questions
1Why publish the app port as 127.0.0.1:3000:3000?
2What do you commit to git for configuration?
3Why pin an image tag like 1.0.0 instead of latest?
4You run docker compose down -v on the server. What is lost?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios