Skip to content

Docker endpoint

When production runs in Docker, a developer usually has an SSH login on the Docker host and nothing else: no rights on the Docker daemon, and no way to reach the database that lives on the stack’s private network. The sync endpoint is a small container that whoever deploys the stack adds to it. It runs an SSH server, carries the tools a sync needs on the far side (rsync and a MariaDB client), and reads the stack’s own .env for the database credentials. A developer then syncs through ordinary SSH to the Docker host and never needs Docker rights.

The image is noixdev/ddev-sync-endpoint. It has versioned tags only and no latest: pin the tag, for example noixdev/ddev-sync-endpoint:0.1.0-rc.8, so a redeploy cannot change what listens in your stack.

The endpoint publishes SSH on a port of the Docker host’s own loopback address, 127.0.0.1. ddev-sync reaches it with a jump through the host: it connects to your Docker host with the host and user you already use, and from there to 127.0.0.1:<sync_port>, logging in as the endpoint’s sync account. Nothing is exposed on a public interface, and no new firewall rule is needed.

The Docker host’s own SSH server has to allow forwarding, because the last hop is a forward opened on it. Many hardened images ship AllowTcpForwarding no, and ddev-sync names it when that is the refusal:

the host in between refused to forward the connection. A jump host needs
'AllowTcpForwarding yes' in its sshd_config; many hardened images ship 'no'.

Everything the endpoint serves is mounted into it, usually under /data/. The target’s path is a path inside the endpoint, so it names those mount points, not directories on the host.

VariableDefaultMeaning
DDEV_SYNC_UID33The numeric user id the session runs as. Set it to the id that owns the files you mount, normally the application’s. An unset or empty value means 33; a name, a non-integer or 0 stops the endpoint.
DDEV_SYNC_GID33The same for the group id.
DDEV_SYNC_AUTHORIZED_KEYSThe public keys that may log in, one per line.
DDEV_SYNC_EXPORT_ENVThe names of environment variables to hand to the sync, for a stack with no .env to mount, or the one word all. See Credentials from the stack’s environment and Handing over everything with all.

Use DDEV_SYNC_UID and DDEV_SYNC_GID, not compose’s user:. An SSH server cannot serve a uid that has no account entry, so a user: the image was not built for exits at once. These two variables are written into the account at start, which works for any id. If the ids do not match the files, the sync fails halfway through an rsync, and ddev sync doctor says so. The application’s ids are usually 33 on a Debian-based image and often 82 on an Alpine-based one.

Keys can come from the variable, from a mounted file, or from both, in which case the endpoint reads their union. With neither it refuses to start, and it also refuses lines that are not valid public keys (for instance a key pasted with Windows line endings). There is no mode in which it accepts anyone: it takes keys only, and no passwords.

To revoke someone, remove their key and redeploy or restart the endpoint, which reads the keys when it starts. Nothing else holds their access.

Mount point in the endpointWhatNotes
/data/…What the sync pulls, such as app_storage:/data/storage:roMount read-only (:ro). A sync only ever reads production.
/data/.envThe stack’s environment file, ./.env:/data/.env:roThe endpoint reads the database credentials from here. They resolve on the stack’s network, so a host such as db works. Without a .env, see Credentials from the stack’s environment.
/etc/ddev-sync/authorized_keysOptional file with public keysAlternative or addition to DDEV_SYNC_AUTHORIZED_KEYS.
/etc/ssh/keysA named volume for the host keyMust persist. See below.

If it does happen, the sync says what to look at:

the sync endpoint's host key is not the one recorded for it. Under
provider: docker a redeploy that dropped the endpoint's host-key volume
looks exactly like this; check that the volume mounted at /etc/ssh/keys
survived, then remove the stale known_hosts entry.

Each developer then removes the stale entry once. It is recorded under the name ddev-sync-endpoint-<host>-<sync_port>, with the values from the target, and under the add-on the known-hosts file lives in the sync container, so the command runs there:

Terminal window
ddev exec -s sync ssh-keygen -R ddev-sync-endpoint-prod.example-2222

Targets has this beside the other ways a host key can refuse a connection.

Publish the SSH port on the loopback address only, and keep the 127.0.0.1: prefix:

ports:
- "127.0.0.1:2222:22"

The number on the left is what your target calls sync_port.

