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.
How it works
Section titled “How it works”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.
What goes into the endpoint
Section titled “What goes into the endpoint”Environment variables
Section titled “Environment variables”| Variable | Default | Meaning |
|---|---|---|
DDEV_SYNC_UID | 33 | The 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_GID | 33 | The same for the group id. |
DDEV_SYNC_AUTHORIZED_KEYS | The public keys that may log in, one per line. | |
DDEV_SYNC_EXPORT_ENV | The 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.
Mounts
Section titled “Mounts”| Mount point in the endpoint | What | Notes |
|---|---|---|
/data/… | What the sync pulls, such as app_storage:/data/storage:ro | Mount read-only (:ro). A sync only ever reads production. |
/data/.env | The stack’s environment file, ./.env:/data/.env:ro | The 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_keys | Optional file with public keys | Alternative or addition to DDEV_SYNC_AUTHORIZED_KEYS. |
/etc/ssh/keys | A named volume for the host key | Must 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. Underprovider: docker a redeploy that dropped the endpoint's host-key volumelooks exactly like this; check that the volume mounted at /etc/ssh/keyssurvived, 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:
ddev exec -s sync ssh-keygen -R ddev-sync-endpoint-prod.example-2222Targets 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.
What the image carries
Section titled “What the image carries”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.
Which names to list
Section titled “Which names to list”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:
| Framework | Variables |
|---|---|
| Laravel | DB_DATABASE, DB_USERNAME, DB_PASSWORD, DB_HOST, DB_PORT |
| Symfony and Shopware | DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, DATABASE_PORT |
| TYPO3 | TYPO3_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 own | DATABASE_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.
What the endpoint does with the list
Section titled “What the endpoint does with the list”- A mounted
.envwins. 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, oneNAME=valueper line, readable only by the user the session runs as (DDEV_SYNC_UIDandDDEV_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.
Handing over everything with all
Section titled “Handing over everything with all”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
PATHandHOSTNAMEincluded, written to the same file as a list would be. The endpoint’s ownDDEV_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
allit is skipped, and its name, never its value, is written into the file asDDEV_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 breakadded when M is not zero. No names. - Only the word.
allhas to be the whole value.all,DB_HOSTis a list of two names, one of them a variable calledall. A variable really namedallcan still be handed over this way. - What reaches a laptop. What a project lists in
env.pulland its profile’s defaults is copied; the rest is merely readable in the session. Only a project that setsenv.pull_allcopies all of it. - A mounted
.envhides it. As for the credentials, the sync reads the files under/data/first and uses the handover only when none exists. With a.envmounted, 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.
The target
Section titled “The target”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: productionhost 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: deployerand adeployer:block are refused, because the endpoint has noreleases/currentstructure to resolve. See Deployer. - No
jump_host. The connection already goes through the Docker host. - No
wp_clicredentials.database.remote_credentials: wp_cliis refused, because WP-CLI needs a PHP the endpoint deliberately does not carry. Explicit credentials together withdatabase.wp_table_prefixare not refused, but they are untested with WordPress. See Frameworks. sync_portgoes withprovider: docker. It is required there, and refused without it.
A self-signed database certificate
Section titled “A self-signed database certificate”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: falseLeave it out when your database does not need it.
Four compose examples
Section titled “Four compose examples”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.
1. In the stack
Section titled “1. In the stack”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:
COMPOSE_PROFILES=sync docker compose up -d ddev-syncTarget
targets: production: provider: docker host: example.com user: deploy sync_port: 2222 path: /data/ stage: productionWith a Laravel project, files.paths in the profile pulls storage/app/public/, which this mount layout places at /data/storage/app/public/.
2. A separate project
Section titled “2. A separate project”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.
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:
docker compose -f docker-compose.ddev-sync.yml up -dThe 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.
3. Behind a reverse proxy
Section titled “3. Behind a reverse proxy”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: productionThe .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.
4. Several projects on one host
Section titled “4. Several projects on one host”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: productionBoth 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.
Checking it
Section titled “Checking it”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.
Other shapes
Section titled “Other shapes”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.