Short answer

Put the services in compose.yaml in a folder of their own, the passwords in a .env file next to it (chmod 600), and start everything with docker compose up -d. Keep the database in a named volume so docker compose down does not lose it, and publish web ports as 127.0.0.1:8080:8080. docker compose down -v deletes the data.

Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed

Before you start

  • Docker Engine with the Compose plugin, installed as in Install Docker on Ubuntu. Compose is docker compose, with a space.
  • Your user in the docker group, so no sudo is needed (details).

Step 1: The project folder and compose.yaml

The folder’s name becomes the project name: containers, the network and the volume are all named after it (myapp-db-1, myapp_db-data).

Server
$ mkdir ~/myapp && cd ~/myapp
Server
$ cat > compose.yaml <<'EOF'services:  db:    image: postgres:17-alpine    restart: unless-stopped    environment:      POSTGRES_DB: app      POSTGRES_USER: app      POSTGRES_PASSWORD: ${DB_PASSWORD}    volumes:      - db-data:/var/lib/postgresql/data    healthcheck:      test: ["CMD-SHELL", "pg_isready -U app -d app"]      interval: 5s      timeout: 3s      retries: 10   adminer:    image: adminer:5    restart: unless-stopped    ports:      - "127.0.0.1:8080:8080"    depends_on:      db:        condition: service_healthy volumes:  db-data:EOF
  • No version: line at the top: Compose v2 does not need it.
  • POSTGRES_PASSWORD: ${DB_PASSWORD}: the value comes from .env, so the password is not in the file you might share.
  • db-data:/var/lib/postgresql/data: the database lives in a named volume, outside the container.
  • healthcheck plus depends_on: condition: service_healthy: Adminer starts only when PostgreSQL answers, not merely when its container has started.
  • 127.0.0.1:8080:8080: the web port is reachable from the server only. A plain 8080:8080 listens on every address, and Docker’s rules bypass ufw (shown here).
  • restart: unless-stopped: both come back after a crash or a reboot.

Step 2: The password in .env

Let the server make a random password, so you never type one into a command (where it would stay in your shell history), and make the file readable by you only:

Server
$ echo "DB_PASSWORD=$(openssl rand -hex 16)" > .env && chmod 600 .env
Server
$ ls -l .env-rw------- 1 deploy deploy 45 Nov 19 23:03 .env
Output hidden (1 line)

Check the file before starting anything:

Server
$ docker compose config --quiet && echo validvalid
Output hidden (1 line)

Step 3: Start it

Server
$ docker compose pull --quiet Image postgres:17-alpine Pulling Image adminer:5 Pulling Image adminer:5 Pulled Image postgres:17-alpine Pulled
Output hidden (4 lines)
Server
$ docker compose up -d Network myapp_default Creating Volume myapp_db-data Creating Volume myapp_db-data Created Network myapp_default Created Container myapp-db-1 Creating Container myapp-db-1 Created Container myapp-adminer-1 Creating Container myapp-adminer-1 Created Container myapp-db-1 Starting Container myapp-db-1 Started Container myapp-db-1 Waiting Container myapp-db-1 Healthy Container myapp-adminer-1 Starting Container myapp-adminer-1 Started
Output hidden (14 lines)

Compose made the network and the volume, started db, waited until it was Healthy, and only then started Adminer. -d runs it in the background.

Server
$ docker compose psNAME              IMAGE                COMMAND                  SERVICE   CREATED         STATUS                   PORTSmyapp-adminer-1   adminer:5            "entrypoint.sh docke…"   adminer   6 seconds ago   Up Less than a second    127.0.0.1:8080->8080/tcpmyapp-db-1        postgres:17-alpine   "docker-entrypoint.s…"   db        9 seconds ago   Up 6 seconds (healthy)   5432/tcp
Output hidden (3 lines)
Server
$ curl -s http://127.0.0.1:8080/ | grep -o '<title>[^<]*</title>'<title>Login - Adminer</title>
Output hidden (1 line)
Server
$ sudo ss -tln | grep ':8080 'LISTEN 0      4096       127.0.0.1:8080      0.0.0.0:*
Output hidden (1 line)

Adminer answers, and port 8080 listens on 127.0.0.1 only. To open it from your own computer, use an SSH tunnel to the server or put it behind Nginx with HTTPS.