The endpoint carries an SSH server, rsync and a MariaDB client, and deliberately nothing else: no Docker client, so it has nothing to say to the Docker daemon; no rclone, because object storage is pulled from the developer’s side; no PHP; and no SSH client, so it cannot hop onwards. It reports its own health, so a stack whose SSH server died stops showing as Up.

Credentials from the stack’s environment

Section titled “Credentials from the stack’s environment”

Not every stack has a .env to mount. A stack deployed from Portainer, for one, usually holds its variables in the stack’s own settings, and there is no file beside the compose file to point the endpoint at. The application gets its credentials as environment variables, and so can the endpoint, but there is a catch: the SSH server starts each session with a clean environment, so a variable set on the endpoint container is invisible to the sync unless the endpoint is told to pass it on.

DDEV_SYNC_EXPORT_ENV does that. List the names of the variables, separated by commas or spaces, and the endpoint writes them to a file at start that the sync reads. Give the variables their values the way you would for any other service. In a Portainer stack, pass the stack’s variables to the endpoint exactly as you do for the application, so both services get them:

services:
app:
image: registry.example.com/example/app:1.4.0
environment:
DB_HOST: db
DB_PORT: "3306"
DB_DATABASE: ${DB_DATABASE}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
environment:
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
DDEV_SYNC_EXPORT_ENV: "DB_HOST,DB_PORT,DB_DATABASE,DB_USERNAME,DB_PASSWORD"
DB_HOST: db
DB_PORT: "3306"
DB_DATABASE: ${DB_DATABASE}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}

With a plain compose file whose variables live in a file that is not named .env, use env_file: on the endpoint and keep only the list:

ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
env_file:
- ./app.env
environment:
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
DDEV_SYNC_EXPORT_ENV: "DB_HOST,DB_PORT,DB_DATABASE,DB_USERNAME,DB_PASSWORD"

In both cases leave out the /data/.env mount. The target needs no change.

List the variables the project’s profile reads. They are the same ones a .env would hold, and all five must be handed over and non-empty, the port included: set it to 3306 even if your stack leaves it out. The sync uses the names of the profile your project runs, as below:

FrameworkVariables
LaravelDB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_HOST, DB_PORT
Symfony and ShopwareDATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, DATABASE_PORT
TYPO3TYPO3_DB_NAME, TYPO3_DB_USER, TYPO3_DB_PASSWORD, TYPO3_DB_HOST, TYPO3_DB_PORT
Any other (generic, Drupal), once the project reads credentials from the environment with database.remote_credentials: env and no env_map of its ownDATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, DATABASE_PORT

If your stack names them differently from that profile, or the project runs a different profile than the stack’s naming suggests, list the names the stack uses and map them with database.env_map in the project file, as for any remote environment file.

  • A mounted .env wins. The sync looks at the files under /data/ first and uses the handover only when none of them exists. With both in place, the mounted file is the one read.
  • The file. The endpoint writes the variables to /run/ddev-sync/app.env, one NAME=value per line, readable only by the user the session runs as (DDEV_SYNC_UID and DDEV_SYNC_GID). It is rewritten at every start, and an endpoint restarted without the list leaves no file behind.
  • The log. The endpoint says how many variables it handed over, never their names or values: handing over 5 variables to the sync session.
  • What it refuses. A name that is not a plain variable name (letters, digits and underscores, not starting with a digit, and not one of the endpoint’s own DDEV_SYNC_ settings), a listed variable that is not set on the endpoint, and a value that contains a line break each stop the endpoint with a message that says which. A listed variable that is set but empty does not stop it; the sync then reports it as missing, as for a .env.

The credentials are as exposed as those in a mounted .env: anyone who holds a key in the endpoint’s authorized_keys can reach them, which the box at the top of this page already assumes.

A project can ask the sync to copy more than the database credentials into a developer’s local file, with env.pull in Configuration. Under provider: docker those values have to be handed over by the endpoint too, and listing each new name in the stack every time a project needs one more is upkeep. Setting the variable to the single word all ends that:

