To execute a Dockerfile, you build a Docker image from it using docker build and then run a container from that image using docker run.
The most common mistake new Docker users make is treating a Dockerfile like a shell script. You can’t just hit “run” on it. A Dockerfile is a set of instructions for building a Docker image, and that image is what actually runs as a container. Mastering “how to execute a Dockerfile” means learning this two-step choreography: docker build then docker run.
Executing A Dockerfile: The Build And Run Workflow
A Dockerfile is a blueprint. The docker build command executes its instructions to create a portable snapshot called an image. The docker run command takes that image and spins it up as a live container. You never run the file itself — you run the output it produces.
Understanding this distinction is the gateway to containerization. The Docker Engine reads each instruction in the file line by line, creates a temporary intermediate container for each step, and saves the result as a layered image. That final image is executable. The file is not.
How To Build An Image From A Dockerfile
Here is the exact workflow to write, build, and run your first container from a Dockerfile. Follow these steps exactly to avoid the common pitfalls.
Step 1: Create a Dedicated Directory
Don’t drop a Dockerfile into your home or root directory. The build context includes every file in the current directory, and building from a messy folder will slow things down or cause errors.
mkdir ~/my-project && cd ~/my-project
Step 2: Create the Dockerfile
The filename is conventionally Dockerfile with a capital D. Use any text editor to create it.
touch Dockerfile
nano Dockerfile
Step 3: Write Your Instructions
Start with a base image using FROM. Add the commands needed to set up your application.
FROM python:3.11-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Step 4: Build the Image
Run the docker build command from within your project directory. The -t flag tags the image with a name.
docker build --tag my-app .
Step 5: Run a Container from the Image
Once the build completes, spin up a container using the image name you just tagged.
docker run my-app
If your CMD was ["python", "app.py"], your application will start running in the terminal. You have just executed your Dockerfile.
Core Dockerfile Instructions Reference
These are the most frequently used building blocks for any Dockerfile. Master these, and you can containerize almost any application.
| Instruction | Purpose | Example |
|---|---|---|
FROM |
Sets the base image for subsequent instructions. | FROM node:18-alpine |
WORKDIR |
Sets the working directory for commands. | WORKDIR /usr/src/app |
COPY |
Copies files from the host into the image. | COPY package*.json ./ |
RUN |
Executes commands in a new layer during the build. | RUN npm install |
EXPOSE |
Informs Docker the container listens on a port. | EXPOSE 3000 |
CMD |
Provides the default command for an executing container. | CMD ["node", "app.js"] |
LABEL |
Adds metadata to an image. | LABEL version="1.0.0" |
Why Can’t I Just Run The Dockerfile Directly?
There is no docker run Dockerfile command. The Dockerfile isn’t executable — it is a source file read by the Docker builder. Trying to run it directly is like trying to execute a blueprint instead of the building built from it.
The Three Most Common Mistakes
- Wrong order of operations:
RUNcommands execute during the build. Anything needed for the application must be installed before the finalCMDsets the startup behavior. - Not pinning your base image: Using
FROM python:latestcan pull a different version next month and silently break your app. Pin to a specific tag likepython:3.11-slimfor reproducible builds. - Bad build context: Running
docker buildfrom$HOMEsends your entire home directory to the Docker daemon. Always use a clean project folder. Docker’s official guide on building and tagging images covers these best practices in depth.
Useful docker run Flags For Real Applications
Running a basic container is just the start. These flags control how the container interacts with the host system and handle production scenarios.
| Flag | Purpose | Use Case |
|---|---|---|
-d |
Run container in detached mode (background). | Running a web server or API. |
-p |
Map a host port to a container port. | docker run -p 8080:80 nginx |
-v |
Mount a volume for persistent data. | docker run -v /data:/data ubuntu |
--name |
Assign a readable name to the container. | docker run --name my-app nginx |
-it |
Interactive mode with a TTY (keep stdin open). | docker run -it ubuntu bash |
--rm |
Automatically remove container when it exits. | docker run --rm alpine ls |
How To Share Your Image On Docker Hub
Once your image works locally, you can share it or deploy it from anywhere. Log in to Docker Hub using the CLI, tag your image with your username, and push it up.
docker login
docker tag my-app your-username/my-app
docker push your-username/my-app
The Complete Dockerfile Workflow At A Glance
This is the entire cycle. Commit it to memory and every container task will follow this same shape.
- Prepare: Create a project folder and write your
Dockerfile. - Build: Run
docker build -t your-image .to create the image. - Run: Run
docker run your-imageto start a container. - Share: Run
docker push your-username/your-imageto upload it.
You never execute the file itself. You execute the image it builds.
References & Sources
- Docker Docs. “Build and push your first image.” Official guide on the
docker buildanddocker pushworkflow. - Akamai (Linode). “How to Use Dockerfiles.” Detailed walkthrough on creating and testing Dockerfiles.
- Dockerfile Reference. “Dockerfile reference.” All available instructions and their syntax.
