How to build and run a Docker Container with Dockerfile

How to build and run a Docker Container with Dockerfile

A Dockerfile is a text document that contains all the commands we could call on the command line to build a docker image. Using docker build command we can create an automated build that executes several lines of command which are defined in the dockerfile.

In short, docker can build images automatically by reading the instructions from a Dockerfile.

The advantage of using Dockerfile is that we can expect the build of our app defined in the Dockerfile behaves exactly the same wherever it runs.

Step 1: Create a Dockerfile

FIrst. let us create an empty directory on our local machine. Then, change directories (cd) into the new directory, create a file called Dockerfile.

# mkdir myapp
# cd  myapp
# vi Dockerfile

Copy-and-paste the following content into the Dockerfile, and save it.

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host -r requirements.txt

# Make port 80 available to the world outside this container

# Define environment variable

# Run when the container launches
CMD ["python", ""]

This Dockerfile refers to a couple of files we haven’t created yet, namely and requirements.txt. Let’s create those next.

Step 2: Create and requirements.txt

Let us create and requirements.txt in the same folder which contains the Dockerfile.



from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

def hello():
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":'', port=80)

Step 3: Build the app

We are ready to build the app. Make sure we are still at the top level of our new directory. Here’s what ls should show:

# ls
Dockerfile   requirements.txt

Now run the build command. This creates a Docker image, which we’re going to name using the --tagoption. Use -t to use the shorter option.

# docker build --tag=myapp .

Next , let us check the build image with below command.

# docker image ls

myapp            latest      236754cef254

Step 4: Run the app

We will run the app mapping the machine’s port 8000 to the container’s port 80 using -p:

# docker run -p 8000:80 myapp

We should see a message that Python is serving our app at But that message is coming from inside the container, which doesn’t know we mapped port 80 of that container to 8000, making the correct URL http://localhost:8000

Go to the below URL in a web browser to see the display content served up on a web page.


Now let’s run the app in the background, in detached mode:

# docker run -d -p 8000:80 myapp

Our container is now running in the background. We can check the container ID with below command.

# docker container ls
CONTAINER         ID           IMAGE            COMMAND CREATED
1a3f5gh2cs267    myapp     "python"      25 seconds ago

We can also stop and start the conatiner using below command.

Stop the container:

# docker container stop 1a3f5gh2cs267

Start the conatiner:

# docker container start 1a3f5gh2cs267

This completes the Docker build with Dockerfile tutorial. Enjoy your Docker journey!

Safi Ahmed Choudhury

Safi Ahmed Choudhury

Safi is the founder and chief editor of ZoomTutorials Blog, a leading tutorials and technology blogging site specializing in DevOps, SysAdmin and Cloud Technologies to help IT professionals in their day to day work. He is a Senior Cloud and DevOps Solutions Engineer at a leading eCommerce development Company and has more than 8 years of SysAdmin experience working with Fortune 500 companies to solve their most important IT backbones. Safi lives in Hyderabad with his wife and a son.

One thought on “How to build and run a Docker Container with Dockerfile

  1. Hi,

    Thank you for writing the post. It is very helpful for someone like me who is just starting the Docker journey. Your post is very easy to understand. Please keep posting more post on Docker and Docker Compose.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.