Step 4: Put data in, read the logs

Server
$ docker compose exec db psql -U app -d app -c "CREATE TABLE notes (t text); INSERT INTO notes VALUES ('kept');"CREATE TABLEINSERT 0 1
Output hidden (2 lines)
Server
$ docker compose logs db --tail 3db-1  | 2026-11-19 23:07:10.929 UTC [1] LOG:  listening on Unix socket "/run/postgresql/.s.PGSQL.5432"db-1  | 2026-11-19 23:07:10.954 UTC [56] LOG:  database system was shut down at 2026-11-19 23:07:10 UTCdb-1  | 2026-11-19 23:07:10.980 UTC [1] LOG:  database system is ready to accept connections
Output hidden (3 lines)

Add -f to follow the logs; leave out the service name to see all of them.

Does docker compose down delete the data?

No. down removes the containers and the network, not the named volume:

Server
$ docker compose down Container myapp-adminer-1 Stopping Container myapp-adminer-1 Stopped Container myapp-adminer-1 Removing Container myapp-adminer-1 Removed Container myapp-db-1 Stopping Container myapp-db-1 Stopped Container myapp-db-1 Removing Container myapp-db-1 Removed Network myapp_default Removing Network myapp_default Removed
Output hidden (10 lines)
Server
$ docker volume lsDRIVER    VOLUME NAMElocal     myapp_db-data
Output hidden (2 lines)
Server
$ docker compose up -d Network myapp_default Creating Network myapp_default Created Container myapp-db-1 Creating Container myapp-db-1 Created Container myapp-adminer-1 Creating Container myapp-adminer-1 Created Container myapp-db-1 Starting Container myapp-db-1 Started Container myapp-db-1 Waiting Container myapp-db-1 Healthy Container myapp-adminer-1 Starting Container myapp-adminer-1 Started
Output hidden (12 lines)
Server
$ docker compose exec db psql -U app -d app -c "SELECT * FROM notes;"  t------ kept(1 row)
Output hidden (4 lines)

New containers, same data. Only down -v deletes it (at the end).

Changing POSTGRES_PASSWORD later does nothing

The PostgreSQL image uses POSTGRES_PASSWORD only the first time, when the volume is empty. After that the password is stored inside the database. An app connects over the Compose network, as this one-off container does; with the current password it gets in:

Server
$ docker compose run --rm --no-deps db sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -h db -U app -d app -c "SELECT 1"' Container myapp-db-run-c2db3e2fbade Creating Container myapp-db-run-c2db3e2fbade Created ?column?----------        1(1 row)
Output hidden (6 lines)

Now give .env a new password and recreate the container:

Server
$ echo "DB_PASSWORD=$(openssl rand -hex 16)" > .env
Server
$ docker compose up -d Container myapp-adminer-1 Running Container myapp-db-1 Recreate Container myapp-db-1 Recreated Container myapp-db-1 Starting Container myapp-db-1 Started Container myapp-db-1 Waiting Container myapp-db-1 Healthy
Output hidden (7 lines)
Server
$ docker compose run --rm --no-deps db sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -h db -U app -d app -c "SELECT 1"' Container myapp-db-run-0c12784580b7 Creating Container myapp-db-run-0c12784580b7 Createdpsql: error: connection to server at "db" (172.18.0.2), port 5432 failed: FATAL:  password authentication failed for user "app"
Output hidden (3 lines)

The container has the new password in its environment, but the database still has the old one. Set it inside the database (the command reads the new value from the container’s environment, so the password is never typed):

Server
$ docker compose exec db sh -c "psql -U app -d app -c \"ALTER USER app PASSWORD '\$POSTGRES_PASSWORD'\""ALTER ROLE
Output hidden (1 line)
Server
$ docker compose run --rm --no-deps db sh -c 'PGPASSWORD="$POSTGRES_PASSWORD" psql -h db -U app -d app -c "SELECT 1"' Container myapp-db-run-58c2964e1f34 Creating Container myapp-db-run-58c2964e1f34 Created ?column?----------        1(1 row)
Output hidden (6 lines)

Step 5: Check it after a reboot

