[Getting started with Docker from scratch] Newbies getting to know Docker for the first time

✍For readers: everyone

✍Column : Docker zero-based introductory tutorial

Table of contents

The difference between Docker containers and virtual machines

Important terms in Docker 

Install Docker on Ubuntu

Create an application in Docker

Push the image to Docker Hub

Get and run the image from Docker Hub

in conclusion


Docker is a set of platform-as-a-service (PaaS) products that use operating system-level virtualization to deliver software in packages called containers. Containers are isolated from each other and bundled with their own software, libraries, and configuration files; they can communicate with each other through clear channels. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.

The difference between Docker containers and virtual machines

1.Docker container

  • Docker containers contain binaries, libraries, and configuration files as well as the application itself.
  • They do not include a guest operating system for each container, but rely on the underlying operating system kernel, which makes the containers lightweight.
  • Containers share resources with other containers in the same host operating system and provide operating system-level process isolation.

2. Virtual machine

  • A virtual machine (VM) runs on a hypervisor, which allows multiple virtual machines and their own operating systems to run on a single computer.
  • Each virtual machine has its own copy of the operating system as well as applications and necessary binaries, making the virtual machines larger and requiring more resources.
  • They provide hardware-level process isolation and are slower to boot.

Important terms in Docker 

1.Docker Image

  • It is a file composed of multiple layers and is used to execute code in a Docker container.
  • They are a set of instructions for creating docker containers.

2.Docker Container

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

3.Docker file

  • It is a text document that contains the necessary commands that, when executed, help assemble a Docker image.
  • Docker images are created using Docker files.

4.Docker Engine

  • The software that hosts the containers is called Docker Engine.
  • Docker Engine is a client-server based application
  • The docker engine has 3 main components:
    • Server : Responsible for creating and managing Docker images, containers, networks and volumes on Docker. It's called a daemon.
    • REST API : It specifies how the application interacts 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 docker commands.

5. Docker Hub

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

Install Docker on Ubuntu

1. Remove old versions of Docker

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

2. Install 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 containing 2 files (Dockerfile and main.py file).

2. Edit main.py with the following code.

#!/usr/bin/env python3

print("Docker and GFG rock!")

3. Edit the Dockerfile using the following command.

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

4. Create a Docker image.

After creating and editing the main.py file and Dockerfile, create an image to contain your application.

$ sudo docker build -t python-test 。

The "-t" option allows defining the name of the image. "python-test" is the name we chose for the image.

5. Run the Docker image

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

$ sudo docker run python-test

Push the image to Docker Hub

1. Create an account on Docker Hub.

2. Click the "Create Repository" button, enter the file name, and then click "Create".

3. Now "tag our image" and "push it to the Docker Hub repository".

Now, run the following command to list the docker images:

$ docker images

4. Push the image to the Docker Hub repository

$ docker push afrozchakure/python-test

Get and run the image from Docker Hub

1. To remove all versions of a specific image from the local system, we use its image ID.

$ docker rmi -f af939ee31fdc

2. Now run the image, if the image does not exist on the local machine, it will get the image from docker hub.

$ docker run afrozchakure/python-test

in conclusion

Now you know the basics of Docker, the difference between virtual machines and Docker containers, and some common terms in Docker. Additionally, we have completed the process of installing Docker on the system. We created an application using Docker and pushed our image to Docker Hub. Finally, we saw how to remove a specific image from the local system and then pull the image from Docker Hub if it doesn't exist locally.

Guess you like

Origin blog.csdn.net/arthas777/article/details/133397815