Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation and installation of Docker

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

First, unveil the mystery of Docker

Docker, an open source tool for applications running in Linux containers, is a lightweight "virtual machine" that was born in 2013 and has become the hottest project in the open source community in just a few years.

.

So why is Docker so popular? Just take a look at the following picture:

As a lightweight virtual machine, Docker has significant advantages over traditional virtual machines.

The reason why Docker has many advantages is inseparable from the characteristics of operating system virtualization. Traditional virtual machines require additional hypervisors and virtual machine operating system layers, while Docker containers are virtualized directly at the operating system level.

Usage scenarios of Docker

Now developers need to be able to easily create applications that run on cloud platforms, must be detached from the underlying hardware, and need access to these resources anytime and anywhere, which is what Docker has to offer. Docker's container technology can easily create a lightweight, portable, self-sufficient container for any application on a single host. Packaging the application through this container means simplifying the trivial repetitive work of redeploying and debugging, and greatly improves work efficiency. For example, if you migrate a server from Tencent Cloud to Aliyun, if you use Docker container technology, you only need to start the required container on the new server.

Second, the core concepts of Docker:

1. Mirror image

The mage of Docker is the basis for creating containers, similar to snapshots of virtual machines. It can be understood as a read-only template for the Docker container engine. For example, an image can be a complete CentOS operating system environment, called a CentOS image, or an application with MySQL installed. It is called a MySOL image. Wait

Doacker provides a simple mechanism to create and update existing images. Users can also download the completed application image from the Internet to use it directly.

two。 Container

