8 min read

Introduction to Docker & Containers

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

Table of content

  1. What is docker ?
  2. Difference between Docker vs VM
  3. What is Docker Image
  4. What is Docker Container
  5. What is Docker Registries
  6. How to Download Docker Image
  7. How to run Docker Image
  8. Stop an active container
  9. Run container in Background
  10. Access specific container's Shell (CLI)
  11. Access logs from active container
  12. How to port bind host and container
  13. List all containers
  14. Restart a container
  15. Assign a name to your container
  16. Difference between private and public docker registries
What is Docker ?

Docker is an open-source containerization platform used in IT to develop, deploy, and run applications in a contained environment. It allows developers to create containers that package up applications and all the necessary dependencies into a single package, making it easier to move and deploy across different environments.

Docker works by using containerisation technology to create isolated environments called containers. Each container includes everything an application needs to run, such as code, libraries, and system tools, without requiring a full virtual machine. This makes it possible to run multiple containers on a single host operating system, allowing for efficient use of resources and easier management of applications.

Docker is used by many organizations to streamline their software development and deployment processes, allowing for faster and more reliable delivery of applications. It is widely used in cloud computing environments, where it is used to deploy and manage applications on a variety of cloud platforms.

What problem does Docker resolve ?

  • Docker solves the problem of software dependencies and deployment.
  • It allows developers to package their applications and all of their dependencies into a container.
  • Containers can be easily deployed and run consistently across different environments.
  • Before containerization, developers faced challenges with dependency management, compatibility issues, and deployment.
  • With Docker, applications and dependencies can be packaged into a single container that can be deployed on any machine that supports Docker.
  • This makes it easy to move applications between development, testing, and production environments without worrying about compatibility issues or dependencies.
  • Docker abstracts the operating system and provides a standardized environment for running applications.
Docker vs Virtual Machine (VM)

What is the difference between Docker and VM

  • Docker uses Host OS Kernel run Docker containers, whereas VM has its own kernel (Guest OS)
  • Docker image is small in size in (100~MB), whereas VM size range from (1~GB)
  • Docker load faster as it uses host Kernel. VM takes longer to boot up its Kerel.
  • VM compatible with all other OS, as It uses its own kernel when running. Whereas Docker is only compatible with Linux Distros.

Linux Docker Application can not be run on the Windows Kernel, as it would need Linux Kernel run. However, Docker developed Docker Desktop application to run Linux containers on Windows or MacOS.

Docker Desktop achieve this by running Hypervisor layer with a lightweight Linux Distro to provide Linux kernel to run Linux based containers.

🍉
Docker was originally developed for Linux OS, and most contains are Linux based.

Download the latest version of Docker Desktop based on your System specification. Docker Desktop : https://docs.docker.com/desktop/

What is Docker Image ?
  • Docker allows to package application within its environment into package that can be shared and distributed easily.
  • This includes app source code, and complete environment configuration. Example - Application (JS app) , Any services required (node, npm) and OS Layer (Linux)
  • Add environment variables, create directories, files etc.
What is Docker Container ?
  • Container is a running instance of an image in a pre-configured environment.
  • Multiple containers can be run from 1 Docker image.

Once the Docker Desktop is installed on our machine, we can access GUI as well as CLI has shown below.

Commands

  • docker images = List all Docker images
  • docker ps = List running containers
🍉
Notice the Images section has disk-usage-extension listed on GUI & CLI. 
What is Docker Registries ?

Docker registries are servers that store and distribute Docker images. A Docker image is a lightweight, standalone, and executable package that contains everything needed to run a piece of software, including code, libraries, dependencies, and configuration files.

There are number of different version of images available to download.

  • Official Images e.g. Linux, Mongo, Postgres etc.
  • Official Images maintained by Software authors
  • Docker Community

Docker Hub hosts one of the largest Docker Register, that are created by various developers like Google, AWS and communities. We can access it form the link below.

Docker Hub : https://hub.docker.com/

We don't have to register in order to download the images, simply search for the Docker Image on search field as shown below.

