TExcelTAD
Home › Docker Compose › command, entrypoint & user

command, entrypoint & user

An image decides what runs when a container starts: its ENTRYPOINT and CMD (see CMD and ENTRYPOINT in the Docker course). A Compose service can override both, and also choose the user, the working directory and how the process is stopped. These keys look trivial, yet they cause a surprising share of "works with docker run, fails in Compose" bugs, mostly because of how Compose turns a string into a command.

How the final command is built

The container runs ENTRYPOINT + CMD, as one argument list. Compose's keys replace the image's parts:

Compose key Replaces Note
command: the image's CMD arguments for the entrypoint, or the whole program if there is no entrypoint
entrypoint: the image's ENTRYPOINT setting it also drops the image's CMD, unless you set command too
entrypoint: "" or [] clears ENTRYPOINT then command is the whole program

The model applies those rules (they are Docker's rules; Compose just passes the values on):

python · try it yourself
def final_argv(image_entrypoint, image_cmd, entrypoint=None, command=None):
    if entrypoint is not None:
        ep = entrypoint
        cmd = command if command is not None else []      # a new entrypoint resets the image CMD
    else:
        ep = image_entrypoint
        cmd = command if command is not None else image_cmd
    return ep + cmd

postgres = (["docker-entrypoint.sh"], ["postgres"])
python_app = ([], ["gunicorn", "app:app"])

cases = [
    ("postgres, no overrides", postgres, {}),
    ("postgres, command: postgres -c log_statement=all", postgres,
     {"command": ["postgres", "-c", "log_statement=all"]}),
    ("postgres, entrypoint: [bash]", postgres, {"entrypoint": ["bash"]}),
    ("postgres, entrypoint: [] + command: [psql, --version]", postgres,
     {"entrypoint": [], "command": ["psql", "--version"]}),
    ("app, command: python -m worker", python_app, {"command": ["python", "-m", "worker"]}),
]
for label, (ep, cmd), overrides in cases:
    print(f"{label:55} -> {final_argv(ep, cmd, **overrides)}")
postgres, no overrides                                  -> ['docker-entrypoint.sh', 'postgres']
postgres, command: postgres -c log_statement=all        -> ['docker-entrypoint.sh', 'postgres', '-c', 'log_statement=all']
postgres, entrypoint: [bash]                            -> ['bash']
postgres, entrypoint: [] + command: [psql, --version]   -> ['psql', '--version']
app, command: python -m worker                          -> ['python', '-m', 'worker']

The third case is the classic surprise: overriding only the entrypoint of the Postgres image gives a bare bash without the postgres argument, because the image's CMD was dropped with its entrypoint.

Strings are split, not run by a shell

command and entrypoint accept a list (exec form) or a string. A string is split into words like a shell would split it, respecting quotes, and each word becomes an argument. It is not passed to /bin/sh. Shell operators such as &&, ; and | therefore don't work, and Compose silently drops everything from the operator on:

compose · try it yourself
services:
  build-and-run:
    image: node:24-alpine
    command: npm run build && npm start
  pipe:
    image: alpine:3.22
    command: cat /etc/hosts | grep db
  quoted:
    image: alpine:3.22
    command: echo "two words" 'and more'
name: demo
services:
  build-and-run:
    command:
      - npm
      - run
      - build
    image: node:24-alpine
    networks:
      default: null
  pipe:
    command:
      - cat
      - /etc/hosts
    image: alpine:3.22
    networks:
      default: null
  quoted:
    command:
      - echo
      - two words
      - and more
    image: alpine:3.22
    networks:
      default: null
networks:
  default:
    name: demo_default

build-and-run would run npm run build and exit, never starting the app, and pipe would print the whole file. When you need shell features, call a shell explicitly. The cleanest forms are a list with sh -c, or a YAML literal block for several lines:

compose · try it yourself
services:
  one-liner:
    image: node:24-alpine
    command: ["sh", "-c", "npm run build && exec npm start"]
  script:
    image: alpine:3.22
    command:
      - sh
      - -c
      - |
        echo "waiting for config"
        until [ -f /config/ready ]; do sleep 1; done
        exec my-server --config /config/app.toml
name: demo
services:
  one-liner:
    command:
      - sh
      - -c
      - npm run build && exec npm start
    image: node:24-alpine
    networks:
      default: null
  script:
    command:
      - sh
      - -c
      - |
        echo "waiting for config"
        until [ -f /config/ready ]; do sleep 1; done
        exec my-server --config /config/app.toml
    image: alpine:3.22
    networks:
      default: null
networks:
  default:
    name: demo_default

Use exec for the last program so that it replaces the shell and becomes PID 1. Otherwise the shell receives the stop signal, may not forward it, and docker compose stop waits the full grace period before killing the container.

Interpolation vs. the container's shell

A $ in a command is first seen by Compose (interpolation, on your machine), then possibly by a shell in the container. Decide which one you mean:

compose · try it yourself
services:
  greet:
    image: alpine:3.22
    command: ["sh", "-c", "echo deployed $RELEASE by $USER on $(hostname)"]
# --- .env
RELEASE=2026.10.1
name: demo
services:
  greet:
    command:
      - sh
      - -c
      - echo deployed 2026.10.1 by $USER on $(hostname)
    image: alpine:3.22
    networks:
      default: null
networks:
  default:
    name: demo_default

$RELEASE was filled in from .env by Compose. $USER and $(hostname) are escaped, so the container's shell gets $USER and $(hostname) and expands them at run time. (The output shows them as $ because config keeps its output a valid Compose file.)

Clearing the entrypoint

Some images have an entrypoint you want to bypass for a one-off task. entrypoint: [] (or "") clears it:

compose · try it yourself
services:
  debug-shell:
    image: postgres:18
    entrypoint: []
    command: ["bash"]
  migrate:
    image: shop/api:1.4
    entrypoint: ["/usr/local/bin/tini", "--"]
    command: ["python", "manage.py", "migrate"]
name: demo
services:
  debug-shell:
    command:
      - bash
    entrypoint: []
    image: postgres:18
    networks:
      default: null
  migrate:
    command:
      - python
      - manage.py
      - migrate
    entrypoint:
      - /usr/local/bin/tini
      - --
    image: shop/api:1.4
    networks:
      default: null
networks:
  default:
    name: demo_default

From the CLI, docker compose run --entrypoint bash db does the same for a single run.

user, working_dir and friends

compose · try it yourself
services:
  app:
    image: node:24-alpine
    user: "${UID:-1000}:${GID:-1000}"
    working_dir: /app
    init: true
    stop_signal: SIGINT
    stop_grace_period: 1m30s
    tty: true
    stdin_open: true
name: demo
services:
  app:
    image: node:24-alpine
    init: true
    networks:
      default: null
    stop_grace_period: 1m30s
    stop_signal: SIGINT
    user: 1000:1000
    working_dir: /app
    stdin_open: true
    tty: true
networks:
  default:
    name: demo_default
Key What it does
user run as this user: name, uid, uid:gid or name:group; overrides the Dockerfile's USER
working_dir the start directory (overrides WORKDIR)
init: true run a tiny init (tini) as PID 1 that forwards signals and reaps zombie processes
stop_signal signal sent by stop/down (default SIGTERM; Node dev servers often prefer SIGINT)
stop_grace_period how long to wait before SIGKILL (default 10s)
tty, stdin_open like docker run -t -i; needed for interactive REPLs attached with docker compose attach

user: "${UID:-1000}:${GID:-1000}" is a common development trick: files the container writes into a bind mount belong to you, not to root. Bash defines UID but does not export it, so run export UID GID=$(id -g) first or put the values in .env; otherwise the default applies, as it did above. Numeric IDs work even if the image has no such user; a user name must exist in the image's /etc/passwd.

Stopping: a model of the grace period

docker compose stop sends stop_signal, waits up to stop_grace_period, then sends SIGKILL. A process that ignores the signal (often a shell that doesn't forward it) costs the full wait on every restart:

python · try it yourself
def stop(service, handles_signal, shutdown_seconds, grace=10):
    if handles_signal and shutdown_seconds <= grace:
        return f"{service:22} exited cleanly after {shutdown_seconds}s"
    return f"{service:22} killed (SIGKILL) after {grace}s"

print(stop("api (exec gunicorn)", True, 2))
print(stop("api (sh -c, no exec)", False, 2))
print(stop("worker (slow drain)", True, 25))
print(stop("worker (grace 30s)", True, 25, grace=30))
api (exec gunicorn)    exited cleanly after 2s
api (sh -c, no exec)   killed (SIGKILL) after 10s
worker (slow drain)    killed (SIGKILL) after 10s
worker (grace 30s)     exited cleanly after 25s

Learn more: Healthchecks · Compose Security · Pods and commands in Kubernetes

Test yourself

What does command: npm run build && npm start run?

  1. Both commands through /bin/sh
  2. Only npm run build: the string is split into words and the && part is dropped
  3. An error at config time
  4. npm with the arguments run build && npm start

You set only entrypoint: [bash] on the postgres image. What runs?

  1. bash postgres
  2. bash
  3. docker-entrypoint.sh bash
  4. postgres

How do you make $USER expand inside the container rather than in Compose?

  1. Write $USER
  2. Write $USER
  3. Use single quotes
  4. It always expands in the container