What is Docker? A Simple Guide for Developers

Vinod PrajapatiVinod Prajapati
6 min read

What is Docker?

Have you ever wondered why Docker containers were created in the first place?
Before Docker, developers often faced a major problem, code that worked perfectly on their own computer would suddenly fail when run on a different system, like a server. This happened because applications need the right environment to function properly, including the correct operating system, libraries, and configuration settings. If even one of those things was different between the developer’s machine and the server, there is a high probability to break the application.

Imagine you have a recipe (the image) and you want to cook it (create a container). Docker allows you to package the ingredients and instructions into a portable container, ensuring that the dish (application) will always be cooked the same way, regardless of where you're cooking it, who is cooking it.

Docker is a tool that speeds up the process of creating, testing, and deploying applications by using containers. It packages an application into a container, which contains all the necessary elements for the application to operate. This container can be run on any system, making it highly versatile. Docker is offered as free, open-source software. Docker allow you to run process in an isolated enviornment in your machine without affecting your machine’s file system and resources. it allow to run app without installing required dependency in local system.

What are containers ?

Containers are a way to package and distribute software applications in a way that makes them easy to deploy and run consistently across different environments. They allow you to package an application, along with all its dependencies and libraries, into a single unit that can be run on any machine with a container runtime, such as Docker.

A Docker container (an Image under execution is container) is a lightweight runnable instance of Docker images. It encapsulates the application, its environment, and the dependencies to run the application isolated from the underlying system, such as code, runtime, system tools, libraries, and settings. Docker containers can be started, stopped, moved, and deleted.

container is a concept and docker is a tool to containarize the app. Docker is a tool/platform that makes it easy to create, manage, and run containers.

Why do we need Containers?

  • Everyone has different Operating systems.

  • Steps to run a project can vary based on OS.

  • Extremely harder to keep track of dependencies as project grows.

3 Different container running in you PC without affecting local resources in an isolated enviornment.

What is Docker Image?

A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files. This image informs how a container should instantiate, determining which software components will run and how.

Docker images are instructions written in a special file called a Dockerfile. It has its own syntax and defines what steps Docker will take to build your container.

What’s Inside a Docker Image?

  • Base Layer

  • Dependencies

  • App Code

  • Metadata

What is a Dockerfile?

The Dockerfile used DSL (Domain Specific Language) and contains instructions for generating a Docker image. Dockerfile will define the processes to quickly produce an image.

A Dockerfile is a text document that contains all the commands a user could call on the command line to create an image. Basically the Dockerfile it is source code of the image.

If you want to create an image from your own code, that you can push to dockerhub, you need to create a Dockerfile for your application.

A Dockerfile has 2 parts

  1. Base image

  2. Bunch of commands that you run on the base image (to install dependencies like Node.js)

Let’s write our own Dockerfile.

Let’s try to containerise this backend app:- https://github.com/vinodpr1/docker-backend

Commands

  • WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPYinstructions that follow it.

  • RUN: Executes any commands in a new layer on top of the current image and commits the results.

  • CMD: Provides defaults for executing a container. There can only be one CMD instruction in a Dockerfile.

  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.

  • ENV: Sets the environment variable.

  • COPY: Allow files from the Docker host to be added to the Docker image

What is Docker Hub?

It is similar to github and npm registry, but it lets you push images rather than source code. Docker Hub is a repository service and it is a cloud-based service where people push their Docker Container Images and also pull the Docker Container Images from the Docker Hub anytime or anywhere via the internet. Generally it makes it easy to find and reuse images. It provides features such as you can push your images as private or public registry where you can store and share Docker images.

Docker Engine

Docker Engine is a software that lets you build, run, and manage applications inside something called containers OR The software that hosts the containers is named Docker Engine. Docker Engine is a client-server based application. The docker engine has 3 main components:

  1. Server: It is responsible for creating and managing Docker images, containers, networks, and volumes on the Docker. It is referred to as a daemon process.

  2. REST API: It specifies how the applications can interact with the Server and instructs it what to do.

  3. Client: The Client is a docker command-line interface (CLI), that allows us to interact with Docker using the docker commands.

Dockerizing an Existing Backend Repo

Nodejs Backend:- https://github.com/vinodpr1/docker-backend

  1. Clone the Repository.

     git clone https://github.com/vinodpr1/docker-backend
     cd docker-backend
    
  2. Create a .dockerfile in the root of the project.

     FROM node:22-alpine
    
     WORKDIR /app
    
     COPY ./package.json ./package.json
    
     COPY ./package-lock.json ./package-lock.json
    
     RUN npm install
    
     COPY . .
    
     CMD ["npm", "start"]
    
  3. Create a .dockerignore file (optional but i would recommend).

     node_modules
    
     .env
    
  4. Build the Docker Image

     docker build -t nodejs-backend-app .
    
  5. Run the Container

    -d helps to run the container in the background (detached from terminal)

    -p 3000:3000 used for port mapping.

     docker run -d -p 3000:3000 --name backend-container nodejs-backend-app
    
  6. Verify It’s Working

     docker ps3
    

Port Mapping

In below images request on port number of local system is being forwarded to corresponding port number of container, port mapping is required because our container runs in a isolated environment. we can run more than 1 container on a same port because they runs in an isolated environment but we can’t map different container to same port of our machine.

Request on port 3000 of my local machine will be forwarded to port 3000 of container.

Common docker commands

Docker images:- Shows you all the images that you have on your machine.

docker images

Docker ps:-Shows you all the containers you are running on your machine.

docker ps

Docker run:- Lets you start a container

docker run -d -p <local_machine_port>:<cotainer_port> <image_name>

Docker Build:- Lets you build an image.

 docker build -t <image__tag> .

Docker stop:- Lets you stop a container.

docker stop <containerID_OR_container_name>

Docker kill:- Lets you stop a container forcefully.

docker kill <containerID_OR_container_name>

Docker rmi:- Lets you remove image from system.

docker rmi <image_name_OR_imageID>

Docker rmi -f :-Force delete if image is in use.

docker rmi -f <image_name_OR_imageID>

Docker logs:- Lets you see the logs on a container.

docker logs <containerID_OR_container_name>

Docker exec:- Lets you interact with docker container’s cli.

docker exec -it <containerID_OR_container_name> it
1
Subscribe to my newsletter

Read articles from Vinod Prajapati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinod Prajapati
Vinod Prajapati