Server
$ sudo reboot
Server
$ docker compose psNAME              IMAGE                COMMAND                  SERVICE   CREATED          STATUS                   PORTSmyapp-adminer-1   adminer:5            "entrypoint.sh docke…"   adminer   36 seconds ago   Up 7 seconds             127.0.0.1:8080->8080/tcpmyapp-db-1        postgres:17-alpine   "docker-entrypoint.s…"   db        28 seconds ago   Up 7 seconds (healthy)   5432/tcp
Output hidden (3 lines)
Server
$ docker compose exec db psql -U app -d app -c "SELECT * FROM notes;"  t------ kept(1 row)
Output hidden (4 lines)

Both containers came back by themselves (restart: unless-stopped, and Docker starts at boot), with the data.

Update to newer images

Run the two commands from step 3 again: docker compose pull --quiet fetches newer images for the tags in the file, and docker compose up -d recreates the containers that changed, as it did above when .env changed. The named volume stays.

Remove everything, data included

Server
$ docker compose down -v Container myapp-adminer-1 Stopping Container myapp-adminer-1 Stopped Container myapp-adminer-1 Removing Container myapp-adminer-1 Removed Container myapp-db-1 Stopping Container myapp-db-1 Stopped Container myapp-db-1 Removing Container myapp-db-1 Removed Volume myapp_db-data Removing Network myapp_default Removing Volume myapp_db-data Removed Network myapp_default Removed
Output hidden (12 lines)
Server
$ docker volume ls -q | grep -c db-data0
Output hidden (1 line)

The configuration files, ready to copy

Both files as they were on the test server after the reboot, with the app running and its data kept.

compose.yaml
~/myapp/compose.yaml
services:  db:    image: postgres:17-alpine    restart: unless-stopped    environment:      POSTGRES_DB: app      POSTGRES_USER: app      POSTGRES_PASSWORD: ${DB_PASSWORD}    volumes:      - db-data:/var/lib/postgresql/data    healthcheck:      test: ["CMD-SHELL", "pg_isready -U app -d app"]      interval: 5s      timeout: 3s      retries: 10   adminer:    image: adminer:5    restart: unless-stopped    ports:      - "127.0.0.1:8080:8080"    depends_on:      db:        condition: service_healthy volumes:  db-data:

Read back from the test server (Ubuntu 26.04) on 2026-11-19, after the steps had worked. Placeholders such as [password]: type your own value in the box above the file to fill them in, or replace them yourself.

.env (make your own password with the command in step 2)

Optional · fill in your own values to copy this file as it is

Get it with cat ~/myapp/.env. It is the random password step 2 wrote into .env.

You can skip this: then replace [password] with your own value in each command and file below before you run it. A password typed here is kept only while this page is open, never saved or sent.

~/myapp/.env
DB_PASSWORD=[password]

Read back from the test server (Ubuntu 26.04) on 2026-11-19, after the steps had worked; the password was generated on the server with openssl rand -hex 16. Placeholders such as [password]: type your own value in the box above the file to fill them in, or replace them yourself.

Troubleshooting

  • password authentication failed for user "app" after changing .env: the database keeps its first password; set the new one with ALTER USER. Details.
  • The data is gone after down: it was not in a named volume, or down -v was used. Details.
  • The app starts before the database is ready: add a healthcheck and condition: service_healthy. Details.
  • The port is reachable from the internet although ufw blocks it: publish it on 127.0.0.1. Details.

What these steps changed on the server

Both runs recorded the server before the first step and after the reboot (see the Tested on box below): one new listening port, 127.0.0.1:8080 (Docker’s proxy for Adminer), and about 630 MB more disk used, mostly the two images. Nothing changed in /etc and no package was added after Docker itself.

Ubuntu 26.04 vs 24.04

The same check ran on a fresh 26.04 server and on a fresh 24.04 server, each with its own run log, with the same Docker and Compose versions from Docker’s repository. Every command printed the same result.

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
docker-ce5:29.10.0-1~ubuntu.26.04~resolute5:29.10.0-1~ubuntu.24.04~noble
docker-compose-plugin5.8.0-1~ubuntu.26.04~resolute5.8.0-1~ubuntu.24.04~noble
postgres-image17.1217.12
adminer-image5.5.15.5.1
Commands and resultsThe same on every release: the same commands, the same results.
ProofRun log, 29 checks passedRun log, 29 checks passed