Docker container (Conainer! Is a running instance created from a mirror that can be started. Stop and delete. Each container created is isolated from each other. A platform that is invisible to each other and ensures security. You can think of containers as a simple version of the Linux environment, and Docker uses containers to run and isolate applications.

3. Warehouse

Docker Repository is a place where images are stored centrally. After creating your own image, you can use the push command to upload it to a public repository (Pbli) or a private repository (Private), so that when you use the image on another machine, you just need to pull it from the repository.

Warehouse Registration Server (Registry) is a place for storing repositories, which contains multiple repositories. Each warehouse centrally stores a certain type of images and uses different Tag to distinguish them. At present, the largest public repository is Docker Hb, which stores a large number of images for users to download and use.

Third, install Docker

There are two ways to install Docker under Centos system: one is to use curl to obtain the installation script of Docker for installation. The other is to use the YUM repository to install Docker.

Note: currently, Docker only supports 64-bit operating systems.

Note: currently, Docker only supports 64-bit operating systems.

Note: currently, Docker only supports 64-bit operating systems.

Here I use Docker's own YUM source for installation

Start the installation:

[root@test /] # yum-y install docker # install docker [root @ test /] # systemctl start docker # launch service [root@test /] # systemctl enable docker # set to boot Created symlink from / etc/systemd/system/multi-user.target.wants/docker.service to / usr/lib/systemd/system/docker.service.

The installed Docker has two programs: the Docker server and the Docker client. The Docker service process manages all the containers. The Docker client acts as the remote controller of the Docker server and can be used to control the server process of the Docker. In most cases, the Docker server and client run on the same machine.

[root@test /] # docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-75.git8633870.el7.centos.x86_64 Go version: go1.9.4 Git commit: 8633870 go1.9.4 Git commit 1.13.1 Built: Fri Sep 28 19:45:08 2018 OS/Arch: linux/amd64Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-75.git8633870.el7.centos.x86_64 Go version: go1.9.4 Git commit: 8633870 Built: Fri Sep 28 19:45:08 2018 OS/Arch: linux/amd64 Experimental: false

Docker mirroring operation

The corresponding image needs to exist locally before Docker runs the container. If no local image exists, Docker will try to download it from the default image repository https://hub.docker.com, which is a public repository officially maintained by Docker.

1. Search for images:

[root@test /] # docker search dhcp # Command format docker search keyword INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATEDdocker.io docker.io/networkboot/dhcpd Suitable for running a DHCP server for you... 43 [OK] docker.io docker.io/joebiellik/dhcpd DHCP server running on Alpine Linux 15 [OK] docker.io docker.io/gns3/dhcp A DHCP container for GNS3 using dnsmasq 3 [OK]. / / omit part of the content

2. Obtain the image:

Search for a matching image, which can be downloaded locally using docker pull

[root@test /] # docker pull docker.io/networkboot/dhcpdUsing default tag: latestTrying to pull repository docker.io/networkboot/dhcpd... Latest: Pulling from docker.io/networkboot/dhcpd898c46f3b1a1: Downloading 5.602 MB/32.47 MB63366dfa0a50: Download complete 041d4cd74a92: Download complete 6e1bee0f8701: Download complete 114483241095: Downloading 3.693 MB/6.702 MBef446bdcb1f0: Waiting

3. View image information:

[root@test /] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/networkboot/dhcpd latest 52cbff801df2 8 months ago 105MB can read from the echo message: REPOSITORY: the repository to which the image belongs TAG: the tag information of the image, marking different images in the same warehouse IMAGE ID: the unique ID number of the image Uniquely identifies the image CREATED: image creation time VIRTUAL SIZE: image size

You can obtain the details of the image based on the unique ID ID number of the image:

[root@test /] # docker inspect 52cbff801df2 [{"Id": "sha256:52cbff801df2c6e2da3866d9f9476f20f190f64a0e886fbdfa79d843befa666a", "RepoTags": ["docker.io/networkboot/dhcpd:latest"],.. / / omit part of the content

The details of the image include various information such as creation time, system version, hostname, domain name, user, volume, label, operating system, device ID, etc.

For ease of use, add a new label to the local image:

[root@test /] # docker tag docker.io/networkboot/dhcpd dhcp:dhcp [root@test /] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdhcp dhcp 52cbff801df2 8 months ago 105 MBdocker.io/networkboot/dhcpd latest 52cbff801df2 8 months ago 105 MB

4. Delete the image:

Command format: docker rmi warehouse name: label or docker rmi image ID number

When using the docker rmi command followed by the ID number of the image, you must make sure that the image is not in use by the container. When deleting, the system will first delete all tags that point to the image, and then delete the image file itself. If the image is already used by the container, the correct thing to do is to delete all containers that depend on the image before deleting the image.

5. Save and load images:

Deposit out

[root@test /] # docker save-o / tmp/dhcp docker.io/networkboot/dhcpd [root@test /] # ll-d / tmp/dhcp-rw- 1 root root 107891712 Nov 25 17:56 / tmp/dhcp

Load

[root@test tmp] # docker load-- input dhcp [root@test tmp] # docker load

< dhcp 6、上传镜像: 本地存出的镜像越来越多,就需要指定一个专门存放这些镜像的地方--仓库,目前比较方便的就是公共仓库,默认上传到 DockerHub 官方仓库,需要注册使用公共仓库的账号,可以使用docker login 命令来输入用户名、密码和邮箱来注册和登陆。在上传镜像之前还需要对本地镜像添加新的标签,然后再使用docker push来进行上传。 例如: 在公共仓库上已经注册了一个账号,本例账号为test,镜像标签为docker/dhcp:centos7.3 (1)首先使用账号登录官方仓库: [root@test /]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.Username: test # 输入账号Password: # 输入密码Login Succeeded (2)成功登陆后就可以上传镜像: [root@test /]# docker push docker/dhcp:centos7.3 Docker 容器操作 容器是Docker的另一个核心概念,简单来说,容器就是镜像的一个运行实例,是独立运行的一个或一组应用及它们所必须的运行环境,包括文件系统、系统类库、shell环境等。镜像是只读模板,而容器会给这个只读模板一个额外的可写层。 1、容器的创建与启动: Docker 的创建就是镜像加载到容器的过程,Docker 的容器十分轻量级,用户可以随时创建或删除。新创建的容器默认处于停止状态,不运行任何程序,需要在其中发起一个进程来启动容器,这个进程就是该容器的唯一进程,所以当该进程结束的时候,容器也会完全停止。停止的容器可以重新启动并保留原来的修改。 [root@test /]# docker create -it docker.io/networkboot/dhcpd /bin/bash # 创建容器08ab448cd0be320b24d6878b377bfa6957127c7ffb4c7e050e64340deeee1562# -i 表示让容器的输入保持打开;-t 表示让Docker分配一个伪终端 如果此处创建容器命令报错"WARNING: IPv4 forwarding is disabled.Networking will notwork." ,就使用vi编辑器打开/usr/lib/sysctl.d/00-system.conf文件,在其中添加net.ipv4.ip_ forward=1, 然后使用service network start命令重启网络服务。 使用docker create 命令创建新容器后会返回一个唯一的ID。 [root@test /]# docker ps -a # 查看所有容器的运行状态 -a 可以列出系统最近一次启动的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES08ab448cd0be docker.io/networkboot/dhcpd "/entrypoint.sh /b..." 5 minutes ago Created keen_payne命令格式: docker start 容器的ID/名称[root@test /]# docker start 08ab448cd0be # 可跟容器的ID与名称08ab448cd0be[root@test /]# docker ps -a | grep 08ab448cd0be 08ab448cd0be docker.io/networkboot/dhcpd "/entrypoint.sh /b..." 8 minutes ago Up About a minute keen_payne# 容器启动后,可以看到容器状态一栏已经变成了 UP ,表示容器已经处于启动状态 2、容器的运行与停止: [root@test /]# docker stop 08ab448cd0be08ab448cd0be[root@test /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES08ab448cd0be docker.io/networkboot/dhcpd "/entrypoint.sh /b..." 10 minutes ago Exited (0) 5 seconds ago keen_payne# 查看容器的运行状态,可以看出容器处于Exited,中止状态。 3、容器的进入: [root@test /]# docker exec -it 08ab448cd0be /bin/bash# -i 表示让容器的输入保持打开;-t 表示让Docker分配一个伪终端root@08ab448cd0be:/# ls # 已经进入到了容器中bin boot dev entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varroot@08ab448cd0be:/# exitexit[root@test /]# 4、容器的导出与导入: 用户可以将任何一个 Docker 容器从一台机器迁移到另一台机器。在迁移过程中,首先需要将已经创建好的容器导出为文件,可以使用 docker export 命令实现,无论这个容器是处于运行还是停止均可导出。导出之后可将导出文件传输到其他机器,通过相应的导入命令实现容器的迁移。 命令格式:docker export 容器ID/名称>

File name [root@test /] # docker export 08ab448cd0be > centos7dhcp [root@test /] # ll-d centos7dhcp-rw-r--r-- 1 root root 87825920 Nov 25 19:58 centos7dhcp

The exported file is copied from machine A to machine B, and then imported using the docker import command to become a mirror.

Command format: cat file name | docker import-generated image name: label [root@test /] # cat centos7dhcp | docker import-centos7:dhcpsha256:e7193f77c973a89dd2d1046f09eb4328798f4ee9dff92ad13514fb7268bc3c27 [root@test /] # docker images | grep centos7centos7 dhcp e7193f77c973 17 seconds ago 84.7 MB

5. Delete the container:

Command format: docker rm container ID/ name [root@test /] # docker stop 08ab448cd0be08ab448cd0be [root@test /] # docker rm 08ab448cd0be08ab448cd0be [root@test /] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES# if you delete a running container, you can add the-f option to force the deletion However, it is recommended to stop the container before deleting it.

The default storage directory of Docker is / var/lib/docker, and the images, containers, logs and other contents of Docker are all stored here. You can use large-capacity partitions to store these contents, and generally choose to establish LVM logical volumes, so as to avoid the problem of insufficient storage directory capacity during Docker operation.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report