Docker Run ↔ Compose
Convert a docker run command into a compose.yaml service and back, with ports, environment, volumes, mounts, networks, restart policy and command intact, and a note for every flag Compose cannot express.
Runs entirely in your browser — nothing you paste is uploaded.
Forward and back conversions run in this browser tab; nothing you paste is uploaded.
services:
blog-db:
image: postgres:16-alpine
container_name: blog-db
ports:
- "5432:5432"
environment:
POSTGRES_USER: blog
POSTGRES_PASSWORD: change-me
POSTGRES_DB: blog
volumes:
- blog-db-data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
blog-db-data:Save this as compose.yaml and run it with docker compose up -d.
docker run to Compose cheat sheet
quick reference- docker run -d --name web -p 3000:3000 image
- The image is the first argument that is not a flag; everything after it is the command Compose writes under command:.
- -p 8080:80
- Compose splits the same value into host:container under ports:, and a leading host IP such as 127.0.0.1:8080:80 is kept as written.
- -e KEY=VALUE
- Repeated -e flags become an environment map. A bare -e KEY with no value turns the whole block into the list form Compose interpolates from the host.
- -v name:/path
- A named volume also needs the top-level volumes: entry that Compose writes for you; a path starting with / or . is a bind mount instead.
- --restart unless-stopped
- Restart policies carry over unchanged, and Compose itself stops a service with restart: "no" rather than the docker run --rm flag.
- docker compose up -d
- What replaces the docker run command: Compose runs the service detached, so the -d flag has no equivalent in the file.
Frequently asked questions
answered below the toolWhy is one of my flags in a note?
A few flags, such as --rm or --pull, have no compose.yaml equivalent, so they are listed instead of being dropped silently.
How do volumes become Compose entries?
A named volume also writes the top-level volumes: block, while a path starting with / or . becomes a bind mount.
Can I go from compose.yaml back to docker run?
Yes. Each service becomes one docker run command with its ports, environment, volumes, network and restart policy.
Related Docker syntax guides
All Docker guidesDocker: exec user process caused: exec format error
An arm64 image on an amd64 host is a runtime failure, not a pull failure. Diagnose the platform, build for the target, publish multi-arch, and spot the script variant.
DockerDocker Compose: bind: address already in use and Port Conflicts
A published port is a host socket, and two projects cannot own it. How to find the real owner, why an override file merged instead of replacing, and how to publish dynamically.
DockerAlpine musl vs Debian slim: DNS Timeouts and Native Builds
musl rejects manylinux wheels and linux-x64-gnu prebuilds, so the installer starts compiling without a toolchain. And ndots:5 in Kubernetes adds five seconds per lookup.
DockerDocker Compose Connection Refused: localhost vs Service DNS
localhost inside a container is that container. Compose resolves service names on a shared network, depends_on does not wait for readiness, and binding loopback breaks the rest.