How to Create a Docker Container using Dockerfile

Thangarajnagulraj
featurepreneur
Published in
2 min readSep 27, 2022

Docker allows you to build containers using a Dockerfile. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to build an image.

A Dockerfile consists of various commands and arguments listed successively to automatically perform actions on a base image in order to create a new one. It helps us to avoid issuing the command every time while running container.

You can build the Docker image using one of the following two options:

  1. Interactively launch a BASH shell under the Python Base image and then save the image.
  2. Build the Docker image using Dockerfile with the web site included.

In this tutorial, I will explain how to create a Dockerfile, install the Python packages, add the necessary content and then build the image.

Requirements

python:3.9.5-slim-buster with Docker installed on your system.

Creating a Dockerfile

A Dockerfile is a text file that has a series of instructions to build an image. It supports a set of commands that we need to use in our Dockerfile.

There are several commands supported like FROM, CMD, ENTRYPOINT, VOLUME, ENV and many more. Each and every instruction set in the Dockerfile adds an additional layer to the image and then performs a commit.

Here, we will create a Dockerfile to create an image to install the Apache Web Server container.

Here, we will create a Dockerfile to pull the image already created python:3.9.5-slim-buster

To do this, we will need to create a file named Dockerfile using any text editor:

Add the following content which includes the commands and arguments for the container.

FROM python:3.9.5-slim-busterWORKDIR /codeCOPY . /codeRUN pip3 install -r requirements.txtEXPOSE 5000CMD ["python3","app.py"]

Now, save and close the file.

In the above Dockerfile, the first parameter FROM tells Docker what the source of our image is, in this example we're using python-flask image.

A RUN parameter executes a series of commands inside the image to install package. Here we are installing the python requirements package.

The EXPOSE parameter set's the default Apache port 5000 so that the website will be available normally.

Building an Image using Dockerfile

Now, after we finish creating our Dockerfile for the python-flask container, we are ready to create our first python-flask images with docker

We’ll need to run the following command in our current working base directory to build an image.

sudo docker build -t python-flask .

-> The -t parameter used to tag the Docker image. The . parameter is used to specify the location of the Dockerfile that we created.

Creating a Docker Container

Using the image we have built, we will now proceed to create a container running an python-flask inside, using a name of our choice. Here we will use python-flask.

sudo docker run --name python-flask -p 80:80

After setting everything up, you can verify the Apache Web Server by typing the url http://localhost:80 in your web browser.

--

--