4.2.6.2 CMD Runtime Override
A focused guide to CMD Runtime Override, connecting core concepts with practical Docker and container operations.
CMD runtime override is the ability to replace an image's default CMD entirely by specifying a different command when starting a container, without needing to rebuild or modify the image itself.
Overriding by Specifying a Command at Run Time
Any command specified after the image name in docker run completely replaces the image's CMD, rather than modifying or appending to it.
CMD ["python", "app.py"]
docker run myapp
docker run myapp python manage.py migrate
The second invocation runs an entirely different command, ignoring the image's default CMD completely.
Why This Override Mechanism Is Useful
A single image can serve multiple purposes depending on what command is supplied at run time — running the main application by default, while still supporting alternative invocations like database migrations or one-off administrative tasks using the same underlying image and dependencies.
docker run myimage python manage.py createsuperuser
Overriding in Compose and Orchestration Configuration
The same override mechanism is available when defining containers through docker compose or orchestration manifests, specifying a command that takes precedence over the image's own default.
services:
migrate:
image: myapp:1.0
command: ["python", "manage.py", "migrate"]
This reuses the exact same image as the main application service, but runs a different one-off command instead of the application's default startup behavior.
Interaction With ENTRYPOINT
If the image also defines an ENTRYPOINT, a runtime override of CMD only replaces the default arguments appended to that fixed entrypoint, rather than replacing the entire command — a distinction worth understanding before assuming an override behaves the same way regardless of whether ENTRYPOINT is present.
docker run myimage --different-flag
Why CMD Runtime Override Matters
This override capability is what allows a single, well-built image to serve as the basis for many different specific operations — running the application, running migrations, running diagnostic commands — all without maintaining separate images or rebuilding anything for each variation.