Introduction to Docker - GeeksforGeeks

30 Jul, 2020
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.
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.
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.
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.
It is a runtime instance of an image.
Allows developers to package applications with all parts needed such as libraries and other dependencies.
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.
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.
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.
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.
$ sudo apt-get remove docker docker-engine docker.io containerd runc

$ sudo apt-get update
$ sudo apt-get install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg-agent \
   software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable nightly test"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

 Check if docker is successfully installed in your system
$ sudo docker run hello-world

Dockerfile
main.py
Python3
#!/usr/bin/env python3  print("Docker and GFG rock!")
FROM python:latest
COPY main.py /
CMD [ "python", "./main.py" ]

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.
Once the image is created, your code is ready to launch.
$ docker run python-test

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

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

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.
Advanced Computer Subject
GBlog
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
ML | Linear Regression
System Design Tutorial
Decision Tree Introduction with example
Python | Decision tree implementation
Copying Files to and from Docker Containers
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
DSA Sheet by Love Babbar
Socket Programming in C/C++
GET and POST requests using Python
Must Do Coding Questions for Product Based Companies