As any other application, all docker images has different versions of Images ranges from old version to 'latest'. It is best practice to download specific version Docker Image for stability.

How to download Docker Image

Docker Image can be downloaded from CLI using pull command as shown below

docker pull {name}:{tag}= Pull an image from a registry

If we were to download version 3.17.3 of alpine, our command would be docker pull alpine:3.17.3

pulling Docker Image from registry 
🍉
Docker Hub registry (docker.io/library) is used as default download source for Docker

We can see our recent Alpine v3.17.3 download after listing the images using docker images command.


Here's is another example of downloading lightweight Linux distro below.

Pulling image without a version would always download the latest version of image as we can see above. These are two different versions of Alpine. Now we have two images downloaded locally. Next, we will check out how to run the image.

How to Run Docker Image ?

Docker image can be run using the following command.

docker run {name}:{tag} = Creates a container from given image with version and starts it.

Running nginx container

After starting the image, we also check active containers by running docker ps command as shown below.

Docker automatically generates a random name for the container, if we don't specify one. In our case, Alpine active container named as 'Wonderful_bartik'. We can refer to container either by their name or container ID on command.

How to stop an active container ?

docker stop {container id or NAMES } = Stop active container.

How to access specific container's Shell (CLI)

docker exec -it {container id} bash = Gives access to Shell within the specified container.

How to run container in background

docker run -d {name}:{tag} = Runs container in background and prints the full container ID. We can also use --detach instead of -d command.

How to get logs from active container ?

docker logs {container ID} = Print out logs from service running inside the container, that are present at the time of execution.

Accessing logs form particular container
How to Port Bind on Docker

In order to access containers from local host, we have to port bind our local machine to container port. We can link any client port to default container port. In this case, we have linked a client port 9090 to container (Nginx) port 80.

docker run -d -p 9090:80 {container}:{flag}

-p or --publish= Publish a container's port to the host

-p {host_port}:{container_port} = Binds the container port to local host.

Once the command is entered, we might have pop-up from Windows Defender prompting to accept the connection. Click on Allow access to bind the ports.

Docker Container Port Binding

We can test the nginx port binding by going to localhost:9090 on our web browser, as shown below.

We can also confirm its working by looking at our logs. Notice the GET request made by our web browser, which indicates requests made by our web browser.

🍉
Although we can use any port we want to bind the host to container, It is best practice to use the same port on our host as container. 

How to list all containers ?
⚠️
Docker Run command creates new container every time we run it. It does not reuse the previous container.

Which means we might have lots of containers depending on how many time we have executed Docker run command. We can find out by entering docker ps -a

-a or --all = will list all containers, including active and inactive containers.

How to restart the container ?

Instead of repeatedly creating a new container using the docker run command, we can simply start a previously created container by using its container ID or name, as demonstrated below.

docker start {container id} = This will restart the container.

As we can see above, we can run multiple containers of the same application, but in different versions.

How to assign a name to your container

As you may be aware, containers typically generate random names upon creation, which can be challenging to recall and irrelevant to our application. However, we can solve this problem by assigning our desired name to the container.

--name {given name} = Assign a name to the container

This can be achieved by running the this command docker run --name nginx-app -d -p 9090:80 nginx.1.23.4

Now on we can reference this particular container by its name.

Difference between Private and Public Docker Registries

There are two types of Docker registries: public and private.

Public registries are open to anyone and are often used for sharing open-source software. Examples of public Docker registries include Docker Hub, Google Container Registry, and Amazon Elastic Container Registry.

Private registries, on the other hand, are used for storing and sharing proprietary software within an organization or team. Examples of private Docker registries include Docker Trusted Registry, Google Container Registry Private, and Amazon Elastic Container Registry Private.

Docker images can be pushed to and pulled from Docker registries using the Docker CLI or API. When a Docker image is pushed to a registry, it is assigned a unique identifier called a digest, which can be used to retrieve the image later. Docker registries also support authentication and access control, which allows users to control who can push and pull images from the registry.


We have covered the basics of Docker and Containers. In future blog posts, we will look at how to run a Docker Image in a production environment.

I hope this blog post useful 😊