✅Docker's Tutorial📃

✅Docker's Tutorial📃

What is a Docker?

Docker is fantastic tool to automate the deployment of software in lightweight containers. By using Docker, developers can quickly build, pack, ship and run the application as lightweight, portable, self-sufficient containers and running virtually anywhere.

Docker Architecture

The main concepts are images and containers. A Docker image is a file used to execute code in a Docker container. A Docker container is a running instance of docker image. However, unlike on traditional virtualization, a Docker container runs on the host kernal. Within a Docker Image, there is no separate operating system.

1_2Y4-1mecA6GWJzF5t-PWiA.pngDockerizing our app

Each Docker has its own IP address, file system and defined resource limitations for CPU and memory. A Docker container doesn't have to boot an OS it starts instantly.

Getting started with Docker


To install the docker just follow up the official document here.

Basic commands for Docker

Checking the images on our local

$ docker images

Screenshot 2022-07-25 215728.png if you newly install Docker, you won’t see anything on the list. You will just see the header result, REPOSITORY, TAG, IMAGE ID, CREATED, SIZE.

Pull the image

To pull an image you can use the command:

$ docker pull <image-name>

or for a specific image version

$ docker pull <image-name>:<tag-name>

For this example, we will pull nodejs image. To pull the image you can check it here. On the right you can see the command to pull.

Screenshot 2022-07-25 220916.png On the Tags menu, you can see the available version of the image.

Screenshot 2022-07-25 221208.png Use the command and pull the image.

Screenshot 2022-07-25 222126.png If you don't specify the tag then it'll automatically pull the latest version of image. After pulling, you can see the node on the image list.

Screenshot 2022-07-25 222403.png

Check the container

To check the running container, you can use

$ docker ps

It will show only the running container, but if you have some container that stopped, it won’t show on the list. To see all containers that we have, we can use

$ docker ps --all

You can use --all or -a. It will show all the containers even if it has been stopped.

Screenshot 2022-07-25 223036.png As you can see this container has been stopped.

Create the container

There are many ways to create a container. These commands are:

$ docker container create <image-name>

to create a container with a specific image tag

$ docker container create <image-name>:<tag>

to create a container with a predefined name, we can use --name argument on our command

$ docker container create --name <some-container-name> <image-name>:<tag>

For example, let’s create a container for our pulled node image:

$ docker container create --name test node

Screenshot 2022-07-25 223620.png let's see the container,

Screenshot 2022-07-25 223700.png As you can see the container is created by not running, this is because create command just creates the container and does not start it. You can also create multiple containers from the same image with one condition, the container name must be different because the container name is unique. If you create a container without specifying the name, Docker will generate a random name for the container.

Start the container

After we create the container, we can run it using the command

$ docker container start <container-name>

Screenshot 2022-07-25 224944.png We can use CONTAINER ID also.

Remove the container

To remove the container we use the command

$ docker container rm <container-name>

An error will occur. This is happening because Docker wants to first stop then remove it. There is two ways to handle this

  • Stop the container using
$ docker container stop <image-name>

and then run the container remove command again

  • Force delete the container by using an argument --force
$ docker container rm <image-name> --force

Remove the Image

To remove the image we can simply type the command,

$ docker image remove <image-name>:<tag>

Docker Logs

We can see the logs of the docker to help debug our app inside the container. We can use the command,

$ docker logs <container-name>

This is the logs of the hello-world container.

Screenshot 2022-07-25 232843.png


We have already used Docker to create the image from the container registry. What if we want to build the image. Here comes the DockerFile. DockerFile is just a text file consists of instructions that we want to build the image. DockerFile consist of commands which contains list of instruction to build the Docker image. The docker build command processes this file generating a Docker Image in your Local Image Cache, which you can then start-up using the docker run command, or push to a permanent Image Repository.

For example, we need to prepare some application code to demonstrate how to build an application image. For the code I'll make the server.js using expressJs.


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

app.listen(4000, ()=>{
    console.log(`Listening to port ${PORT}`)

Create a Dockerfile

# 1. Call the image
# FROM <image name>:<tag>
# for the base we will use node image version 14

FROM node:14
# 2. Working directory where files are located
# WORKDIR <dir>
WORKDIR /backend

# 3. Copy file that needed in this image
# COPY <source> <destination>
# we will put file package.json in ./ inside the container
COPY package*.json ./

#4. Define the ports
# define the port to interact with internal container

#5. Install the necessary dependencies 
# RUN <cmd>
RUN npm install

#6. Tell the image how to run the app
# CMD [<add command using array>]
CMD ["npm", "start", "dev"]

Docker commands

FROM — Select the base image to build

WORKDIR — Define the default working directory

COPY — Copy the file from source to destination

EXPOSE — Define which Container ports to expose

RUN — Specify commands to make changes to your Image and subsequently the Containers started from this Image. This includes updating packages, installing software, adding users, creating an initial database, setting up certificates, etc.

CMD — This is the command that will run when the Container starts

Build the Image

We can use 3 way to build an image:

  • simple way

    docker build <path-to-dockerfile>
  • add a name in our image

    docker build --tag <app name> <path-to-dockerfile>
  • add name and version number

    docker build --tag <app name>:<version number> <path-to-dockerfile>

    Run the image

$  docker run -d -p 4000:4000 <container_name>

Here, -p flag is used to create a mapping between the host’s port 4000 to the container’s port 4000. And -d flag is used to run the new container in “detached” mode (in the background).

Docker Compose

The Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Compose works in all environments: production, staging, development, testing, as well as CI workflows.

For example, we'll create the single-container compose file to build and run the container.

version: "3"
    image: node:14
    build: .
    container_name: backend
    restart: always
      - "4000:4000"
      - ./backend/.env.dev
      - ./backend:/backend
      - /backend/node_modules

Here, indentation is very important.

Docker-compose commands

version — setting docker compose file version.

services — refers to containers’ configuration.

image — pulling the image.

build — path to the dockerfile.

container_name — name of the container.

restart — to restart the container under every circumstance

ports — port for communication.

env_file — path to the environment file.

volumes — physical areas of disk space shared between the host and a container, or even between containers.

To start Docker Compose :

$ docker-compose up

In case our file has a different name than the default one (docker-compose.yml), we can exploit the -f and — file flags to specify an alternate file name:

$ docker-compose -f -d custom-compose-file.yml up

-d flag to run in the background as a daemon

Stop the active services

$ docker-compose stop

To reset the status of our project, instead, we simply run down, which will destroy everything with only the exception of external volumes

$ docker-compose down

Still Curious?

If you wish to explore more here is the link.

Thanks for reading.

Did you find this article valuable?

Support Sagar Medtiya by becoming a sponsor. Any amount is appreciated!