Docker Overview

Photo by Ian Taylor on Unsplash

Docker Overview

Docker is a suite of Platform-as-a-Service (PaaS) tools that leverage operating system-level virtualization to provide software delivery in the form of containers. These containers are self-contained environments that isolate software, libraries, and configuration files from one another while allowing them to communicate through well-defined channels. By utilizing a single operating system kernel to run all containers, Docker requires fewer resources compared to a virtual machine.

Difference between Docker Containers and Virtual Machines

  1. Docker Containers

  • Docker containers bundle applications, libraries, and configuration files without including a guest operating system for each container. Instead, they rely on the underlying OS kernel, which makes them lightweight.

  • Containers share resources with other containers on the same host OS and offer process isolation at the OS level.

  1. Virtual Machines
  • Virtual machines run on hypervisors, which allow multiple VMs to run on a single machine with their own operating system.

  • Each VM has a copy of an operating system, application, and necessary binaries, making it significantly larger and requiring more resources.

  • VMs provide process isolation at the hardware level but are slow to boot.

Important Terminologies in Docker

  1. Docker Image

  • A Docker image is a file consisting of multiple layers that enables code execution in a Docker container. It provides a set of instructions for creating Docker containers.
  1. Docker Container
  • A Docker container is an instance of a runtime image. It enables developers to bundle applications with all necessary components, including libraries and dependencies.
  1. Docker File
  • A Docker file is a text document that contains commands for assembling a Docker image. The Docker image is created using the Docker file.
  1. Docker Engine
  • Docker Engine is the software that hosts the containers. It is a client-server application that consists of three primary components:

    • Server: A daemon process that creates and manages Docker images, containers, networks, and volumes on the Docker host.

    • REST API: Specifies how applications can interact with the server and instructs it what to do.

    • Client: A Docker command-line interface (CLI) that allows interaction with Docker using docker commands.

  1. Docker Hub
  • Docker Hub is the official online repository for Docker images. It makes it easy to find, manage, and share container images with others. Users can access pre-built images from Docker Hub or upload and share their own images.

Installing Docker on Ubuntu

1. Remove old version of Docker

$ sudo apt-get remove docker docker-engine docker.io containerd runc

2. Installing Docker Engine

$ sudo apt-get update

$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

Check if docker is successfully installed in your system

$ sudo docker run hello-world

Create an application in Docker

1. Create a folder with 2 files (Dockerfile and main.py file) in it.

2. Edit main.py with the below code.

#!/usr/bin/env python3
print("Docker and GFG rock!")

3. Edit Dockerfile with the below commands.

FROM python:latest
COPY main.py /
CMD [ "python", "./main.py" ]

4. Create a Docker image.

Once you have created and edited the main.py file and the Dockerfile, create your image to contain your application.

$ docker build -t python-test .

The ‘-t’ option allows to define the name of your image. ‘python-test’ is the name we have chosen for the image.

5. Run the Docker image

Once the image is created, your code is ready to launch.

$ docker run python-test

Push an image to Docker Hub

1. Create an Account on Docker Hub.

2. Click on the “Create Repository” button, put the name of the file, and click on “Create”.

3. Now will “tag our image” and “push it to the Docker Hub repository” which we just created.

Now, run the below command to list docker images:

$ docker images

The above will give us this result

REPOSITORY TAG IMAGE_ID CREATED SIZE afrozchakure/python-test latest c7857f97ebbd 2 hours ago 933MB

Image ID is used to tag the image. The syntax to tag the image is:

docker tag <image-id> <your dockerhub username>/python-test:latest
$ docker tag c7857f97ebbd afrozchakure/python-test:latest

4. Push image to Docker Hub repository

$ docker push afrozchakure/python-test

Fetch and run the image from Docker Hub

1. To remove all versions of a particular image from our local system, we use the Image ID for it.

$ docker rmi -f af939ee31fdc

2. Now run the image, it will fetch the image from the docker hub if it doesn’t exist on your local machine.

$ docker run afrozchakure/python-test

Conclusion

So you learnt about fundamentals of Docker, the distinctions between Virtual Machines and Docker Containers, as well as common Docker terminologies. We also walked through the steps of installing Docker on our machines, creating a Docker application, and pushing our image to Docker Hub. Finally, we discovered how to remove a specific image from our local system and then pull it from Docker Hub if it isn't available locally.

Adios 👋