Dockerfile, way to create custom images.

Kratarth Paliwal
4 min readMar 27, 2021

What is Dockerfile?

A Dockerfile is a script/text configuration file that contains collections of commands and instructions that will be automatically executed in sequence in the docker environment for building a new docker image. This file is written in a popular, human-readable Markup Language called YAML.

The docker build command processes this file generating a Docker Image in your Local Image Cache, which you can then start up using the docker run command, or push to a permanent Image Repository.

Dockerfile — UseCase

By using a Docker image, it is not only possible to deploy one container after another, it’s quite easy. Once you’ve pulled the image from a registry (such as Docker Hub), each container can then be deployed with a single docker command. But what happens when you find yourself having to deploy numerous containers (each for a different purpose) from the same image? All of a sudden the management of those containers can get a bit cumbersome.

Say, for example, you pull down the latest Ubuntu image for development. Before you can develop with that container, there are a number of modifications you want to make to the image (such as upgrading software and adding the necessary development packages for the job at hand).

For this, you could manually edit each image as needed (creating a new image for each necessary variation on the theme), or you could construct a Dockerfile for each variation. Once you have your Dockerfile constructed, you can quickly build the same image over and over, without having to take the time to do it manually. Carefully crafted Dockerfiles can save you considerable time and effort.

I want to walk you through the process of crafting a Dockerfile. I will demonstrate by using the latest Ubuntu image, update and upgrade that image, and then install the build-essential package. This will be a fairly basic Dockerfile, but one you can easily build upon.

Dockerfile Basics

Before we construct our Dockerfile, you need to understand what makes up the file. This will be a text file, named Dockerfile, that includes specific keywords that dictate how to build a specific image. The specific keywords you can use in a file are:

  • ADD copies the files from a source on the host into the container’s own filesystem at the set destination.
  • CMD can be used for executing a specific command within the container.
  • ENTRYPOINT sets a default application to be used every time a container is created with the image.
  • ENV sets environment variables.
  • EXPOSE associates a specific port to enable networking between the container and the outside world.
  • FROM defines the base image used to start the build process.
  • MAINTAINER defines the full name and email address of the image creator.
  • RUN is the central executing directive for Dockerfiles.
  • USER sets the UID (or username) which is to run the container.
  • VOLUME is used to enable access from the container to a directory on the host machine.
  • WORKDIR sets the path where the command, defined with CMD, is to be executed.
  • LABEL allows you to add a label to your docker image.

Example:

Creating a custom image for Python Interpreter

  1. Create a workspace in which we create Dockerfile(py_int is the workspace in my case).

2. Create Dockerfile(name should be Dockerfile, and there is always only one Dockerfile in a workspace)

custom image created from centos image pre-created in DockerHub registry.

3. Create a python file, inside your same workspace, which will copy to container image at build time.

Test python code

4. Building custom image by ‘docker build’ command, which is internally building a stack of each layer(each line in Dockerfile)

5. Running Python Interpreter container by giving input as our source file

Running good looks like code ran in our Base system but the output is coming from the container.

--

--