IPv4 & IPv6 Leasing - Any RIR, Any LocationOrder Now
Hostperl

Docker Compose Secrets Rotation on Debian 12

By Raman Kumar

Share:

Updated on Nov 24, 2026

Docker Compose Secrets Rotation on Debian 12

Why this Docker Compose secrets rotation matters

If your app stores API keys, database passwords, or webhook tokens in Docker Compose, you should not need a full outage to rotate them. This guide walks you through Docker Compose secrets rotation on Debian 12 with a controlled cutover, rollback points, and verification checks that catch mistakes early.

The workflow is meant for a real VPS or dedicated host running production containers on Hostperl VPS hosting at Hostperl VPS. You will create a non-root admin, prepare a versioned Compose deployment, stage new secret values, restart services in a controlled order, and verify the app before removing the old secret material.

What you need before you start

  • A Debian 12 server with root SSH access.
  • A domain or hostname already pointed at the server if you will test the app over HTTP.
  • An application already deployed with Docker Compose under /opt/myapp.
  • A second terminal on your local computer for testing the new login.

This guide assumes Debian 12 because the package names, firewall commands, and systemd behavior are specific to that release family. If you are on a different Linux distribution, adjust the package manager and firewall steps, but keep the same order.

Connect to the server and confirm the operating system

On your local computer:

ssh root@203.0.113.10

203.0.113.10 is a documentation example. Replace it with the real public IP address assigned to your server.

After you log in, check the OS before changing anything else.

On the VPS as root:

cat /etc/os-release

You should see Debian 12 details such as VERSION_CODENAME=bookworm. If you do not, stop here and adapt the commands for your platform.

Create a non-root deploy user and keep root open

Do not close your root SSH session yet. Keep it open until the new login works from a second terminal.

On the VPS as root:

apt update
apt install -y sudo adduser openssh-server

This updates the package index and makes sure the sudo and SSH tools are installed.

adduser deploy

Set a strong password when prompted. Then add the user to the sudo group.

usermod -aG sudo deploy

Now prepare SSH keys for the new account.

mkdir -p /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
cp /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keys
chown -R deploy:deploy /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys

If your root account does not already use an SSH key, copy your public key into /home/deploy/.ssh/authorized_keys manually from your local computer instead of leaving password-only SSH in place.

On your local computer, open a second terminal:

ssh deploy@203.0.113.10

Again, replace the example IP with your real server address. Then test sudo.

On the VPS as the non-root sudo user:

sudo -iu deploy
sudo whoami

The final command should return root. Only after that works should you think about disabling root password login later.

Back up the current Compose project and secret files

Secrets rotation starts with a recovery point. Back up the active stack before you change anything.

On the VPS as the non-root sudo user:

sudo mkdir -p /opt/myapp-backups
sudo tar -czf /opt/myapp-backups/myapp-$(date +%F-%H%M%S).tar.gz /opt/myapp

This creates a timestamped archive of the current application directory. If you need to roll back, you can restore the old Compose file and secret files from this archive.

Review your current Compose structure.

cd /opt/myapp
pwd
ls -la

You should see your compose.yml or docker-compose.yml, any .env file, and application data directories.

Install Docker on Debian 12 if it is missing

If Docker is already installed and healthy, skip to the next section. Otherwise install the current Debian packages.

On the VPS as the non-root sudo user:

sudo apt update
sudo apt install -y ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Now add the Docker repository for Debian 12.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Confirm the versions and check that Compose is available.

docker --version
docker compose version

If you want to manage containers without sudo, add the deploy user to the docker group now and open a fresh shell.

sudo usermod -aG docker deploy

Define the secret rotation plan

Rotate one secret at a time so you can isolate failures. In production, the safest order is usually: application token first, database password second, and any downstream API keys last. That lets you prove the app can restart with the new value before you move to the next dependency.

For this tutorial, the app will use an environment file at /opt/myapp/.env. The new values are written to a replacement file first, then moved into place atomically.

Create the new secret file safely

On the VPS as the non-root sudo user:

cd /opt/myapp
sudo cp .env .env.old
sudo nano .env.new

Paste your updated environment content into the editor. A typical file might look like this:

APP_ENV=production
APP_KEY=replace-with-new-random-value
DATABASE_URL=postgresql://myapp:NEW_DATABASE_PASSWORD@db:5432/myapp
EXTERNAL_API_TOKEN=replace-with-new-token

Save and exit nano with Ctrl+O, Enter, then Ctrl+X.

Lock down the file permissions before it becomes active.

sudo chown root:root /opt/myapp/.env.new
sudo chmod 600 /opt/myapp/.env.new
sudo mv /opt/myapp/.env.new /opt/myapp/.env

Check that only root can read it.

sudo ls -l /opt/myapp/.env

You should see -rw------- permissions.

Update the Compose file for controlled restarts

Your Compose file should reference the environment file, expose only the ports you need, and keep secrets out of the file itself. Open it now.

On the VPS as the non-root sudo user:

cd /opt/myapp
sudo nano compose.yml

Use a structure like this if you need a template for one application container and one database container:

services:
  app:
    image: myorg/myapp:2026.1
    restart: unless-stopped
    env_file:
      - .env
    depends_on:
      - db
    ports:
      - "8000:8000"

  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: myapp
      POSTGRES_PASSWORD: ${DATABASE_URL_PASSWORD}
    volumes:
      - dbdata:/var/lib/postgresql/data

volumes:
  dbdata:

Save and exit nano. If your app already has a working Compose file, keep the service names and image tags already in production. Do not rename services during a secret rotation unless you also plan to migrate the data volume.

Before you touch the running stack, validate the Compose syntax.

docker compose config

