DEV Community

Cover image for Getting started with docker
Kunal Singh
Kunal Singh

Posted on

Getting started with docker

INTRODUCTION

Docker is a container management service. The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere. The initial release of Docker was in March 2013 and since then, it has become the buzzword for modern world development, especially in the face of Agile-based projects.

*USES OF DOCKER
*

Docker streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide your applications and services. Containers are great for continuous integration and continuous delivery (CI/CD) workflows.
Docker’s container-based platform allows for highly portable workloads. Docker containers can run on a developer’s local laptop, on physical or virtual machines in a data center, on cloud providers, or in a mixture of environments.
Docker’s portability and lightweight nature also make it easy to dynamically manage workloads, scaling up or tearing down applications and services as business needs dictate, in near real time.

*DOCKER ARCHITECTURE
*

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface. Another Docker client is Docker Compose, which lets you work with applications consisting of a set of containers.

*DOCKER DAEMON
*

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

*DOCKER CLIENT
*

The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

*DOCKER DESKTOP
*

Docker Desktop is an easy-to-install application for your Mac or Windows environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. For more information, see Docker Desktop.

*DOCKER REGISTRIES
*

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry. When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

*DOCKER OBJECTS
*

When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

*DOCKER IMAGES
*

An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run. You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Docker file with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Docker file creates a layer in the image. When you change the Docker file and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

*DOCKER CONTAINERS
*

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine. A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.

*ENOUGH THEORY LET’S START THE PRACTICAL
*

*Install docker in your local system
*

Install docker in your system. You can refer to the video or documentation here. Once the installation is complete you can proceed to Step 2.

Open the visual studio code (VS Code) and create a directory and create a file inside it with javascript extension (test.js) and create a Dockerfile. Install relevant packages related to docker
–Docker
|- test.js
|- Dockerfile

test.js

Once the above step is done lets create an image. But before creating an image let us understand what will be there in the image.

So, if we have to run a JavaScript file we can use node for that like node test.js, So we need a Kernel or an OS and then we need a runtime environment in this case it is Node. Then we need a file which we have to execute and in the end we have to run that file to get the output.

In the Docker file created in our directory in step 2 we will write steps to be followed to run that file independently.
We will start to write with a base image. A base image has a bunch of files where we go and take files and add additional files to it.

FROM node:alpine -> There are different node images which are built on top of different distributions of linux. ( : ) colon is used to mention which Linux distribution we would like to use. Alpine is nothing but a linux distribution. If we want we can take some other distribution as well.

COPY source destination -> The COPY instruction copies new files or directories from and adds them to the filesystem of the container at the path .
WORKDIR /path/to/workdir -> The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Docker file. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Docker file instruction.

The CMD instruction has three forms:
CMD "executable","param1","param2"
CMD "param1","param2"
CMD command param1 param2 (shell form)
There can only be one CMD instruction in a Docker file. If you list more than one CMD then only the last CMD will take effect.
The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

*The code in out file will be:
*

FROM node:alpine
COPY . /app
WORKDIR /app
CMD node test.js

*IMPORTANT COMMANDS TO MAKE DOCKER RUN
*

DESCRIPTION -> To build a docker image
COMMAND -> docker build -t name-of-file destination

DESCRIPTION -> Running an image
COMMAND -> docker run ubuntu

DESCRIPTION -> To see list of running containers
COMMAND -> docker ps OR docker container ls

DESCRIPTION -> To see list of running and stopped containers
COMMAND -> docker ps -a OR docker container ls -a

DESCRIPTION -> To run docker in an interactive mode
COMMAND -> Docker run -it ubuntu

*REFERENCES
*

DOCKER BUILDER GUIDE (https://docs.docker.com/engine/reference/builder/)
An introduction to Docker for reproducible research
(https://dl.acm.org/doi/10.1145/2723872.2723882)
The Docker Book: Containerization is the new virtualization
Learning Docker
Docker: Up & Running: Shipping Reliable Containers in Production

Top comments (0)