Docker Compose vs Docker Run: Convert Any Command to YAML

Updated 7 min read By the Self-Hosting HQ Editorial Team
Quick answerdocker run starts one container from flags you type each time; docker compose reads a compose.yaml file that declares one or more services, their ports, volumes, environment and restart policy, and starts them together with docker compose up -d. Use docker run for quick tests and one-off commands, and Compose for anything you want to keep running, update or rebuild later. Every common docker run flag has a direct compose.yaml equivalent.

Most self-hosted apps document a one-line docker run command, and most experienced users immediately turn it into a Compose file. The docker compose vs docker run question is less about features than about workflow: both talk to the same Docker Engine and create the same kind of container, but one keeps your configuration in your shell history while the other keeps it in a file you can version, back up and re-apply.

This guide explains when each is the right tool, gives a flag-by-flag mapping table, and walks through converting a real, officially documented docker run command into compose.yaml. Commands reflect Docker’s documentation as of December 2026 (Compose v5.6.0 is the version referenced on Docker’s Linux install page) and use the Compose plugin syntax, docker compose, with a space.

What each command does

docker run creates and starts a single container from an image. Everything about it, from ports to volumes to environment variables, is passed as flags on that one command line. Docker’s reference describes -d as “Run container in background and print container ID,” -p as “Publish a container’s port(s) to the host,” and --restart as the policy that brings a container back after it stops.

docker compose up reads a Compose file, usually compose.yaml in the current folder, and creates or updates every service defined in it. Docker’s reference for up notes that when a service’s configuration or image has changed, Compose “picks up the changes by stopping and recreating the containers,” while preserving mounted volumes. Compose also creates a project network automatically: per Docker’s networking guide, it creates <project-name>_default, attaches every service, and registers each service name in internal DNS so containers can reach each other by name, such as db.

When to use docker run vs Compose

Situation Better choice Why
Testing an image for two minutes docker run --rm Nothing to clean up afterward
One-off command (a CLI tool, a migration) docker run --rm It runs, prints output and disappears
A service you’ll keep for months Compose Config lives in a file you can back up and edit
An app with a database or several containers Compose One file, shared network, start/stop together
Updating to a new image version Compose pull then up -d recreates only what changed
Moving to a new server Compose Copy the folder and run up -d
Documenting your setup for future you Compose The file is the documentation

A good rule: if you’d be annoyed to retype the command after a reboot or a server rebuild, it belongs in a Compose file.

Flag-by-flag mapping: docker run to compose.yaml

docker run flag compose.yaml key Example
image:tag (last argument) image: image: nginx:stable
--name web container_name: (optional) container_name: web
-d docker compose up -d Detach is a command option, not a key
-p 127.0.0.1:8080:80 ports: - "127.0.0.1:8080:80"
-v /srv/app:/data volumes: - /srv/app:/data
-v appdata:/data (named volume) volumes: + top-level volumes: declare appdata: at the bottom
-e TZ=Europe/Berlin environment: TZ: Europe/Berlin
--env-file .env.app env_file: - .env.app
--restart unless-stopped restart: restart: unless-stopped
--network host network_mode: network_mode: host
--network mynet networks: list the network under the service
--privileged privileged: privileged: true
--device /dev/ttyUSB0 devices: - /dev/ttyUSB0:/dev/ttyUSB0
--cap-add NET_ADMIN cap_add: - NET_ADMIN
--stop-timeout 60 stop_grace_period: stop_grace_period: 60s
--user 1000:1000 user: user: "1000:1000"
arguments after the image command: command: ["--verbose"]
--rm no direct equivalent Compose containers are removed by docker compose down

Docker’s Compose reference lists the allowed restart values as no (the default), always, on-failure[:max-retries] and unless-stopped, the same set docker run --restart accepts.

Worked example: convert a real docker run command

Home Assistant documents both forms for its Container installation, which makes it a useful, verifiable example. Here is the docker run command from Home Assistant’s Linux installation page, with the time zone and config path filled in:

docker run -d \
  --name homeassistant \
  --privileged \
  --restart=unless-stopped \
  --stop-timeout 60 \
  -e TZ=Europe/Amsterdam \
  -v /srv/homeassistant/config:/config \
  -v /run/dbus:/run/dbus:ro \
  --network=host \
  ghcr.io/home-assistant/home-assistant:stable

Step 1: Pick out each flag

  1. Image: ghcr.io/home-assistant/home-assistant:stable becomes image:.
  2. Name: --name homeassistant becomes container_name: homeassistant.
  3. Privileges: --privileged becomes privileged: true.
  4. Restart policy: --restart=unless-stopped becomes restart: unless-stopped.
  5. Stop timeout: --stop-timeout 60 becomes stop_grace_period: 60s.
  6. Environment: -e TZ=... becomes an environment: entry.
  7. Volumes: each -v becomes a line under volumes:.
  8. Network: --network=host becomes network_mode: host.
  9. Detach: -d disappears from the file; you'll use docker compose up -d.

Step 2: Write compose.yaml

