Day - 99 of DevOps

Day - 99 of DevOps

Key Dockerfile Instructions

Dockerfile

When using containers to deploy your applications, one of the most important things that you must get right is your Dockerfiles. Dockerfiles are how you tell Docker how to build and deploy your containers. If they aren’t well-written or optimized for your needs, that can significantly impact how quickly and efficiently you can get new versions of your application up and running.

How to create a docker file from scratch

Pre-Requisite: Docker should be installed on your system

Below are the steps to create Dockerfile from scratch

This is the sample Dockerfile .

Sample Dockerfile

Let’s see, step by step what is the use of each command and how can we create it from scratch

1)Open your preferred IDE (such as VS Code) and navigate to your project directory. Create a new file in your project directory (at the root level) and name it Dockerfile (no file extension).

I am using VSCode ( Editor) and Cypress Project

Go to Dockerfile and let’s start building it from scratch

Key Dockerfile Instructions

2) FROM

The FROM instruction specifies the base image that the container will be built on top of. This instruction is typically the first one in a Dockerfile and is used to set the base image for the container. The format of the instruction is:

FROM <image>

For example,

FROM node:14-alpine3.16

This instruction tells Docker to use the node:14-alpine3.16 image as the base image for the container. To use a specific version or tag of an image you can use:<version> or:<tag> syntax.

The node:14-alpine3.16 is the official Node.js 14 image based on Alpine Linux version 3.16.

3) WORKDIR

In a Dockerfile, the WORKDIR instruction sets the working directory for any command that follows it in the Dockerfile. This means that any commands that are run in the container will be executed relative to the specified directory. Below is the format of the instruction :

WORKDIR <directory>

For example,

WORKDIR /app

This instruction tells Docker to set the working directory of the container to /app . Any subsequent commands in the Dockerfile, such as COPY, RUN, or CMD, will be executed in this directory.

It’s important to note that the WORKDIR instruction creates the directory if it does not already exist. And the subsequent COPY and ADD commands will be executed relative to the WORKDIR specified.

4) COPY

Use the COPY instruction to copy local files from the host machine to the current working directory. For example, to copy a file named package.json from the host machine’s current directory to the image’s /app directory, you would use the following command:

COPY package.json /app/

If you want to copy all the files from the host’s current directory to the container’s current directory, you can use the below command:

COPY . .

It is used to copy all files and directories from the current directory on the host machine (indicated by “.”) to the current directory within the container.

The first “.” refers to the source directory on the host machine, and the second “.” refers to the destination directory within the container.

5) RUN

Use the “RUN” instruction to execute commands that will run during the image build process. The format of instruction is :

RUN <command_name>

For example, to update the package manager and install a specific package, you would use the following command:

RUN npm install

6) CMD

In a Dockerfile, the CMD instruction sets the command that will be executed when a container is run from the image. The format of the instruction is:

CMD ["executable","param1","param2",...]

For example,

CMD ["npm", "start"]

This instruction tells Docker to run the command npm start when a container is created from the image. This command will start the Node.js application that was installed in the container using the npm install command.

Apart from all the instructions mentioned, there are some more instructions like ENV, and EXPOSE which are also used in creating Dockerfile. Below is a detailed description of the same:

7) ENV

Use the ENV instruction to set environment variables inside the image which will be available during build time as well as in a running container. For example, to set the NODE_ENV environment variable to production, you would use the following command:

ENV NODE_ENV production

8) EXPOSE:

Use the EXPOSE command to tell Docker which ports the container will listen on at runtime. For example, if your application listens on port 9000, you would use the following command:

EXPOSE 9000

Sample Docker File would like below:

FROM node:14-alpine3.16

WORKDIR /app

COPY . .

RUN npm install

CMD [ "npm", "start" ]