Dockerfile and Docker Compose

Dockerfile and Docker Compose

📍Introduction

Dockerfile and Docker Compose are two important tools in the Docker ecosystem that help with the containerization and deployment of applications. Using these tools we can package all the dependencies of an application into standardized units called containers that everything the application needs to run including libraries, system tools, code, and runtime.

🔹What is Dockerfile?

A Dockerfile is a text document that contains all the instructions on how to create a Docker image. It defines the base image, adds dependencies, copies files, and configures the environment. Once the Dockerfile is created, it can be used to build a Docker image, which is a lightweight, standalone executable package that includes everything needed to run an application

🔹Docker commands for creating a Dockerfile

  • FROM: It specifies the based image to use for a Docker image.

  • RUN: Executes commands in Docker image, such as installing packages, updating the system, or setting environment variables.

  • COPYand ADD: Copy files or directories into the Docker Image from the local file system or remote URLs.

  • WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, or ADD commands that follow it.

  • EXPOSE: Specifies which ports the Docker container will listen on at runtime.

  • CMD and ENTRYPOINT: Define the command that will run when the container starts

  • ENV: Sets environment variables in Docker image.

  • ARG: Defines variable that can be passed at build time using the '--build arg' flag.

  • LABEL: Adds metadata to the Docker image, such as the maintainer, version, or description.

These are just a few commands that can be used in a Dockerfile. By combining these commands, you can create a Docker image that includes all the dependencies and configurations needed to run your application in a container.

Difference between CMD and ENTRYPOINT

CMD is used to provide default arguments for the container's entry point command. If the Docker command line includes arguments, they will override the CMD arguments. On the other hand, ENTRYPOINT specifies the command that will be executed when the container starts.

Difference between COPY and ADD

COPY is a simpler instruction that only supports copying local files and directories into the Docker image. It does not support extracting tar files or copying files from remote URLs. ADD, on the other hand, supports both local files and directories as well as remote URLs, and it can automatically extract tar files.

Following is the simple Dockerfile which packages NodeJS application in a container:

FROM node:12-alpine

WORKDIR /usr/src/app

COPY package*.json /usr/src/app/

RUN npm install 

COPY . /usr/src/app/

EXPOSE 3000

CMD [ "npm", "start" ]

To build this Dockerfile we have to run the following command:

docker build -t node-app .

-t: Flag is used to tag the Docker image as node-app

To run the Docker image we have to run the following command:

docker run -itd -p 3000:3000 node-app

-itd: Used to run the container in interactive mode and detached mode.

-p: Used to map the port from 3000 to 3000.

🔹What is Docker Compose?

Docker Compose is a tool used to manage multiple Docker containers. It is a YAML file that defines a service, network and volumes needed to run a set of containers. Docker compose can be used to orchestrate the deployment of multiple containers, allowing them to communicate with each other and work together as a cohesive application. Docker compose can also be used to dockerize microservices.

🔹What is YAML?

YAML stands for Ain't Markup Language it is a human-readable data serialization language. Its often used for configuration files, data exchange between different programming languages, and storing structure data. YAML is designed to be easily read by humans and uses indentation to indicate the structure of the data.

Following is a docker-compose.yml file that describes a multi-container setup for a Node.js application and an Nginx reverse proxy.

version: '3'

services:
    nodejs-app:
        build:
          context: ./
        container_name: nodejsserver
        hostname: nodejsserver
        ports:
            - "3000:3000" 
        networks:
            - example-net

    nginx:
        build:
          context: ./nginx
        container_name: nginx
        hostname: nginx
        ports:
            - "80:80" 
        depends_on:
            - nodejs-app
        networks:
            - example-net

networks:
  example-net:
    external: true

The following command is used to build this docker-compose.yaml file:

docker-compose build

The following command is used to run the docker images created using docker-compose.yaml file:

docker-compose up

The following command is used to stop and delete containers started using above command:

docker-compose down

📍Conclusion

Dockerfile is used to create Docker images, while Docker Composr is used to manage multiple Docker containers. Together, they provide a powerful solution for the containerization and deployment of applications. By using Dockerfile and Docker Compose, we can simplify the deployment process, reduce dependency conflicts, and improve the scalability and maintainability of our applications. With the popularity of containerization increasing, developers need to understand these tools and incorporate them into their development workflows.

Did you find this article valuable?

Support Ashutosh Mahajan's blog by becoming a sponsor. Any amount is appreciated!