---
title: "Docker Best Practices: Choosing Between RUN, CMD, and ENTRYPOINT"
description: "RUN executes commands at image build time and creates layers, CMD sets a default command that any docker run arguments replace, and ENTRYPOINT fixes the executable so that run arguments are appended to it."
url: "https://swe.quest/articles/docker-best-practices-choosing-between-run-cmd-and-entrypoint-023cc3644bb13e6a"
canonical: "https://www.docker.com/blog/docker-best-practices-choosing-between-run-cmd-and-entrypoint/"
source: "Docker"
published: "2024-07-15"
difficulty: "medium"
kind: "How-to"
reading_minutes: 13
tags: ["containers","linux"]
---

# Docker Best Practices: Choosing Between RUN, CMD, and ENTRYPOINT

A post on [Docker](https://swe.quest/sources/docker.md), published 2024-07-15, 13 min read, How-to. Original: [https://www.docker.com/blog/docker-best-practices-choosing-between-run-cmd-and-entrypoint/](https://www.docker.com/blog/docker-best-practices-choosing-between-run-cmd-and-entrypoint/)

## Summary

RUN executes commands at image build time and creates layers, CMD sets a default command that any docker run arguments replace, and ENTRYPOINT fixes the executable so that run arguments are appended to it. Pairing an exec-form ENTRYPOINT with CMD as default arguments gives a container that acts like a standalone executable whose parameters users can override. Exec form is preferred for CMD and ENTRYPOINT because the process runs directly as PID 1 and receives signals such as SIGTERM, which allows graceful shutdown. Shell form runs under /bin/sh -c, which usually does not forward signals, so it fits only cases that need pipes, globbing or variable expansion. An ApacheBench example shows a URL argument overriding CMD and failing as a command.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Containers](https://swe.quest/tags/containers.md) (Infrastructure)
- [Linux](https://swe.quest/tags/linux.md) (Infrastructure)

## Prerequisites

- Dockerfile basics
- Unix processes and signals

## Keywords

Docker, Dockerfile, ENTRYPOINT, CMD, ApacheBench

## Related posts

- [Docker Best Practices: Using ARG and ENV in Your Dockerfiles](https://swe.quest/articles/docker-best-practices-using-arg-and-env-in-your-dockerfiles-97d31347b92062d6.md): Docker · 2024-10-16 · Medium · 6 min read · Containers, Configuration management
  ARG defines variables available only during the image build, while ENV defines variables available at build time and at container runtime.
- [Docker Best Practices: Understanding the Differences Between ADD and COPY Instructions in Dockerfiles](https://swe.quest/articles/docker-best-practices-understanding-the-differences-between-add-and-copy-4a75b284bd6c2baa.md): Docker · 2024-08-08 · Medium · 6 min read · Containers, Supply chain security
  Dockerfile COPY only copies files and directories from the build context into an image layer.
- [Introducing Docker Build Checks: Optimize Dockerfiles with Best Practices](https://swe.quest/articles/introducing-docker-build-checks-optimize-dockerfiles-with-best-practices-2400d0a2e13e5604.md): Docker · 2024-07-29 · Medium · 6 min read · Containers, Static analysis, CI/CD, Developer tools
  Docker Desktop 4.33 adds Build checks, which evaluate Dockerfiles against codified best-practice rules during \`docker build\` and report warnings.
- [Docker Best Practices: Using Tags and Labels to Manage Docker Image Sprawl](https://swe.quest/articles/docker-best-practices-using-tags-and-labels-to-manage-docker-image-sprawl-d8a774fa8d22d181.md): Docker · 2024-10-01 · Medium · 6 min read · Containers, Deployment
  Docker's \`latest\` tag does not mean the newest version: it is simply applied to the most recent build or push made without an explicit tag, and mutable tags like \`v1.0.0\` can be silently overwritten or mis-applied.
- [Coding Agent Horror Stories: The Command You Already Approved](https://swe.quest/articles/coding-agent-horror-stories-the-command-you-already-approved-c2fceff8d5f4f4cc.md): Docker · 2026-08-18 · Medium · 14 min read · AI agents, Sandboxing & isolation, Application security, AI for coding, Virtualization
  CVE-2026-22708 in Cursor let shell built-ins like \`export\`, \`typeset\` and \`declare\` run outside the Auto-Run allowlist, so injected text could silently set variables such as \`PAGER\`, \`PYTHONWARNINGS\` or \`PERL5OPT\` and turn an approved command like \`git branch\` into arbitrary code execution, even with an empty allowlist.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
