Rahul's Blog

Dockerfile

August 01, 2020

What is Dockerfile?

Dockerfile is a text file with instructions to build image. In simpler terms it is automation of docker image creation.

Let’s understand this using following steps.

Step 1: Create a file named Dockerfile

$ touch Dockerfile

Step 2: Add instructions in Dockerfile

# getting base image
FROM alpine

# MAINTAINER rahul <rahuldkjain@gmail.com>
# the above command is deprecated, so will use LABEL instead
LABEL maintainer="rahuldkjain@gmail.com"

# it executes during the build of the image
RUN echo "HI my name is rahul jain" | grep "rahul"

# it executes only when you create container out of the image
CMD ["echo", "Hello World from my first docker image"]

Step 3: build docker file to create the image

$ docker build <file-directory>

# Example
# . means the same directory
$ docker build .

If you want to give image-name or tag-name

$ docker build -t myImage1:1.0 <file-directory>

Step 4: See docker images

$ docker images

Step 5: Run Image to create container

$ docker run <image-id>

Reference: Dockerfile Reference


Written by Rahul Jain         
I write code like I make sandwich