Decoding Dockerfile: Step-by-Step Image Building

ยท

4 min read

Dockerfile: A Guide to Docker Image Creation

Welcome to the world of Docker, where creating containerized applications becomes a breeze! In this guide, we will explore the Dockerfile, which contains instructions to build Docker images. Docker's Domain Specific Language (DSL) empowers us to craft powerful images for our applications.

Dockerfile Basics

A Dockerfile contains a series of instructions that guide the Docker Engine to build an image. The engine processes instructions from top to bottom to create the image. We will delve into various Dockerfile keywords to understand their roles in image creation.

Dockerfile Keywords

  1. FROM

    The FROM keyword specifies the base image for creating our Docker image.

    Syntax Examples:

     FROM java:1.8
     FROM python:1.2
     FROM mysql:8.5
     FROM tomcat:9.5
    
  2. MAINTAINER

    The MAINTAINER keyword is used to provide the author's information for the Dockerfile.

    Syntax Example:

     MAINTAINER Pankaj <pankaj@oracle.com>
    
  3. COPY and ADD

    Both COPY and ADD keywords are used to copy files from source to destination within the Docker image.

    Syntax Examples:

     COPY <SRC> <DESTINATION>
     ADD <SRC> <DESTINATION>
     ADD <HTTP-URL> <DESTINATION>
    
  4. RUN

    The RUN keyword executes instructions during image creation.

    Syntax Example:

     RUN yum install git
     RUN yum install maven
     RUN git clone <repo-url>
     RUN cd <repo-name>
     RUN mvn clean package
    
  5. CMD

    The CMD keyword specifies commands to be executed when a container is launched.

    Syntax Examples:

     CMD sudo start tomcat
     CMD java -jar <jar-file>
    
  6. ENTRYPOINT

    The ENTRYPOINT keyword specifies instructions to be executed when a container is launched.

    Syntax Examples:

     ENTRYPOINT ["echo", "Container Created Successfully"]
     ENTRYPOINT ["java", "-jar", "target/springboot.jar"]
    
  7. ENV

    The ENV keyword sets environment variables within the image.

    Syntax Example:

     ENV java /etc/softwares/jdk
    
  8. EXPOSE

    The EXPOSE keyword specifies the port on which the Docker container will run.

    Syntax Example:

     EXPOSE 8080
    
  9. ARG

    The ARG keyword allows us to use dynamic values from the Command Line Interface (CLI).

    Syntax Example:

     ARG branch
     RUN git clone -b $branch <repo-url>
    
  10. USER

    The USER keyword specifies the username when creating an image or container.

    Syntax Example:

    USER dockeruser
    
  11. WORKDIR

    The WORKDIR keyword sets the working directory for the image and container.

    Syntax Example:

    WORKDIR /app/usr/
    
  12. VOLUME

    The VOLUME keyword specifies the Docker volume storage location.

  13. LABEL

    The LABEL keyword adds metadata to Docker objects in key-value format.

Sample Dockerfile:

FROM ubuntu
MAINTAINER Pankaj <pankaj@oracle.com>
RUN echo "Hi, I am run - 1"
RUN echo "Hi, I am run - 2"
RUN echo "Hi, I am run - 3"
CMD echo "Hi, I am CMD-1"
CMD echo "Hi, I am CMD-2"
CMD echo "Hi, I am CMD-3"

Building and Pushing Docker Images

Follow these steps to build and push your Docker image:

  1. Create a Dockerfile using a text editor.

  2. Copy the content into the Dockerfile.

  3. Create the Docker image using the command:

     $ docker build -t <image-name> .
    
  4. Log in to your Docker Hub account from the Docker machine:

     $ docker login
    
  5. Tag the Docker image:

     $ docker tag <image-name> <tag-name>
    
  6. Push the Docker image to Docker Hub:

     $ docker push <tag-name>
    

Interview Questions and Answers

Q) What is the difference between COPY and ADD command?

Ans:

  • COPY: Copies files from one path to another within the same machine.

  • ADD: Copies files from one path to another and supports URLs as sources.

Q) What is the difference between RUN and CMD?

Ans:

  • RUN instructions execute during image creation.

  • CMD instructions execute during container creation.

  • Multiple RUN instructions are processed from top to bottom.

  • Multiple CMD instructions are allowed, but only the last one is processed.

Q) Can we use a user-defined name for Dockerfile?

Ans: Yes, we can. Use the -f flag to specify the filename while building the image:

bashCopy code$ docker build -f <filename> -t <imagename> .

Remember, Dockerfile instructions enable us to craft customized images for our applications, optimizing development and deployment workflows.

Stay tuned for more Docker insights and guides by Pankaj Suryavanshi as we dive deeper into Docker's versatile world! ๐Ÿณ๐Ÿš€

ย