liveBook · Manning

Cmd directive

As explained in Road to Kubernetes

CMD is a Dockerfile instruction that specifies the default command for a container when it starts.

CMD is a Dockerfile instruction that specifies the default command executed when a container starts. It runs at runtime, not while the image is being built, and commonly uses JSON array syntax. section 5.2.1 section 5.3.4

Use CMD to:

  • Run an application directly, such as NGINX with its foreground option. section 5.2.1
  • Launch an entrypoint script that can prepare runtime configuration before starting the application. section 5.3.4
  • Keep the startup command configurable through environment variables such as PORT. section 5.3.4

Starting applications through scripts

A Python image can copy and make an entrypoint script executable, then use CMD ["./entrypoint.sh"]. The script selects PORT or falls back to 8080 before launching Gunicorn. A Node.js image follows the same pattern with CMD ["./entrypoint.sh"]. section 5.3.4 section 5.4.2

Listing 5.2 Your first Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]

Inspect a minimal Dockerfile that installs NGINX and starts it with CMD in the foreground. section 5.2.1

Listing 5.10 Final Python Dockerfile
...
COPY ./src/ /app

# copy our local conf/entrypoint.sh to /app in the container
COPY ./conf/entrypoint.sh /app/entrypoint.sh
...
# Make our entrypoint script executable
RUN chmod +x /app/entrypoint.sh
...
# Execute our entrypoint script
CMD ["./entrypoint.sh"]

Inspect the complete Python pattern: copy the script, make it executable, and invoke it with CMD. section 5.3.4

How Road to Kubernetes covers it

How deep it goes
Explains direct commands and entrypoint scripts, stopping short of detailed ENTRYPOINT behavior.
Examples
Uses NGINX, Python with Gunicorn, and Node.js applications launched through shell scripts.
What it assumes
Assumes basic Dockerfile and container concepts while introducing runtime-versus-build execution and entrypoint scripts.
What you can do after
Builds Dockerfiles that start web applications directly or through configurable entrypoint scripts.
How it connects
Connects Dockerfile instructions with entrypoint scripts, environment variables, container images, and the ENTRYPOINT directive.

Also explained in

Compare how 2 books explain cmd directive →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.