Docker Series - (1) principles and basic operation

docker.png

Docker is an open source application container engine, based on open source Go language, and follow Apache2.0 agreement.

Docker allows developers to package their applications and dependencies to a lightweight, portable container and then posted to any popular Linux machine, can be virtualized.

Docker container is full use of the sandbox mechanism will not have any interface with each other, compared to the virtual machine, start fast vessel speed, performance overhead is very low.

Docker architecture

First introduced to the Docker's architecture, can be represented by the following figure, mainly by Clients, Hosts, Registries composition mentioned below a few proper nouns, introduce me to one individual:

Picture1.png

Image

Docker mirroring, for creating a template Docker container. Java can be seen inside the Class class.

Container

A container or a group of independent operation. Images generated after starting Container, the object can be viewed as instances of Java classes inside. Image may generate a multiple start times Container.

Client

Docker client, using the daemon communication Docker Api and Docker's. In general desktop development, the Client and Host are described below to the same client, so that we feel are Client and Host together, in fact, is the principle of two separate modules.

Host

Docker host, a physical or virtual machine for executing Docker daemon and containers.

Registry

Docker warehouse, used to store images. The official Docker image is https://hub.docker.com/ , you can store your own Image in here after registration.

Machine

docker-machine is to create tools to help you quickly install docker environment, when we need to install Docker environment on many Taiwan Server, we can use the Machine installation.

Docker common operating procedures

Daily work, most of the operational use Docker following, mainly includes:

  • Image submitted from the local to the remote mirroring
  • Fetching an image from the remote to the local
  • Testing and mirrored locally

Picture1.png

接下来进行Docker的实际安装与操作。

安装

Docker官方在这里 https://docs.docker.com/install 给出了详细的安装流程,直接按照步骤一步步安装,一般不会有什么问题。

注意:在Ubuntu系统上进行安装时,推荐使用Install using the repository的方式安装,否则直接使用安装包安装会出现很多依赖问题。

Docker基本操作

Docker安装好了以后,我们可以运行

docker run hello-world

来启动我们的第一个docker容器,正常情况下,docker会从远端仓库下载好hello-world以后直接启动。

Screen Shot 2019-11-23 at 8.43.17 PM.png

Docker的操作主要包括两类,一类是对Image镜像的操作,另一类是对Container容器的操作。

容器与镜像的关系

首先通过下图再次认识一下容器与镜像的关系,简单来讲,镜像类似Class类,容器类似new对象。另外镜像类似于Class类,也有继承的关系,也就是说一个镜像可以基于另一个镜像,增加自己的新的功能和需求,这点在后续的制作Docker镜像和Docker文件结构中会详细讲到。

Picture1.png

镜像操作

以下是主要的镜像操作,

docker [images|rmi|tag|build|history|save|load]
  • images:列出本地镜像列表
  • rmi [镜像名:版本]:删除镜像
  • tag [镜像名:版本] [仓库]/[镜像名:版本]:标记本地镜像,将其归入某一仓库
  • build -t [镜像名:版本] [path]:Dockerfile 创建镜像
  • history [镜像名:版本]: 查看指定镜像的创建历史
  • save -o xxx.tar [镜像名:版本] / save [镜像名:版本]>xxx.tar : 将镜像保存成 tar 归档文件
  • load --input xx.tar / docker load<xxx.tar : 从归档文件加载镜像

容器操作

相比于对镜像的操作,对于容器的操作更多,

docker [run|start|stop|restart|kill|rm|pause|unpause]
  • run/create[镜像名]: 创建一个新的容器并运行一个命令
  • start/stop/restart[容器名]:启动/停止/重启一个容器
  • kill [vessel name]: container directly kill, not to the process response time
  • rm [vessel name]: Delete the vessel has stopped
  • pause / unpause [vessel name]: pause / resume the process vessel
docker [ps|inspect|exec|logs|export|import]
  • ps: View the container list (the default container to view the running, -a view all containers)
  • inspect [container name]: See configuration metadata container
  • exec -it [vessel name] / bin / bash: interworking environment into the container
  • logs --since = "2019-02-01" -f --tail = 10 [container name]: See container log
  • Copying the data path between the container and the host: cp path1 [vessel Name]
  • export -o test.tar [vessel name] / docker export [vessel name]> test.tar: file system as a tar archive
  • import test.tar [image name: the version number]: Import archive, become a mirror

The basic operation of the vessel above the mirror and, in general, the development of the more common three below,

1. Start the container and running in the background

docker run -d --name myhello-world hello-world

Screen Shot 2019-11-23 at 9.09.25 PM.png

Noted that the -dparameters are running in the background, --namespecify the name of the container.

While at runtime binding container port, such as when the container starts nginx, nginx may be bound to a port 80 of the unit 8080.

docker run -d -p 8080:80 --name nginx nginx:latest

2. Running into the container

docker exec -it nginx /bin/bash

-itInteractive start bash, this will be frequently used in the back, because often into the container for debugging.

This section describes the basic architecture Docker and operation, follow-up will introduce the file structure and Docker Docker production.


This article from the "road of sophisticated back-end" original, starting in blog http://teckee.github.io/ , please indicate the source

Search "sophisticated back-end road of" public concern number and immediately get the latest articles and 2,000 yuan worth BATJ boutique interview courses .

后端精进之路.png

Guess you like

Origin www.cnblogs.com/way2backend/p/11992646.html