Introduction to Docker

栏目: IT技术 · 发布时间: 5年前

内容简介:In this blog we’ll try to understand one of the most popular tools used toWe will try to look at the things that make Docker so special and learn how you canIt consists of 3 main components :

Learn the why, what and how of Docker

May 17 ·5min read

Introduction to Docker

Image credits: flickr.com

In this blog we’ll try to understand one of the most popular tools used to containerize and deploy applications over the internet i.e. Docker. It makes deploying applications extremely simple.

We will try to look at the things that make Docker so special and learn how you can build, deploy, and fetch applications using Docker & Docker Hub using just a few steps.

What is Docker ?

  • It is a tool used to create, deploy and run applications by using containers .
  • Containers allows developers to package up an application with all the parts it needs .
  • Containers are isolated from one another and bundle their own software, libraries and configuration file.

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.

Introduction to Docker

Docker vs Virtual Machines (Credits: Docker.com)

ImportantTerminologiesin 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 application 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

It consists of 3 main components :

  1. 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.
  2. REST API : It specifies how the applications can interact with the Server, and instructs it what to do.
  3. Client : The Client is a docker command-line interface (CLI), that allows us to interact with Docker using the docker commands.

Introduction to Docker

Components of Docker Engine (Credits: Docker.com)

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 \
    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

Create an application in Docker

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

. 
├── Dockerfile 
└── main.py 0 directories, 2 files

2. Edit main.py with the below code.

#!/usr/bin/env python3
print(“Docker is magic!”)

3. Edit Dockerfile with the below commands.

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

4. Create the 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 choosen 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 .

Introduction to Docker

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

Introduction to Docker

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

# Run this commend 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 IDis 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 fetches the image from docker hub if it doesn’t exist on your local machine.

$ docker run afrozchakure/python-test

Conclusion:

So you have learnt about the basics of Docker, the difference between Virtual Machines and Docker Containers along with 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.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法设计与应用

算法设计与应用

迈克尔 T. 古德里奇(Michael T. Goodrich)、罗伯特·塔马契亚(Roberto Tamas / 乔海燕、李悫炜、王烁程 / 机械工业出版社 / 2017-11-20 / CNY 139.00

本书全面系统地介绍算法设计和算法应用的各个领域,内容涵盖经典数据结构、经典算法、算法分析方法、算法设计方法以及算法在各个领域的应用,还包含一些高级主题。本书采用应用驱动的方法引入各章内容,内容编排清晰合理,讲解由浅入深。此外,各章都附有巩固练习、创新练习和应用练习三种类型的题目,为读者理解和掌握算法设计和应用提供了很好的素材。 本书可作为高等院校计算机及相关专业“数据结构和算法”课程的本科生......一起来看看 《算法设计与应用》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具