Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 103 of 8022. Docker Image LifecycleFree lesson

Defining Container Entrypoints and Default Commands

Learners will configure container initialization using the ENTRYPOINT and CMD instructions to create predictable and executable container behaviors.

In this lesson, you will learn how to make your containers act like predictable, standalone applications by defining exactly what runs when they start.

When you launch a container, it needs to know which process to execute. We define this behavior using two specific Dockerfile instructions: ENTRYPOINT and CMD.

The Core Difference

Think of ENTRYPOINT as the "main" command of your application—the thing that must run for your container to exist. CMD, on the other hand, provides the default arguments for that command. Because CMD is just an argument, a user can easily override it when they run the container, while the ENTRYPOINT remains fixed.

Practical Implementation

Consider a container that runs a simple greeting script. We use the "exec form" (brackets) to ensure our process runs as the main container process (PID 1):

# Define the fixed command
ENTRYPOINT ["/usr/bin/echo"]

# Define the default argument
CMD ["Hello, World!"]

If you run this container without extra arguments, it prints Hello, World!. However, because we used CMD for the message, a user can override it:

docker run my-image "Goodbye, World!"

In this case, the container ignores "Hello, World!" and prints "Goodbye, World!" instead. The ENTRYPOINT stays in charge, but the CMD provides the flexibility for users to change the input.

Why does this matter?

If you only used RUN (which we learned about in lesson 24), those commands would execute during the image build phase. By using ENTRYPOINT and CMD, you shift that execution to the container runtime phase. This allows your containers to remain lightweight and reusable; you build the image once, but you can change how it behaves the moment you start it, without rebuilding the entire package.

By separating the "what" (the binary or script in ENTRYPOINT) from the "how" (the defaults in CMD), you create containers that are robust, modular, and easy for other team members to interact with.

Key Takeaway: Use ENTRYPOINT to define the container’s primary executable and CMD to provide default arguments that users can easily override at runtime.