If the file has a syntax error, Docker prints the line and stops. Fix the file before continuing.

Restart the app in a safe order

Restart the least critical service first if your application separates web, worker, and database roles. For a simple stack, bring the database up first, then the app.

On the VPS as the non-root sudo user:

docker compose pull
sudo docker compose up -d db
sudo docker compose up -d app

If your stack has workers, run them after the app container starts cleanly. If the app uses a single container only, restart that one container and watch the logs right away.

docker compose logs -f --tail=100 app

Watch for authentication errors, missing environment variables, or refused connections to the database. A clean startup ends with the application listening on its expected port.

Open the firewall before you close any existing access

If UFW is already in use, add the needed rule before changing SSH policy. Debian does not enable UFW by default, so this section only applies if you choose to use it.

On the VPS as root:

apt install -y ufw
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status verbose

Keep your current SSH session open while you test a second login. Remove old access paths only after the new one is confirmed.

Test the rotated secret from the application side

You want both server-side and client-side proof that the change worked.

On the VPS as the non-root sudo user:

docker compose ps
docker compose logs --tail=50 app

Then run a local health check from the server itself. Replace the path with your app's real health endpoint.

curl -fsS http://127.0.0.1:8000/health

On success, the command returns the health response and exits with status code 0.

On your local computer:

curl -fsS http://203.0.113.10:8000/health

Again, replace the documentation IP with your real server address. If you use a reverse proxy in front of the app, test the public hostname instead of the raw port.

Remove the old secret only after the new one is proven

Do not delete the old secret from backups until the new login has been exercised through the app. For example, log in as a real user, submit a form that touches the database, or trigger a webhook that uses the new token.

When the new value is stable, clear the old copy from your working directory and backups according to your retention policy. If you must keep an archive, store it offline and restrict access.

On the VPS as the non-root sudo user:

shred -u /opt/myapp/.env.old

shred is not a perfect guarantee on every storage device, but it is safer than a plain delete for local files. If your compliance policy requires a different disposal method, follow that policy instead.

Rollback if the new secret breaks production

If the app fails after the change, restore the previous file immediately and restart only the affected containers.

On the VPS as the non-root sudo user:

cd /opt/myapp
sudo cp .env.old .env
sudo docker compose up -d app
sudo docker compose logs --tail=50 app

If the rollback needs a full restore, untar the backup you created earlier and bring the stack back to the last known good state.

sudo tar -xzf /opt/myapp-backups/myapp-YYYY-MM-DD-HHMMSS.tar.gz -C /

Replace the archive name with the file you actually created. After a full restore, repeat docker compose config and docker compose up -d before you test again.

Check persistence after a reboot

Production changes are not finished until they survive a restart.

On the VPS as root or the non-root sudo user:

sudo reboot

Reconnect after the server comes back.

On your local computer:

ssh deploy@203.0.113.10

Then check that Docker brought the stack back automatically.

On the VPS as the non-root sudo user:

docker compose ps
docker compose logs --tail=30 app

If the containers are not running after reboot, confirm that the Docker service is enabled.

systemctl is-enabled docker
systemctl status docker --no-pager

Troubleshooting the most likely failures

1. Compose file syntax error

Diagnostic:

docker compose config

Expected clue: a line number or malformed YAML message.

Fix: edit compose.yml, correct indentation, and run the command again.

2. App starts but cannot read the new secret

Diagnostic:

docker compose logs --tail=100 app
sudo ls -l /opt/myapp/.env

Expected clue: permission denied, missing variable, or an empty environment value.

Fix: restore chmod 600 and make sure the file is owned by root:root.

3. Container cannot reach the database

Diagnostic:

docker compose logs --tail=100 db
docker compose exec app sh -lc 'nc -zv db 5432'

Expected clue: refused connection, wrong database password, or the database container restarting.

Fix: verify the database environment values, then restart the database container before the app.

4. SSH works as root but not as deploy

Diagnostic:

id deploy
sudo -l -U deploy
ls -ld /home/deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys

Expected clue: missing sudo group membership or bad SSH permissions.

Fix: rerun usermod -aG sudo deploy, then correct ownership and permissions on .ssh.

If you want help running container workloads with less guesswork, Hostperl VPS gives you a solid base for Docker Compose deployments, secret rotation, and rollback testing. For larger workloads or heavier database use, review Hostperl VPS hosting and Hostperl dedicated server hosting to match CPU, memory, and storage to the job.

Hostperl's support model fits teams that need practical recovery steps, not just a server login. That matters when you are changing live secrets and want a provider that understands launch windows, migration risk, and fast rollback.

Related Hostperl guides

If you are building a safer release process around this stack, these related guides may help:

Final check

A correct secret rotation ends with four things: the app starts cleanly, the health endpoint returns success, the server reboots without manual intervention, and the old secret is no longer sitting in the active config. If any one of those fails, treat the rotation as incomplete and roll back before users find the problem.

For production hosting on Hostperl, this is the kind of careful change that keeps deployments calm and support tickets low. A Hostperl VPS is a good fit for this workflow when you need controlled container releases without overbuying hardware.

FAQ

Can I rotate secrets without stopping Docker?
Yes, if your app reloads config cleanly and can reconnect to dependencies after a container restart. The safest path is still a controlled restart, not a blind in-place edit.

Should secrets live inside the Compose file?
No. Keep secrets in a locked-down environment file or an external secret store, and keep the Compose file free of sensitive values.

What if my app has both web and worker containers?
Restart the worker last. First confirm that the web container starts cleanly with the new secret, then let the worker resume queue processing.

How do I know the rollback is safe?
You already proved the old config works because you backed it up before the change. Restore it, restart the affected container, and run the same health check again.