DEV Community

Sagar Medtiya
Sagar Medtiya

Posted on • Originally published at blog.sagarmedtiya.me on

βœ…Docker's TutorialπŸ“ƒ

img

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

Installation

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

Basic commands for Docker

Checking the images on our local

$ docker images

Enter fullscreen mode Exit fullscreen mode

Screenshot 2022-07-25 215728.pngif you newly install Docker, you wont 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>

Enter fullscreen mode Exit fullscreen mode

or for a specific image version

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

Enter fullscreen mode Exit fullscreen mode

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.pngOn the Tags menu, you can see the available version of the image.

Screenshot 2022-07-25 221208.pngUse 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

Enter fullscreen mode Exit fullscreen mode

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

$ docker ps --all

Enter fullscreen mode Exit fullscreen mode

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

Screenshot 2022-07-25 223036.pngAs 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>

Enter fullscreen mode Exit fullscreen mode

to create a container with a specific image tag

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

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

For example, lets create a container for our pulled node image:

$ docker container create --name test node

Enter fullscreen mode Exit fullscreen mode

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

Screenshot 2022-07-25 223700.pngAs 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>

Enter fullscreen mode Exit fullscreen mode

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

Remove the container

To remove the container we use the command

$ docker container rm <container-name>

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

and then run the container remove command again

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

Enter fullscreen mode Exit fullscreen mode

Remove the Image

To remove the image we can simply type the command,

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

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

This is the logs of the hello-world container.

Screenshot 2022-07-25 232843.png

DockerFile

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.

server.js

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

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

Enter fullscreen mode Exit fullscreen mode

Create a Dockerfile

# BUILD IMAGE FROM THE EXISTING IMAGE
# 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
EXPOSE 4000

#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"]

Enter fullscreen mode Exit fullscreen mode

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

  • add a name in our image

  • add name and version number

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

Enter fullscreen mode Exit fullscreen mode

Here, -p flag is used to create a mapping between the hosts port 4000 to the containers 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 applications 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"
services:
  backend:
    image: node:14
    build: .
    container_name: backend
    restart: always
    ports:
      - "4000:4000"
    env_file:
      - ./backend/.env.dev
    volumes:
      - ./backend:/backend
      - /backend/node_modules

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

-d flag to run in the background as a daemon

Stop the active services

$ docker-compose stop

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Still Curious?

If you wish to explore more here is the link.

Thanks for reading.

Top comments (0)