Skip to main content

Command Palette

Search for a command to run...

Docker for Beginners

Get started with DOCKER containers. Learn different terminologies like Docker-Compose , Docker RUN , Dockerfile.

Updated
โ€ข6 min read
Docker for Beginners
J

Self Learning Programmer ๐Ÿง‘๐Ÿผโ€๐Ÿ’ป

What is Container?

It is a way of packaging an application with all necessary dependency files and configuration files. Containerised applications are very easy to move and share in between, for example, applications are shared between the development and operation teams via containers. Containerisation will increase the efficiency of development and deployment.

Why containers?

Application Development

Before containers during the development of an application, if team members are using different OS the installation of different services like mongo, mongo-express for the application development will be different. It will be also a time-consuming step.

With Containers, you are installing each service not directly to your OS rather the images of each service are pulled. Since it doesn't depend on your host OS the service will be the same for each team member. Also, each service can be installed with a simple command docker run <image> .

Application Deployment

Before the container, the developer team will send the application to the operation team with a set of instructions. The operation team has to set the environment suitable for the application to run according to the instruction. The problem is there is a chance of misunderstanding between the operation and developer teams which will result in back forth communication and delays deployment of the application. Also if the developer team miss any configuration step this can cause big trouble.

Now the developer and operation team can collectively containerise the application. The Containerised application will be easy to deploy on the server. The only task to deploy an application is to pull the containerised application to the server.

Also, users can run different versions of the same application in the same system with the help of containers.

Layers in container

A container contains different layers of images. The base layer of the container will be a Linux image which provides the container with an isolated OS. The Application image will be at the top. The container will contain many intermediate layers of images. Each layer of the image is identified by a unique id.

The major advantage of layers in the container is shown in the above example, firstly I have pulled MariaDB:10.4 since my local repository doesn't have MariaDB it pulled all different layers of image from the docker hub. Later I pulled MariaDB:10.5 but now rather than pulling all layers of images from the docker hub it checked locally for similar images and it only pulled image layers that are not present locally.

Storage for Containers

Containers are stored in a container repository. Mainly there are 2 types of container repositories, private repositories and public repositories.

Private repositories are owned by companies and are used to store the containerised application of the company. Amazon has amazon ECR to store docker images in a private repository. Whereas a public repository is available to all, the public repository for docker is DockerHub. Containerised applications present in the public repository are free to use.

Container vs Image

The actual package that contains the application along with all its dependencies and configuration files is called Image. Images can be moved around between team members.

When an image is pulled to a computer and the image is started, now this creates a container. Shortly, if the package is not running it's called an image, if running it's called a container.

Docker vs VM

An operating system can be classified into 2 layers kernel and application layer. The kernel layer is used to communicate with hardware components whereas, the application runs over the OS kernel layer. Docker virtualises the application layer of the OS, which means it uses the host OS kernel. Whereas Virtual Machine virtualises both the application and kernel layer of the operating system.

The Size of the docker image will be less as compared to VM. Docker image starts and runs fast as compared to VM. Older versions of Mac and Windows may not support docker.

Docker installation

To install docker, head over to Docker's official website and download the suitable docker desktop for your OS. You can learn docker with fun through docker playground.

Basic commands in Docker

  • docker pull <image>:

    This command is used to pull an image of an application to your local repository

  • docker run <image>:

    This command runs an image and creates an instance(container) of it.

  • docker ps:

    This list all the containers running instantly.

  • docker images:

    The above command will list all the image files in the local storage.

  • docker stop <container id>:

    Above command is used to stop a running container.

  • docker start <container id>:

    This command can be used to restart a stopped container.

  • docker ps -a:

    This command displays all the containers running and not running.

  • docker run -p<host-port>:<container-port> <image>:

    The above command is used to bind a host port and a container port of an instance of an image.

  • docker logs <container-id/unique-name>

    This command displays all the logs of the corresponding image.

  • docker run --name <unique name> <image>

    --name flag can be used to give a custom name to the container.

  • docker exec -it <container-id> /bin/bash

    The above command is used to get into the terminal of the running container.

Workflow of Docker in Development

In the development phase, the application is built. Code is commited using version controller git. Jenkins is a Continuous Integration tool which will build the application and generate an image of the same using Dockerfile. Companies use private container repositories to store images. Further, the image is pulled from the Container repository to the server and it is deployed.

Docker compose vs Docker run

We can use both docker-compose or docker run to run a container. Containers created through YAML files (docker-compose) will be more structured and easier way to edit. Below show what docker-compose and docker run looks like. Both of the operations end up creating containers.

In Docker compose (LHS) we mention port binding under the property ports in yaml file whereas in Docker-run we use the flag '-p' to specify port binding. Also, we can see on the right-hand side we are explicitly mentioning the container network for mongo-express and mongo so that both containers can communicate with each other, but the containers created by docker-compose are relaying in the new container network by default.

Docker file

Dockerfile is a blueprint for creating docker images. We can create custom images for our application using Dockerfile.

FROM node:13-alpine

ENV MONGO_DB_USERNAME=admin \
    MONGO_DB_PWD=password

RUN mkdir -p /home/app
COPY ./app /home/app

CMD ["node","/home/app/server.js"]

Major contents of Dockerfile

  • FROM: we always need a base image with which our application image can be created like Node, PHP, Ubuntu etc...

  • ENV: Used to define environmental variables. Not recommended method for creating environmental variables.

  • RUN: Used to execute any Linux command inside the container.

  • COPY: Used to copy files from host to container.

  • CMD: CMD is an entry point command.

docker build -t my-app:1.0 .

The above build command is used to generate your custom image. -t flag is used for mentioning the Image name (my-app) followed by its tag(1.0). The . indicate that Dockerfile is present in the current working directory.

:octocat:

To get practical experience in creating Docker Images for your application head over to git repository demo. Docker-Container-Demo