9.2.1.3 Service Command Field
A focused guide to Service Command Field, connecting core concepts with practical Docker and container operations.
The command field within a Compose service overrides the default command an image would otherwise run, useful for adjusting a service's behavior without needing to modify the underlying image itself.
Overriding an Image's Default Command
Specifying command replaces whatever CMD (or equivalent) the image itself defines.
services:
api:
image: node:20-alpine
command: npm run dev
Rather than whatever default command the base node:20-alpine image might otherwise run, this service specifically runs npm run dev.
Using Command for Different Behavior in Different Contexts
The same underlying image can be used for different purposes across different services simply by specifying a different command for each.
services:
web:
image: myapp:1.0
command: npm start
worker:
image: myapp:1.0
command: npm run worker
Both services share the identical image, but each runs an entirely different process, determined by their respective command field.
Specifying Command as a List Versus a String
command can be specified either as a single string or as a list of individual arguments, with the list form avoiding potential ambiguity around shell-style argument parsing.
services:
api:
command: ["npm", "run", "dev"]
Why Command Differs From Entrypoint
While command overrides the default arguments passed to a container's entrypoint, the entrypoint itself (the actual executable that ultimately runs) is a separate, related concept — command alone is sufficient for most cases where only the arguments need to differ from the image's default.
services:
api:
entrypoint: /custom-entrypoint.sh
command: --verbose
Why the Command Field Matters
The command field provides a simple, direct way to customize what a service actually runs without needing to maintain separate images for behavior that differs only in which specific command gets executed.