Introduction to Docker container technology (1)



1. Docker technology introduction

DOCKER is a container engine built on top of LXC technology. It provides container resource isolation and security protection through kernel virtualization technology (namespace and cgroups). KVM is a virtualization technology implemented through hardware, which is implemented through the system. Resource isolation and security guarantee, occupying less system resources

Official website: https://www.docker.com/

Docker composition:

  Client and server

Docker components:

  Mirror image

  container

  warehouse



2.Comparison between Docker and Openstack

image



3. Advantages of Docker

1) Simplified procedure:

Docker allows developers to package their applications and dependent packages into a portable container, and then publish to any popular Linux machine, you can achieve virtualization. Docker has changed the way of virtualization. Under the processing of Docker containers, it only takes a few seconds to complete.

2) Diversity:
Docker helps you pack your entanglements! For example, Docker image; Docker image contains the operating environment and configuration, so Docker can simplify the deployment of multiple application instances. For example, Web applications, background applications, database applications, big data applications such as Hadoop clusters, message queues, etc. can all be packaged into a mirror deployment.

3) Saving expenses: The
era of cloud computing has made developers no longer have to configure expensive hardware in pursuit of effects. Docker has changed the mindset that high performance is necessarily high in price. The combination of Docker and cloud makes cloud space more fully utilized. It not only solves the problem of hardware management, but also changes the way of virtualization.


4 Application scenarios of Docker

1) The configuration needs to be simplified (the test environment is different from the production environment)

2) Code management (code upload and download)

3) Improve development efficiency (the installation of development environment configuration is the same as that of openstack kvm)

4) Application isolation

5) Server integration

6) Debug

7) Multi-terminal, multi-tenant

8) Need for rapid deployment and environmental consistency


5 Docker installation and configuration

Install Docker service

root@centos7 ~]# yum install docker -y
[root@centos7 ~]# systemctl start docker

Download the image file

[root@centos7 ~]# docker pull centos:latest
Trying to pull repository docker.io/library/centos ...
centos7: Pulling from docker.io/library/centos
93857f76ae30: Pull complete
Digest: sha256:4eda692c08e0a065ae91d74e82fff4af3da307b4341ad61fa61771cc4659af60
[root@centos7 ~]# docker images
REPOSITORY        TAG      IMAGE ID     CREATED     SIZE
docker.io/centos  centos7  a8493f5f50ff 3 days ago  192.5 MB

Delete mirror

[root@centos7 ~]# docker rmi a8493f5f50ff    ##容器ID


6 Docker container creation and management

1) Create a container

method one:

[root@centos7 ~]# docker run centos /bin/echo "nihao"  ##创建容器
nihao
[root@centos7 ~]# docker ps -a   ##查看所有容器
CONTAINER ID  IMAGE  COMMAND   CREATED    STATUS   PORTS    NAMES
3c113f9a4f1b centos "/bin/echo nihao" 43 seconds ago Exited (0) 41 seconds ago  boring_liskov

The container name is not specified here, it is automatically named, and the status is automatic exit


Method 2: Create a custom-named container

[root@centos7 ~]# docker run --name mgg -t -i centos /bin/bash
                             名称  分配伪终端  -i 处于打开状态
[root@2db7f1389dbd /]# ps -ef
UID   PID  PPID  C STIME TTY  TIME CMD
root   1    0  0 22:46 ?   00:00:00 /bin/bash
root   13   1  0 22:49 ?  00:00:00 ps -ef
[root@centos7 ~]# docker ps
CONTAINER ID  IMAGE   COMMAND   CREATED   STATUS  PORTS    NAMES
2db7f1389dbd  centos  "/bin/bash"  4 minutes ago   Up 4 minutes   mgg

docker ps -a displays all containers including those that are not running (same as virsh list --all)


2) Enter, exit, and start the container

