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

