Introduction to Docker

Introduction to Docker

Docker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than a virtual machine.

Difference between Docker Containers and Virtual?Machines

1. Docker Containers

  • Docker Containers contain binaries, libraries, and configuration files along with the application itself.
  • They don’t contain a guest OS for each container and rely on the underlying OS kernel, which makes the containers lightweight.
  • Containers share resources with other containers in the same host OS and provide OS-level process isolation.

2. Virtual?Machines

  • Virtual Machines (VMs) run on Hypervisors, which allow multiple Virtual Machines to run on a single machine along with its own operating system.
  • Each VM has its own copy of an operating system along with the application and necessary binaries, which makes it significantly larger and it requires more resources.
  • They provide Hardware-level process isolation and are slow to boot.

Important Terminologies in Docker?

1. Docker?Image

  • It is a file, comprised of multiple layers, used to execute code in a Docker container.
  • They are a set of instructions used to create docker containers.

2. Docker Container

  • It is a runtime instance of an image.
  • Allows developers to package applications with all parts needed such as libraries and other dependencies.

3. Docker?file

  • It is a text document that contains necessary commands which on execution helps assemble a Docker Image.
  • Docker image is created using a Docker file.

4. Docker?Engine

  • The software that hosts the containers is named Docker Engine.
  • Docker Engine is a client-server based application
  • The docker engine has?3 main?components:
  • Server: It is responsible for creating and managing Docker images, containers, networks, and volumes on the Docker. It is referred to as a daemon process.
  • REST API: It specifies how the applications can interact with the Server and instructs it what to do.
  • Client: The Client is a docker command-line interface (CLI), that allows us to interact with Docker using the docker commands.

5. Docker Hub

  • Docker Hub is the official online repository where you can find other Docker Images that are available for use.
  • It makes it easy to find, manage, and share container images with others.

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.

  • Dockerfile
  • main.py

2. Edit main.py with the below?code.

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 have learned about the basics of Docker, the difference between Virtual Machines and Docker Containers along some common terminologies in Docker. Also, we went through the installation of Docker on our systems. We created an application using Docker and pushed our image to Docker Hub. Lastly, we learned how we could remove a particular image from our local system and later pull the image from Docker Hub if it doesn’t exist locally.

要查看或添加评论,请登录

Vishal Ranaut的更多文章

  • A Gentle Intro to Rust

    A Gentle Intro to Rust

    Introduction to Learning Rust: A Gentle Hike through Programming Hills Welcome to the exciting journey of learning…

  • Unleashing the Power of AWS Lambda Functions: A Deep Dive

    Unleashing the Power of AWS Lambda Functions: A Deep Dive

    Have you ever wondered what it would be like to run code in the cloud without needing to worry about server…

  • NATS Messaging

    NATS Messaging

    What is NATS Software applications and services need to exchange data. NATS is an infrastructure that allows such data…

  • JavaScript Package Managers: NPM Vs YARN Vs PNPM

    JavaScript Package Managers: NPM Vs YARN Vs PNPM

    Package managers are software tools that help programmers and developers to install, update and uninstall packages of…

  • NFT Ticketing

    NFT Ticketing

    What Is NFT Ticketing and How Does It Work? Main Takeaways NFT tickets are digital access credentials that offer…

  • RabbitMQ vs MQTT

    RabbitMQ vs MQTT

    What is RabbitMQ? First of all RabbitMQ is a common message server that is based on Erlang and was initially created to…

    1 条评论
  • Big Data

    Big Data

    Big data databases rapidly ingest, prepare, and store large amounts of diverse data. They are responsible for…

  • The 6 Types of Working Genius

    The 6 Types of Working Genius

    Patrick Lencioni 's 6 types of working genius are listed below The Genius of Wonder (W) – People with this genius can’t…

  • Cloud Computing

    Cloud Computing

    Simply put, cloud computing is the delivery of computing services—including servers, storage, databases, networking…

  • How Smart Contracts Are Changing the Way We Do Business

    How Smart Contracts Are Changing the Way We Do Business

    Blockchain technology is changing the world, and it's only getting started. Blockchain brings forth different…

社区洞察

其他会员也浏览了