OneBite.Dev - Coding blog in a bite size

build docker image using dockerfile

Simple docker step by step how to build docker image using dockerfile with explanation

Docker allows you to create software in containers, using a Dockerfile. This article will guide you on how to build a Docker image using Dockerfile.

Introduction

A Dockerfile is a text document that contains the instructions to build a Docker image. It automates the deployment process of your software in a Docker container.

Step 1: Install Docker

The first step is to install Docker on your machine. It is available on Windows, Mac, and Linux. You can download it from the official Docker website: https://www.docker.com/

Step 2: Create a Dockerfile

After installing Docker, you need to make a Dockerfile. Open up a text editor, create a new file, and save it as Dockerfile.

FROM debian:latest

WORKDIR /app

COPY . /app

RUN apt-get update \
 && apt-get install -y python3-pip python3-dev \
 && cd /app \
 && pip3 install -r requirements.txt

EXPOSE 80

CMD [ "python3", "app.py" ]

The above Dockerfile is an example that creates a Python-based Docker image.

Step 3: Build the Docker Image

To build your image, you would use the docker build command in the terminal. The -t flag allows you to tag your image so it’s easier to find later.

docker build -t my-python-app .

The ”.” at the end of the command specifies that Docker should look for the Dockerfile in the current directory.

Step 4: Verify the Docker Image

You can verify that your image was built correctly by running the docker images command:

docker images

Step 5: Run the Docker Container

After verifying the image, run a container based on your new image:

docker run -p 8888:80 my-python-app

At this point, your software is running in a docker container.

In conclusion, Dockerfiles provide an efficient way to specify the environment and get your software running quickly and consistently. With this guide, you can now build your own Docker images using a Dockerfile. Happy Dockering!

docker