[root@2db7f1389dbd /]# exit   ##退出容器
exit
[root@centos7 ~]# docker start 2db7f1389dbd   ##启动容器
2db7f1389dbd
[root@centos7 ~]# docker attach 2db7f1389dbd  ##进入容器(必须是启动状态下)
[root@2db7f1389dbd /]# hostname
2db7f1389dbd

This way of entering, the container will enter the Down state after exiting, as follows

[root@2db7f1389dbd /]# exit
exit
[root@centos7 ~]# docker ps
CONTAINER ID   IMAGE  COMMAND   CREATED    STATUS    PORTS   NAMES


3) Use the nsenter command to enter the container

[root@centos7 ~]# nsenter --help
Usage:
nsenter [options] <program> [<argument>...]
Run a program with namespaces of other processes.
Options:
-t, --target <pid>     target process to get namespaces from
-m, --mount[=<file>]   enter mount namespace
-u, --uts[=<file>]     enter UTS namespace (hostname etc)
-i, --ipc[=<file>]     enter System V IPC namespace
-n, --net[=<file>]     enter network namespace
-p, --pid[=<file>]     enter pid namespace
-U, --user[=<file>]    enter user namespace
-S, --setuid <uid>     set uid in entered namespace
-G, --setgid <gid>     set gid in entered namespace
    --preserve-credentials do not touch uids or gids
-r, --root[=<dir>]     set the root directory
-w, --wd[=<dir>]       set the working directory
-F, --no-fork          do not fork before exec'ing <program>
-Z, --follow-context   set SELinux context according to --target PID
-h, --help     display this help and exit
-V, --version  output version information and exit

Get the PID of the container

[root@centos7 ~]# docker inspect --format "{{.State.Pid}}" 2db7f1389dbd 
4580
[root@centos7 ~]# nsenter -t 4580 -u -i -n -p
[root@2db7f1389dbd ~]# hostname
2db7f1389dbd
[root@2db7f1389dbd ~]# exit
logout
[root@centos7 ~]# docker ps
CONTAINER ID  IMAGE    COMMAND    CREATED    STATUS   PORTS  NAMES
2db7f1389dbd  centos    "/bin/bash" 22 minutes ago   Up 7 minutes  mgg


4) Delete the container

[root@centos7 ~]# docker ps -a
CONTAINER ID   IMAGE  COMMAND   CREATED     STATUS    PORTS    NAMES
2db7f1389dbd  centos  "/bin/bash"  31 minutes ago  Up 16 minutes  mgg
3c113f9a4f1b  centos  "/bin/echo nihao" 38 minutes ago Exited (0) 38 minutes ago boring_liskov
[root@centos7 ~]# docker rm 3c113f9a4f1b  ##接名称也可以,删除一个停止的容器
3c113f9a4f1b
[root@centos7 ~]# docker rm -f   3c113f9a4f1b ##删除一个正在运行的容器
[root@centos7 ~]# docker ps -a          
CONTAINER ID  IMAGE   COMMAND    CREATED    STATUS    PORTS   NAMES
2db7f1389dbd    centos    "/bin/bash"    31 minutes ago   Up 16 minutes  mgg
[root@centos7 ~]# docker run --rm centos /bin/echo "hello"   ##创建时自动删除,用于测试
[root@centos7 ~]#docker --kill $(docker ps -a -q)            ##删除正在运行的容器


Recommended reading

[Strongly recommended ] Carefully organized|Public Account Article Directory Encyclopedia

TB-level (the road from Xiaobai to Da Niu) technical video resources and benefits are broadcast

Graphical rookie to Danale to achieve financial freedom and the road to the pinnacle of life

The most complete technical video resources in history are here! ! !

"Golden three silver four" toss about the interview

Maybe these will be of interest to you? ?

Migrant Workers’ "Questions and Answers" column is open, welcome to tease

Summary of public account articles in 2017

·end·


Guess you like

Origin blog.51cto.com/15127557/2668509