environment:
DDEV_SYNC_EXPORT_ENV: all
  • What it hands over. Every variable in the container’s environment, image and Docker defaults such as PATH and HOSTNAME included, written to the same file as a list would be. The endpoint’s own DDEV_SYNC_ settings are left out, authorized keys included, and so is any variable whose name is not a plain variable name (letters, digits and underscores, not starting with a digit).
  • What it leaves out. A value with a line break cannot be carried in that file. Under a list it stops the endpoint; under all it is skipped, and its name, never its value, is written into the file as DDEV_SYNC_SKIPPED=NAME,NAME. A sync that pulls one of those names says so.
  • The log. handing over N variables to the sync session, with , skipped M with a line break added when M is not zero. No names.
  • Only the word. all has to be the whole value. all,DB_HOST is a list of two names, one of them a variable called all. A variable really named all can still be handed over this way.
  • What reaches a laptop. What a project lists in env.pull and its profile’s defaults is copied; the rest is merely readable in the session. Only a project that sets env.pull_all copies all of it.
  • A mounted .env hides it. As for the credentials, the sync reads the files under /data/ first and uses the handover only when none exists. With a .env mounted, a pulled name has to be in that file, and a name only the endpoint has is reported as missing, with a message saying that the mount wins.

The first release that understands all is v0.1.0-rc.6, for the add-on and the endpoint image alike. An older endpoint reads the word as the name of a variable and refuses to start with all is not set, so bump the image tag together with the add-on.

On the developer’s side, the target says that production is a Docker stack and where its endpoint listens:

targets:
production:
provider: docker
host: prod.example.com # the Docker host, as you connect to it today
user: deploy # your login on the Docker host
sync_port: 2222 # the port the stack publishes
path: /data/ # the path inside the endpoint
stage: production

host and user belong in your global file, as for any target (see Targets); path, sync_port and stage are what the team shares. ddev-sync supplies the endpoint’s own account, sync, and the loopback address itself.

A Docker target has these restrictions:

  • No Deployer. layout: deployer and a deployer: block are refused, because the endpoint has no releases/current structure to resolve. See Deployer.
  • No jump_host. The connection already goes through the Docker host.
  • No wp_cli credentials. database.remote_credentials: wp_cli is refused, because WP-CLI needs a PHP the endpoint deliberately does not carry. Explicit credentials together with database.wp_table_prefix are not refused, but they are untested with WordPress. See Frameworks.
  • sync_port goes with provider: docker. It is required there, and refused without it.

The endpoint’s MariaDB client, from version 11.4 on, checks the database server’s TLS certificate. Against MariaDB that works. Against a MySQL server with its own self-signed certificate it refuses with “self-signed certificate in certificate chain”. If you see that, give up the check for this connection, on purpose:

database:
verify_server_cert: false

Leave it out when your database does not need it.

All four use the image noixdev/ddev-sync-endpoint:0.1.0-rc.8 and the names app, storage and example.com. Replace them with yours. In each, the application stores what the sync pulls in a volume called storage, and keeps its settings in the .env beside the compose file. The stack’s .env must hold the database credentials the application uses, and be readable by the uid you set. The application’s own variables are left out to keep the examples short.

The most common setup: the endpoint is one more service of the production docker-compose.yml, switched off unless you ask for it with a compose profile. It shares the stack’s network, so db resolves, and it mounts the application’s storage volume.

services:
app:
image: registry.example.com/example/app:1.4.0
restart: unless-stopped
volumes:
- storage:/var/www/html/storage
db:
image: mariadb:11.4
restart: unless-stopped
environment:
MARIADB_DATABASE: app
MARIADB_USER: app
MARIADB_PASSWORD: ${DB_PASSWORD}
MARIADB_RANDOM_ROOT_PASSWORD: "1"
volumes:
- dbdata:/var/lib/mysql
# The sync endpoint. Off unless the `sync` profile is enabled.
ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
profiles: ["sync"]
restart: unless-stopped
ports:
- "127.0.0.1:${DDEV_SYNC_PORT:-2222}:22"
environment:
# The uid and gid that own the storage volume's files.
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
volumes:
- storage:/data/storage:ro
- ${DDEV_SYNC_ENV_FILE:-./ddev-sync/.env}:/data/.env:ro
- ${DDEV_SYNC_KEYS_FILE:-./ddev-sync/authorized_keys}:/etc/ddev-sync/authorized_keys:ro
- ddev_sync_hostkeys:/etc/ssh/keys
volumes:
storage:
dbdata:
ddev_sync_hostkeys:

Next to the compose file, create ddev-sync/.env with the database lines of the stack’s environment (it must be readable by DDEV_SYNC_UID) and ddev-sync/authorized_keys with one public key per line. The paths come from DDEV_SYNC_ENV_FILE and DDEV_SYNC_KEYS_FILE, so a deployment tool can point them elsewhere. No .env to copy? Drop that mount and list the variables in DDEV_SYNC_EXPORT_ENV instead; see Credentials from the stack’s environment. Start only the endpoint with:

Terminal window
COMPOSE_PROFILES=sync docker compose up -d ddev-sync

Target

targets:
production:
provider: docker
host: example.com
user: deploy
sync_port: 2222
path: /data/
stage: production

With a Laravel project, files.paths in the profile pulls storage/app/public/, which this mount layout places at /data/storage/app/public/.

When you would rather not touch the production stack’s file, put the endpoint in a compose file of its own, beside it. It reaches the stack’s volumes and network by their full names. Compose prefixes names with the project name, which is the directory name unless you set another, so a stack in /srv/app has a network app_default and a volume app_storage. Check with docker volume ls and docker network ls.

docker-compose.ddev-sync.yml
name: app-sync
services:
ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
restart: unless-stopped
ports:
- "127.0.0.1:2222:22"
networks:
- stack
environment:
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
volumes:
- app_storage:/data/storage:ro
- ./.env:/data/.env:ro
- ./authorized_keys:/etc/ddev-sync/authorized_keys:ro
- hostkeys:/etc/ssh/keys
networks:
stack:
external: true
name: app_default
volumes:
app_storage:
external: true
hostkeys:

Without a .env beside it, drop the ./.env mount and list the credential variables in DDEV_SYNC_EXPORT_ENV instead; see Credentials from the stack’s environment. The stack’s network is declared external, so db resolves exactly as it does for the application, and the stack’s volume is external too, so the endpoint reads the same files the application writes. The host-key volume is new and belongs to this project. Start it separately from the stack:

Terminal window
docker compose -f docker-compose.ddev-sync.yml up -d

The endpoint must come up after the stack has created its network and volume, since both have to exist. The target is the same as in example 1.

A reverse proxy routes HTTP by host name. The endpoint is SSH, which the proxy never routes, so the proxy plays no part in reaching it. What matters is that the endpoint stays off the proxy:

  • Under Traefik, label the endpoint with traefik.enable=false, and keep it off the proxy’s network. If Traefik is configured to expose containers by default, the label is what keeps it out.
  • With a proxy on the host (nginx, Caddy, Apache), nothing changes: the proxy forwards the web ports and ignores the loopback SSH port.

In both cases the port stays published on 127.0.0.1 only. Developers reach it through SSH to the Docker host, which is exactly what the target does.

services:
app:
image: registry.example.com/example/app:1.4.0
restart: unless-stopped
networks:
- default
- traefik
volumes:
- storage:/var/www/html/storage
labels:
traefik.enable: "true"
traefik.docker.network: traefik
traefik.http.routers.app.rule: Host(`example.com`)
traefik.http.routers.app.entrypoints: websecure
traefik.http.routers.app.tls: "true"
traefik.http.services.app.loadbalancer.server.port: "8080"
db:
image: mariadb:11.4
restart: unless-stopped
environment:
MARIADB_DATABASE: app
MARIADB_USER: app
MARIADB_PASSWORD: ${DB_PASSWORD}
MARIADB_RANDOM_ROOT_PASSWORD: "1"
volumes:
- dbdata:/var/lib/mysql
ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
profiles: ["sync"]
restart: unless-stopped
# SSH is not HTTP: the proxy must not see this container at all.
labels:
traefik.enable: "false"
ports:
- "127.0.0.1:${DDEV_SYNC_PORT:-2222}:22"
# Only the stack's own network, where `db` lives. Not `traefik`.
networks:
- default
environment:
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
volumes:
- storage:/data/storage:ro
- ${DDEV_SYNC_ENV_FILE:-./ddev-sync/.env}:/data/.env:ro
- ${DDEV_SYNC_KEYS_FILE:-./ddev-sync/authorized_keys}:/etc/ddev-sync/authorized_keys:ro
- ddev_sync_hostkeys:/etc/ssh/keys
networks:
traefik:
external: true
volumes:
storage:
dbdata:
ddev_sync_hostkeys:

