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

URL: https://levelrail.com/learn/deploy-operate/deploy-an-app-with-docker-compose/
Updated: 2026-11-27

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](https://levelrail.com/learn/docker/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](https://levelrail.com/learn/deploy-operate/what-is-ci-cd/))) and pushed to a [registry](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/deploy-operate/rollbacks/)) 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

| 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](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/docker/docker-volumes-and-persistent-data/)) |

## 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 `.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](https://levelrail.com/learn/deploy-operate/back-up-a-postgres-database-in-docker/)).

## Related lessons

- [Docker Compose basics](https://levelrail.com/learn/docker/docker-compose-basics/)): the file format used here
- [Environment variables and secrets](https://levelrail.com/learn/deploy-operate/environment-variables-and-secrets/)): what goes in `.env`
- [Publish container ports safely](https://levelrail.com/learn/docker/publish-container-ports-safely/)): why the 127.0.0.1 prefix matters
- [Deploy a Docker Compose app, self-hosted](https://levelrail.com/blog/deploy-docker-compose-app-self-hosted/)): a longer walkthrough

## Next

Stop logs from filling the disk: [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)).

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

- [Docker docs, Compose file reference](https://docs.docker.com/reference/compose-file/)