mkdir -p ~/stacks/homeassistant && cd ~/stacks/homeassistant
nano compose.yaml
services:
  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    volumes:
      - /srv/homeassistant/config:/config
      - /etc/localtime:/etc/localtime:ro
      - /run/dbus:/run/dbus:ro
    restart: unless-stopped
    stop_grace_period: 60s
    privileged: true
    network_mode: host
    environment:
      TZ: Europe/Amsterdam

This matches the Compose example Home Assistant publishes, which also mounts /etc/localtime read-only so the container uses the host’s clock settings. Note there’s no ports: section: with host networking the app listens directly on the host, so port mappings don’t apply.

Step 3: Replace the old container

If you already started it with docker run, remove that container first so the names don’t clash. The data in the bind-mounted config folder stays on disk:

docker stop homeassistant
docker rm homeassistant
docker compose up -d
docker compose ps
docker compose logs -f homeassistant

docker stop and docker rm remove only the old container, not /srv/homeassistant/config. docker compose up -d creates the new container in the background, ps confirms it’s running, and logs -f follows its output (press Ctrl+C to stop following).

Updating: where Compose saves the most time

With docker run, updating means pulling the new image, stopping and removing the container, and retyping the full command. With Compose it’s two commands inside the project folder:

docker compose pull
docker compose up -d

pull downloads newer versions of the images in the file; up -d recreates only the containers whose image or configuration changed, keeping mounted volumes. Before updating anything important, back up its data folder and read the app’s release notes.

Multi-container apps: where docker run gets awkward

Many self-hosted apps need a database, a cache or a background worker next to the main container. With docker run, you would create a user-defined network by hand, start each container with the right --network flag in the right order, and remember every command for the next rebuild. Compose handles this in one file: each service gets its own block, all of them join the project’s default network automatically, and they find each other by service name through Compose’s internal DNS. A web app can reach its database at the hostname db simply because the database service is called db.

The project name, which prefixes the network and container names, defaults to the folder that holds compose.yaml. Docker’s Compose FAQ explains that you can run a second copy of the same stack on one host by giving it a different project name with -p or the COMPOSE_PROJECT_NAME variable. That’s handy for testing an upgrade next to the live version, as long as the two copies don’t publish the same host ports.

Keep secrets such as database passwords out of the YAML itself. Put them in a .env file next to compose.yaml (Compose reads it for variable substitution) or an env_file:, restrict its permissions, and leave it out of any Git repository you push.

Port publishing: a security habit for both

Whether you use -p or ports:, Docker’s port-publishing documentation warns that “Publishing container ports is insecure by default,” because without a host address Docker publishes to all host interfaces. Prefix the mapping with an address:

    ports:
      - "127.0.0.1:8080:80"

That binds the port to the loopback interface so only the host can reach it, which is ideal when a reverse proxy on the same host is the only thing that should talk to the app. Use your server’s LAN IP instead if other devices on your network need access.

Don't forward admin UIsNever forward an app's admin page or the Docker API from your router to the internet, and don't mount /var/run/docker.sock into containers you don't fully trust. For remote use, put services behind a VPN, or behind a reverse proxy with TLS and strong authentication.

Common conversion mistakes

Mistake Symptom Fix
Tabs instead of spaces in YAML Parse error on up Indent with spaces only
Unquoted short port mappings such as 22:22 YAML 1.1 parsers can read xx:yy as a base-60 number Quote port mappings as strings
Host path without ./ or /, like data:/data Treated as a named volume (often an “undefined volume” error) Use ./data:/data or an absolute path
Old container with the same name still exists “container name already in use” docker rm the old container first
Keeping -d or --rm in command: Flags passed to the app, not Docker Remove them; they’re Docker options
Adding a version: line from an old guide Warning that it’s obsolete Delete it; Compose v2 ignores it

Next steps

Once your services live in Compose files, learn how stopping differs from tearing down in docker compose down vs stop, especially before using the -v flag. If docker compose isn’t recognized on your system, see docker compose command not found, or install it properly with our guide to installing Docker Compose on Ubuntu. More container guides are in the Docker section.

Frequently asked questions

Is docker compose slower or heavier than docker run?

No. Compose is a client that talks to the same Docker Engine; the containers it creates are ordinary containers. The difference is only how you describe and manage them.

Can I manage a container created with docker run using Compose?

Not directly. Compose only manages containers it created for a project. Stop and remove the old container, keep its data folder or volume, and start the service again with docker compose up -d.

Do I still need the version: line at the top of compose.yaml?

No. Docker's Compose FAQ says Compose v2 ignores the top-level version element, so you can leave it out of new files.

Should I set container_name in every service?

Only if you need a fixed name. Docker's reference notes that a service with container_name can't be scaled beyond one container, and Compose already gives containers predictable names based on the project and service.

Sources

  1. docker container run: Docker Docs
  2. docker compose up: Docker Docs
  3. Define services in Docker Compose: Docker Docs
  4. Networking in Compose: Docker Docs
  5. Port publishing and mapping: Docker Docs
  6. Linux (Home Assistant Container installation): Home Assistant
  7. Frequently asked questions (Compose): Docker Docs
Written and maintained by the Self-Hosting HQ Editorial Team

We research every guide from manufacturer documentation, government and standards bodies, and other primary sources, which are cited at the end of each article. We do not accept payment for coverage. Read our editorial policy to see how articles are researched, fact-checked and updated, or tell us if something is out of date.