---
title: "Using the Dockerfile ENTRYPOINT and CMD Instructions"
description: "Learn how to use Dockerfile ENTRYPOINT and CMD instructions to run startup commands in Docker containers in this tutorial!"
canonical: "https://adamtheautomator.com/dockerfile-entrypoint/"
---

# Using the Dockerfile ENTRYPOINT and CMD Instructions

> Learn how to use Dockerfile ENTRYPOINT and CMD instructions to run startup commands in Docker containers in this tutorial!

Source: https://adamtheautomator.com/dockerfile-entrypoint/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Using the Dockerfile ENTRYPOINT and CMD Instructions](https://adamtheautomator.com/wp-content/uploads/2021/07/How-to-Run-Startup-Commands-in-Docker-Containers.jpg)

# Using the Dockerfile ENTRYPOINT and CMD Instructions

[![](https://secure.gravatar.com/avatar/74c679e1d0f3d9d015e2d51aa252907c02ac0ad550d7cd02ccdead7b55b0f315?s=192&d=mm&r=g)Mariam Adedeji](https://adamtheautomator.com/author/mariam-adedeji/)14 July 20215 min. read

Categories: [DevOps](/category/devops/)

Tags:[Docker](/tag/docker/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Creating a Dockerfile](#h-creating-a-dockerfile)
*   [Building a Docker Image](#h-building-a-docker-image)
*   [Running a Docker Container](#h-running-a-docker-container)
*   [Exec vs. Shell Form](#h-exec-vs-shell-form)
*   [Running Startup Commands](#h-running-startup-commands)
*   [Using Variables in a Dockerfile](#h-using-variables-in-a-dockerfile)
*   [Combining Dockerfile ENTRYPOINT and CMD Instructions](#h-combining-dockerfile-entrypoint-and-cmd-instructions)
*   [Conclusion](#h-conclusion)

If you’ve ever needed to run a command or two in your Docker container on startup, this tutorial is for you. Using the [Dockerfile](https://docs.docker.com/engine/reference/builder/) `ENTRYPOINT` and `CMD` instructions, you can run as many startup commands as you’d like.

In this tutorial, you’ll learn how to use the `ENTRYPOINT` and `CMD` instructions to run startup commands in a Dockerfile and understand the differences between them.

Related:[Creating Your First Docker Windows Server Container](https://adamtheautomator.com/docker-windows-server/)

## Prerequisites

Since this tutorial will be a hands-on demonstration, be sure you have the following in place:

*   A Windows 10 PC – Windows 10 v10.0.19042 was used in this tutorial.
*   [Docker Desktop](https://docs.docker.com/docker-for-windows/release-notes/) – This tutorial uses Docker Desktop v3.3.1.

Related:[Deploying your First Container with Docker for Windows](https://adamtheautomator.com/docker-windows/)

## Creating a Dockerfile

Before you can run Docker container startup commands, you must first create a Dockerfile. A Dockerfile is a text document that contains a list of commands to build containers, Docker images and determines how a Docker image is created.

1\. First, open [PowerShell as administrator](https://adamtheautomator.com/powershell-run-as-administrator/).

2\. Create a new folder to store the Dockerfile and all associated files this tutorial will use and change to that directory. This tutorial is using _~/docker_.

```bash
mkdir ~/docker
cd docker
```

3\. Now, create a blank text file named _Dockerfile_ with the following command.

```bash
cd > Dockerfile
```

> _Alternatively, you can create a Dockerfile with the following command if you’re on Linux or Mac OS._

```bash
touch Dockerfile
```

4\. Finally, add the following content into the _Dockerfile_

```docker
FROM ubuntu:20.04
```

You now have created a soon-to-be Dockerfile!

## Building a Docker Image

Now that you’ve created your Dockerfile, you must build a Docker image to execute the commands written in your Dockerfile ENTRYPOINT and CMD instructions. One way to build an image is by using the [`build` command](https://docs.docker.com/engine/reference/commandline/build/).

While in the _~/docker_ directory, run the following command. The command below creates a Docker image called _demo_ (`-t demo`) from the Dockerfile in _~/docker_ by specifying the current working directory (`.`).

```bash
docker build -t demo .
```

![Building a Docker Image](https://adamtheautomator.com/wp-content/uploads/2021/07/demo_image.png)

Building a Docker Image

## Running a Docker Container

After you’ve built the Docker image, you’ll need a container to run the Docker image that will execute the commands from the Dockerfile ENTRYPOINT and CMD instructions.

To run a Docker container, invoke the [`run` command](https://docs.docker.com/engine/reference/run/) to create a writeable container layer over the Docker image (`demo`). The below example is using the `-it` parameter to interactively connect to the container so you can see the sample output.

```bash
docker run -it demo
```

![Running a Docker Container](https://adamtheautomator.com/wp-content/uploads/2021/07/demo_container.png)

Running a Docker Container

## Exec vs. Shell Form

When you begin to work with a Dockerfile and figure out how to run startup commands, you may come across two different methods of defining these commands. Each method will invoke commands but does so a bit differently.

When Docker executes commands, it can do so directly called `exec` or go through the container’s shell (`/bin/sh -c` on Linux or `cmd /S /C` on Windows) called `shell`.

You’ll notice commands executed via `exec` have an instruction followed by the executables to invoke followed by one or more command-line arguments, as shown below.

```docker
ENTRYPOINT ["executables", "parameter1", "parameter2", ...]
CMD ["executables", "parameter1", "parameter2:, ...]
```

Writing commands in `shell` form, on the other hand, doesn’t require wrapping commands in square brackets, as shown below.

```docker
ENTRYPOINT <command> "parameter1"
CMD <command> "parameter1"
```

> _If you don’t specify a argument to `CMD`, Docker will always execute the command in exec form e.g. `CMD <command>`._

If you’re just starting out, differentiating between these two command invocations won’t matter too much but as you get more advanced, you’ll soon see benefits and drawbacks to each.

## Running Startup Commands

Let’s now get in the meat of this tutorial and get your hands dirty by walking through a few examples of running startup commands within a Dockerfile `ENTRYPOINT` and CMD instructions.

1\. Open the Dockerfile you created earlier in your preferred text editor.

2\. Copy and paste the example Dockerfile contents into your Dockerfile, as shown below, and save it.

This Dockerfile creates a layer using the `ubuntu:20.04` as a base image. It then tells Docker to invoke the `echo` command passing it the `Hello world` argument for both the Dockerfile `CMD` and `ENTRYPOINT` instructions using `exec` and `shell` form.

```docker
FROM ubuntu:20.04
# CMD Instruction
CMD ["echo", "Hello world"] # Exec Form
CMD echo "Hello world"      # Shell Form
# ENTRYPOINT Instruction
ENTRYPOINT ["echo", "Hello world"] # Exec Form
ENTRYPOINT echo "Hello world"      # Shell Form
```

3\. While in the _~/docker_ directory, build the new image by running `docker build` and call it `demo`. The command below _[tags](https://docs.docker.com/engine/reference/commandline/tag/)_ the image as `demo` and looks for a Dockerfile in the current working directory (`.`).

```bash
docker build -t demo .
```

![Building a Docker image with docker build](https://adamtheautomator.com/wp-content/uploads/2021/07/Untitled-91-3.png)

Building a Docker image with docker build

4\. Now, run a container using the image then run a Docker container based on the Docker image created earlier. You’ll now see that the container returns `Hello world` which came from the `CMD` instruction provided in the Dockerfile.

```bash
docker run -it demo
```

![Running a Docker container with docker run](https://adamtheautomator.com/wp-content/uploads/2021/07/Untitled-91-4.png)

Running a Docker container with docker run

## Using Variables in a Dockerfile

Sometimes you may not know the exact command-line arguments to pass to command ahead of time. The arguments you need to pass to a command are exposed only at runtime. Rather than statically assigning command arguments, you can capture and pass those arguments to commands with variables.

> _You can only use Dockerfile variables in `shell` form. Docker does not support variables in command invoked via `exec` form._

Open the Dockerfile in your preferred text editor again, replace everything inside with the following series of commands and save it.

You’ll notice this time, the Dockerfile uses environment variables and shown using `ENV`. In the example below, the Dockerfile is defining an environment variable called `name` with a value of `friend`. Once created, this environment variable is then referenced via `$name`.

When Docker runs a container based on this Dockerfile, it will invoke the `echo` command and pass the argument of `Welcome, friend`.

```docker
FROM ubuntu:20.04
ENV name friend

CMD echo "Welcome, $name"
# or
## ENTRYPOINT echo "Welcome, $name"
```

Now, create the Docker image and run the container again optionally providing a tag name of `shellform`. You’ll notice that Docker invoked the `echo` command and returned the expected output.

![Building a Docker Image (shellform) and Running a Docker Container](https://adamtheautomator.com/wp-content/uploads/2021/07/shell.png)

Building a Docker Image (shellform) and Running a Docker Container

## Combining Dockerfile ENTRYPOINT and CMD Instructions

Much of the time, you’ll be invoking startup commands either in CMD or ENTRYPOINT instruction. After all, you can invoke as many commands as you’d like using each method. But, you can also invoke a single command and “build onto it” using both instructions.

Building upon the previous examples, perhaps you have a Dockerfile that looks like the below example. As-is, if you create an image and run a container off of that image, Docker would invoke the `echo` command and return `Hello`.

```docker
FROM ubuntu:20.04
ENTRYPOINT ["echo", "Hello"]
```

Maybe you have another argument you’d like to pass to the `echo` command but not right away. Maybe you’d like to do that further down the Dockerfile. By calling the `CMD` instruction without a command, you can do so.

> _When you specify a command to run via the `ENTRYPOINT` instruction followed by the `CMD` instruction, Docker automatically assumes the value passed to `CMD` is an argument; not a command._

Now, add a `CMD` instruction reference without a command, just an argument called `world`, as shown below.

```docker
FROM ubuntu:20.04
ENTRYPOINT ["echo", "Hello"]
CMD ["world"]
```

> C_ombining instructions should always be written in exec form due to its “array like” behavior of specifying values individually separated by commas vs all in one string._

After building the image and running the container from the image, you can see that instead of two lines of output (`Hello` and `world`), Docker only returns one meaning only a single `echo` command invocation.

![Building a Docker Image (demo3) and Running a Docker Container](https://adamtheautomator.com/wp-content/uploads/2021/07/demo3.png)

Building a Docker Image (demo3) and Running a Docker Container

## Conclusion

You should now have a good understanding of running Docker container startup commands via both the `CMD` and `ENTRYPOINT` Dockerfile instructions. Each instruction is a bit different but accomplishes the same task and can even be used together.

Can you think of a scenario where you’d prefer using `CMD` over `ENTRYPOINT` to run a startup command?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fdockerfile-entrypoint%2F&text=Using%20the%20Dockerfile%20ENTRYPOINT%20and%20CMD%20Instructions)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fdockerfile-entrypoint%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fdockerfile-entrypoint%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-13.webp)

### [Azure Container Apps: Instant Preview Environments per PR](/build-ephemeral-preview-environments-every-pr/)

Build isolated preview environments for every pull request using Azure Container Apps revision labels and Azure DevOps pipelines with scale-to-zero economics.

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2023/07/gitea-docker.jpg)

### [Gitea Docker: Your Ultimate Self-Hosted Git Solution](/gitea-docker/)

Learn how to install a Gitea Docker instance and self-host Git repositories securely in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