Target

targets:
production:
provider: docker
host: example.com
user: deploy
sync_port: 2222
path: /data/
stage: production

The .env mount can likewise give way to DDEV_SYNC_EXPORT_ENV; see Credentials from the stack’s environment.

Note that host is the machine you SSH to. It may be the same name that the proxy serves the website on, but the sync does not go through the website.

Two stacks on the same Docker host each get their own endpoint, on their own loopback port. Each endpoint has its own keys, its own host-key volume and its own view of its own stack, so a key for one stack opens nothing in the other: the stack is the authorisation boundary.

# /srv/shop/docker-compose.yml (ports 2222)
services:
app:
image: registry.example.com/example/shop:2.0.0
restart: unless-stopped
volumes:
- storage:/var/www/html/storage
db:
image: mariadb:11.4
restart: unless-stopped
environment:
MARIADB_DATABASE: shop
MARIADB_USER: shop
MARIADB_PASSWORD: ${DB_PASSWORD}
MARIADB_RANDOM_ROOT_PASSWORD: "1"
volumes:
- dbdata:/var/lib/mysql
ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
profiles: ["sync"]
restart: unless-stopped
ports:
- "127.0.0.1:2222:22"
environment:
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
volumes:
- storage:/data/storage:ro
- ./ddev-sync/.env:/data/.env:ro
- ./ddev-sync/authorized_keys:/etc/ddev-sync/authorized_keys:ro
- ddev_sync_hostkeys:/etc/ssh/keys
volumes:
storage:
dbdata:
ddev_sync_hostkeys:
# /srv/blog/docker-compose.yml (ports 2223)
services:
app:
image: registry.example.com/example/blog:5.2.0
restart: unless-stopped
volumes:
- storage:/var/www/html/storage
db:
image: mariadb:11.4
restart: unless-stopped
environment:
MARIADB_DATABASE: blog
MARIADB_USER: blog
MARIADB_PASSWORD: ${DB_PASSWORD}
MARIADB_RANDOM_ROOT_PASSWORD: "1"
volumes:
- dbdata:/var/lib/mysql
ddev-sync:
image: noixdev/ddev-sync-endpoint:0.1.0-rc.8
profiles: ["sync"]
restart: unless-stopped
ports:
- "127.0.0.1:2223:22"
environment:
DDEV_SYNC_UID: "33"
DDEV_SYNC_GID: "33"
volumes:
- storage:/data/storage:ro
- ./ddev-sync/.env:/data/.env:ro
- ./ddev-sync/authorized_keys:/etc/ddev-sync/authorized_keys:ro
- ddev_sync_hostkeys:/etc/ssh/keys
volumes:
storage:
dbdata:
ddev_sync_hostkeys:

Where a stack has no .env, each endpoint can hand over its credentials with DDEV_SYNC_EXPORT_ENV instead of the .env mount; see Credentials from the stack’s environment.

The directories give the two stacks different compose project names, so the storage, dbdata and ddev_sync_hostkeys volumes do not collide although they share names. Only the published ports must differ. The two targets, in the DDEV project of each site (shown together here):

targets:
shop:
provider: docker
host: example.com
user: deploy
sync_port: 2222
path: /data/
stage: production
blog:
provider: docker
host: example.com
user: deploy
sync_port: 2223
path: /data/
stage: production

Both endpoints answer at 127.0.0.1 of the same Docker host, so ddev-sync gives each one its own name in known_hosts, built from the host and the port (HostKeyAlias). Two endpoints with two different host keys therefore never look like one host whose key changed.

ddev sync doctor checks the connection to the endpoint, reports the path it reads there as “in the endpoint”, and tells you when the files cannot be read because the uid is wrong. If it reports a changed host key, the host-key volume was dropped or replaced by a redeploy; see Targets for where the remembered keys live.

A bastion in front of the Docker host would be a second hop, and provider: docker does not support it yet: a target that sets jump_host together with it is refused. Swarm, Kubernetes and a Docker host without SSH access are not covered either; the endpoint expects an SSH login on a plain Docker host.

  • Sanitizing: what happens to production data once it is on your machine.
  • Syncing: choose what a run does.