DEV Community

Cover image for DOCKER: A Beginner's Guide
Niharika Goulikar
Niharika Goulikar

Posted on

DOCKER: A Beginner's Guide

Docker is a containerization platform. It helps in packaging our application and its dependencies into a container that can be used across multiple environments.

Why do we need docker?

The way software applications and their dependencies are installed on one machine can differ from how they're installed on other machines. To ensure consistent behavior across different platforms, containerization tools (docker being one such) are used.

docker ship your machine meme

Installing docker on your machine-

Windows/Mac: Download Docker Desktop from the Official website and follow the installation instructions.
Linux: Use the package manager for your distribution (e.g., apt, yum, dnf) to install Docker.
For Ubuntu:

sudo apt-get update 
sudo apt-get install docker-ce docker-ce-cli containerd.io
Enter fullscreen mode Exit fullscreen mode

What is a docker image?

Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how.

Building your docker image:

docker build -t apricot:1.0 .
Enter fullscreen mode Exit fullscreen mode

The general command syntax is as follows:
docker build -t < imagename >:< tag-version > .

. refers to the current directory.If the dockerfile is in other directory, then use the below command:

docker build -t nginx /path/to/folder 
Enter fullscreen mode Exit fullscreen mode

We can get the list of local images present on our machine using this command:

docker images
Enter fullscreen mode Exit fullscreen mode

In Docker, if you don't specify a tag name when pulling or building an image, it defaults to the latest tag. For example, if you run docker pull ubuntu, it will pull the image tagged as latest from the Docker registry.

What is docker registry?

The Docker registry, such as Docker Hub, is where images for various types of applications are stored. In simple terms, a Docker registry is analogous to GitHub, but for Docker images. Just like code repositories on GitHub, Docker Hub maintains a version history of your application's images, allowing you to track and manage different versions of your images.

To work with images, we need them locally on our machine.
Pulling image from docker hub:

docker pull mongo
Enter fullscreen mode Exit fullscreen mode

This command will pull the MongoDB image with the latest tag from Docker Hub. If you want to pull a specific version of MongoDB, you can specify the tag like this:

docker pull mongo:<tag>
Enter fullscreen mode Exit fullscreen mode

What is a container?

A container is a running instance of an image. A Docker container is a lightweight, standalone virtual environment that packages application code along with all the dependencies needed to run the application. This ensures that the application runs consistently and reliably across different computing environments.

docker run -d -p 9090:80 --name plum apricot:1.0 
Enter fullscreen mode Exit fullscreen mode

Here,

  • -d flag corresponds to image running in detached mode.
  • -p flag for the port number, the format is local-port: container-port
  • --name for the container name, webserver in our case

To get all the containers that are currently running. It shows information such as container ID, image name, command, creation time, status, ports, and container names.

docker ps
Enter fullscreen mode Exit fullscreen mode

To see all the container including the ones that are stopped use this command.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Starting a container:

docker start <container-name>
Enter fullscreen mode Exit fullscreen mode

Replace <container-name> with the name or ID of the container you want to start.
Stopping a container:

docker stop <container-name>
Enter fullscreen mode Exit fullscreen mode

Replace <container-name> with the name or ID of the container you want to stop.

flow of image container

Docker Volumes

Volumes are used for data persistence in Docker. Without volumes, any data stored within a container is lost when the container is stopped or removed. Unlike a virtual machine, which has its own file system and memory, a Docker container's file system is ephemeral. To ensure that data is retained across container restarts or re-creations, Docker uses volumes. These volumes are stored on the host machine and can be mounted into one or more containers, allowing data to persist independently of the container's lifecycle.

Use this command to create a volume:

docker volume create <volume-name>
Enter fullscreen mode Exit fullscreen mode

Replace <volume-name> with your desired volume name

Use this command in order to explore the file system of the container:

docker exec -it <container-name> /bin/bash
Enter fullscreen mode Exit fullscreen mode

This command lets you access your container and see what's happening inside a container to some extent.

Use this command in order to mount your volume on container

docker run -d -v <volume-name>:<container-path> --name <container-name> <image-name>
Enter fullscreen mode Exit fullscreen mode

Here,

  • <volume-name> corresponds to the name of the volume.
  • <container-path> corresponds to the file path inside the container where the data will be stored.
  • <container-name> corresponds to the name of the container.
  • <image-name> corresponds to the name of image that is locally present on our machine.

Docker Network

Docker networks are used when we want containers to communicate with each other. In simple terms, containers under the same network can communicate with each other.

Use this command to create a network:

docker network create <network-name>
Enter fullscreen mode Exit fullscreen mode

Use this command to run a container and assign that container to a network

docker run -d --name <container-name> --network <network-name> <image-name>
Enter fullscreen mode Exit fullscreen mode

Here,

  • <network-name> corresponds to the name of the network.
  • <container-name> corresponds to the name of the container.
  • <image-name> corresponds to the name of image that is locally present on our machine.

Top comments (8)

Collapse
 
anisaa_96baa257 profile image
Anisa

Nice article!

Collapse
 
harshika_982e868132d9ddba profile image
Harshika

Wonderful!

Collapse
 
akshaya_goulikar_0d04bc39 profile image
Akshaya Goulikar

Easy to understand!

Collapse
 
jennie_py profile image
Priya Yadav

Wonderful!! 😊

Collapse
 
rohan_sharma profile image
Rohan Sharma

Very informative!

Collapse
 
suraj_kumar_79ebbb6e3724f profile image
SuRaj KuMar

Great Article....!!!

Collapse
 
suraj_kumar_79ebbb6e3724f profile image
SuRaj KuMar

Bemisallllll............!!!!!!!!!!

Collapse
 
goulikar_rajesh_9eb772b11 profile image
Rajesh

That's a fantastic way to quickly get started with Docker.