OneBite.Dev - Coding blog in a bite size

build a docker image from dockerfile

Simple docker step by step how to build a docker image from dockerfile with explanation

Building a Docker image from a Dockerfile is a straightforward process which can be achieved in just a few steps. Let’s take a closer look.

Introduction

In this article, you’ll learn about creating your Docker image from a Dockerfile.

Step 1: Install Docker

The first and foremost requirement is having Docker installed on your machine. You can download Docker from the official Docker website and follow the installation instructions according to your operating system.

Step 2: Create Dockerfile

The next step is to create a Dockerfile which includes a set of instructions to create an image. You can create a new file named ‘Dockerfile’ (without any extension) in your preferred text editor.

Below is a sample Dockerfile:

FROM ubuntu:18.04
COPY . /app
RUN make /app
CMD python /app/app.py
  • ‘FROM’ initializes a new build stage and sets the base image.

  • ‘COPY’ adds files from your Docker client’s current directory.

  • ‘RUN’ will execute any commands in a new layer on top of the current image and commit the results.

  • ‘CMD’ provides defaults for an executing container.

Step 3: Building Docker Image

With your Dockerfile, you’re ready to build your Docker image. You can do this using the ‘docker build’ command followed by a tag, so it’s easier to refer to later.

docker build -t your-tag-name .

The ’.’ says to build using the local Dockerfile in your current directory.

Step 4: Confirming Docker Image Creation

Now, you can verify if your Docker image was successfully created using the ‘docker images’ command. If the docker image is created successfully, it will be listed there.

docker images

There you have it, a simple guide to creating a Docker image from a Dockerfile. Let this be the basis of your Docker journey as you discover more about containerization and its many benefits. Enjoy your Dockering adventure!

docker