opened image

Dockerfile: A Complete Guide for Beginners and Professionals

​​​​​​​

 

 

Docker — is a powerful tool for creating, deploying, and managing containers that has become the de facto standard in the software development industry. One of the key elements of Docker is the Dockerfile — a text file containing instructions for building a Docker image. Understanding Dockerfile and knowing how to use it correctly is essential for effective use of Docker.

 

What is a Dockerfile?
 

Dockerfile — is a script that contains a set of commands and instructions for creating a Docker image. A Docker image, in turn, is an immutable template that includes everything needed to run a container: the operating system, libraries, dependencies, and the application itself.

 

 

Basic Dockerfile Instructions
 

 

First, let's look at the basic instructions used in a Dockerfile:

 

FROM: This instruction sets the base image on which the new image will be built. For example, FROM ubuntu:20.04 indicates that the base image will be Ubuntu 20.04.

RUN: Used to execute commands in the container. Typically used for installing packages and performing other operations necessary to prepare the environment.

CMD: Defines the command that will be executed when the container starts. Unlike RUN, this command is not executed during the image build, but only when the container is run.

ENTRYPOINT: Allows you to specify a command that will always be executed when the container starts. It can be overridden when starting the container with command-line parameters.

COPY and ADD: These instructions are used to copy files and directories from the local file system to the image's file system. COPY performs a simple copy, while ADD can additionally unpack files and download them via URL.

ENV: Sets environment variables inside the container.

EXPOSE: Indicates which ports will be open in the container for external access.

VOLUME: Creates a mount point for working with persistent storage.

WORKDIR: Sets the working directory for all subsequent instructions.

 

 

Example Dockerfile

 

Let's consider a simple example of a Dockerfile for a Node.js application:

 

# Specify the base image
FROM node:14

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the application source code
COPY . .

# Specify the port
EXPOSE 3000

# Define the command to run the application
CMD ["npm", "start"]

 

 

 


Detailed Breakdown of Dockerfile:

​​​​​​​

FROM node:14 — Indicates that the base image will be the official Node.js image version 14. This means that all necessary dependencies for Node.js will be included in our image.

WORKDIR /app — Sets the working directory /app, where files will be copied and commands will be executed.

COPY package.json ./* — Copies the package.json and package-lock.json files into the container's working directory. These files contain information about the application's dependencies.

RUN npm install — Executes the installation of dependencies specified in package.json.

COPY . . — Copies all files and folders from the current directory (where the Dockerfile is located) into the container's working directory.

EXPOSE 3000 — Indicates that the container will listen on port 3000.

CMD ["node", "app.js"] — Defines the command to run the application. In this case, it is running the app.js file using Node.js.


 

Create a file app.js with the following content, or use your own:


 

// app.js

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

 


Create a file package.json with the following content:

 

{
  "name": "my-node-app",
  "version": "1.0.0",
  "description": "A simple Node.js app with Express",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

 

 

 

Using Dockerfile

 

Now that we have reviewed the basic instructions and looked at an example Dockerfile, let's move on to practical use. To create a Docker image and run a container, you need to execute a few commands in the terminal.

 

Make sure that the app.js, package.json, and Dockerfile files are in the same directory. Then create the image.

 

Creating the image:

 

docker build -t my-node-app .

 

Here, -t my-node-app sets the name of the image, and . indicates that the Dockerfile is located in the current directory.

 

 

Running the container:

 

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

 

The docker run command starts a container based on the created image. The -p 3000:3000 parameter indicates that port 3000 on the host machine will be forwarded to port 3000 in the container. Your server should start successfully, and you will be able to access it by opening http://localhost:3000 in your browser, where you will see the simple greeting "Hello, World!".

 

 

 

​​​​​​​

 

Conclusion

 

Dockerfile — is a powerful tool for automating and standardizing the process of creating Docker images. Understanding the basic instructions and principles of working with Dockerfile allows developers to effectively manage environments and deploy applications in containers. It is important to remember that a Dockerfile should be optimized and contain only the necessary commands to minimize the image size and improve container performance.