Docker installation and basic operation commands (detailed pictures and texts!)
Article Directory
- Docker installation and basic operation commands (detailed pictures and texts!)
- 1. Introduction to Docker
- Second, install Docker
-
- 1. Install the latest version of Docker dependent version environment
- 2. Set up the Alibaba Cloud image source of the docker community version
- 3. Install docker and set it to start automatically after booting and check the docker version
- 4. Configure Alibaba Cloud's image accelerator
- 5. Network optimization
- Three, basic commands
1. Introduction to Docker
Docker uses the Go language launched by Google for development and implementation. It is based on the Linux kernel's cgroup, namespace, and AUFS-like Union FS technologies to encapsulate and isolate processes, which is a virtualization technology at the operating system level. Since the isolated process is independent of the host and other isolated processes, it is also called a container. The initial implementation was based on LXC. Since version 0.7, LXC has been removed and the self-developed libcontainer has been used. Starting from 1.11, it has further evolved to use runC and containerd.
On the basis of containers, Docker has carried out further encapsulation, from file system, network interconnection to process isolation, etc., which greatly simplifies the creation and maintenance of containers. This makes Docker technology more portable and faster than virtual machine technology.
1. Comparison of traditional virtual machine and Docker
- The following figure and table compare the differences between Docker and traditional virtualization methods. Traditional virtual machine technology is to virtualize a set of hardware, run a complete operating system on it, and then run the required application process on the system; while the application process in the container runs directly on the host's kernel, and the container does not have its own The kernel, and there is no hardware virtualization. Therefore, containers are more portable than traditional virtual machines.
Architecture comparison between traditional virtual machine and Docker
Comparison of the characteristics of traditional virtual machines and Docker
characteristic | virtual machine | container |
---|---|---|
Start Time | slow | fast |
Capacity occupied | Large (GB level) | Small (MB level) |
system | Logical isolation | Depends on the kernel (shared) |
System support | Generally dozens | Single machine supports thousands of containers |
performance | Weaker than native | Close to native |
Safety | Strong | weak |
2. What is Docker
- Is a lightweight "virtual machine"
- Open source tools for running applications in Linux containers
The double quotes in the virtual machine here are because there is no need to install the system in the container
3. Docker usage scenarios
Packaged applications simplify deployment and
can be freely migrated away from the underlying hardware (for example: server migration from Tencent Cloud to
Alibaba Cloud) . The typical scenario of docker is mentioned on the docker website:
Automating the packaging and deployment of applications(使应用的打包与部署自动化)
Creation of lightweight, private PAAS environments(创建轻量、私密的PAAS环境)
Automated testing and continuous integration/deployment(实现自动化测试和持续的集成/部署)
Deploying and scaling web apps, databases and backend services(部署与扩展webapp、数据库和后台服务)
由于其基于LXC的轻量级虚拟化的特点,docker相比KVM之类最明显的特点就是启动快,资源占用小。因此对于构建隔离的标准化的运行环境,轻量级的PaaS(如dokku), 构建自动化测试和持续集成环境,以及一切可以横向扩展的应用(尤其是需要快速启停来应对峰谷的web应用)。
4. Why use Docker
1) More efficient use of system resources
2) Faster startup time
3) Consistent operating environment
4) Continuous delivery and deployment
5) Easier migration
6) Easier maintenance and expansion
5. Three-tier structure of cloud computing
SAAS(应用即服务) devops CI/CD 持续交付/持续集成 极速迭代产品(亚马逊11.6秒)
PAAS(平台即服务) 环境(架构) docker kubernets mysql
IAAS(基础设施即服务) 硬件(服务器、网络设备、防火墙等)虚拟化、网络虚拟化——>大二层
6. Docker's core concept and installation method
1) The core concept of Docker
- Mirror
A read-only template for the Docker container engine - Container
running instance created from image - Warehouse
A place where mirror images are centrally stored
2) Two ways to install Docker on CentOS
- Use CURL to get the Docker installation script for installation
- Use YUM repository to install Docker
Second, install Docker
1. Install the latest version of Docker dependent version environment
iptables -F
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum install -y yum-utils device-mapper-persistent-data lvm2 #在线源安装
#yum-utils提供了yum-config-manager
#Device Mapper 存储驱动程序需要device-mapper-persistent-data和lvm2
#Device Mapper是Linux内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。
2. Set up the Alibaba Cloud image source of the docker community version
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Establishing metadata cache After
updating yum source or configuring yum source, yum makecache is usually used to generate cache;
this command caches the package information locally in advance to improve the speed of searching and installing software.
yum makecache fast
3. Install docker and set it to start automatically after booting and check the docker version
yum -y install docker-ce
cd /etc/docker #这里因为还没启动docker所以还没生成目录
systemctl start docker
systemctl enable docker
cd /etc/docker
ls
ifconfig
docker version
4. Configure Alibaba Cloud's image accelerator
Alibaba Cloud official website: https://account.aliyun.com/
Log in to Alibaba Cloud -> Console -> Search for container mirroring service on it -> Click on the "Mirror Tool" drop-down in the lower left corner and click on "Mirror Accelerator" to
configure the accelerator and restart Load service
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["阿里云镜像加速地址"]
}
EOF
systemctl daemon-reload
systemctl restart docker
5. Network optimization
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
service network restart
systemctl restart docker
Three, basic commands
View docker version information
docker version
1. Operation for mirroring
Find the specified mirror
docker search 服务名
例如:
docker search nginx
Download mirror
docker pull 服务名
例如:
docker pull nginx
View mirror information
docker images
docker inspect 镜像的ID
例如
docker images
docker inspect f6d0b4767a6c
Tag the mirror
Note: The mirror ID remains unchanged after labeling
docker tag 仓库名:原镜像名 仓库名:新镜像名
例如:
docker tag nginx:latest nginx:test
docker images
docker images | grep nginx
Delete mirror
docker rmi 镜像的ID
docker rmi 仓库名:镜像名
例如:
docker rmi f6d0b4767a6c
docker images
docker rmi nginx:test
docker images
Mirror storage
docker save -o 存放镜像的位置 仓库名:镜像名
例如:
docker save -o /opt/nginx_latest nginx:latest
Loading image
方法一:
docker load < 本地导出的镜像名
方法二:
docker --input 本地导出的镜像名
例如:
docker load < nginx_latest
docker load --input nginx_latest
Upload image
docker push [OPTIONS] NAME[:TAG]
例如:按照下面的流程就可以上传到公有云,有兴趣的可以上传
#改标签
docker tag 仓库名:镜像名 用户名/仓库名:镜像名
#登录
docker login
Username: #用户名
Password: #密码
#上传
docker push 用户名/仓库名:镜像名
2. Operations for containers
View the running status of the
container. The STATUS status of the container is
up.
Exited (0) is a container that has stopped normally.
Exited (not 0) A container that has stopped abnormally.
docker ps #查看运行中的容器
docker ps -a #加-a 列出所有的容器,包括未运行的容器
Create a container
docker create [选项] 镜像运行的程序
-i:让容器的标准输入保持打开
-t:让Docker分配一个伪终端
例;
docker create -it nginx:latest /bin/bash
Start, stop, and restart the container
docker start 容器ID:启动一个或多个已经被停止的容器
docker stop 容器ID:停止一个运行中的容器
docker restart 容器ID:重启容器
例:
docker start 25827a2ed368
docker ps -a
docker stop 25827a2ed368
docker ps -a
docker restart 25827a2ed368
Container operation
docker run [选项] 镜像 [命令] [变量]
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用
-c 命令表示后面的参数将会作为字符串读入作为执行的命令
-v: 绑定一个卷
-P: 随机端口映射,容器内部端口随机映射到主机的端口
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
--name="名称": 为容器指定一个名称
--link name:alias 添加链接到另一个容器,格式“--link容器名:别名”
例如:
docker run nginx
docker run -d nginx
docker run -d nginx /bin/bash -c "ls" #命令表示后面的参数将会作为字符串读入作为执行的命令
Note: You can use run for the first time, and try to use start/stop/restart for subsequent maintenance
Enter and exit the container
Note: You can enter only if the container is running
docker exec [选项] 容器 命令
例:
docker exec -it 22550a179349 /bin/bash
docker exec -it 344fb940bebf /bin/bash
exit//退出容器
Export and import of containers
#容器导出
docker export 容器ID > 备份文件名
#容器导入(会生成镜像,而不会创建容器)
cat 备份文件名 | docker import - 仓库名:镜像名
例如:
docker export 22550a179349 > nginx_up
docker export 344fb940bebf > nginx_exited
cat nginx_up | docker import - nginx:web
Delete container
docker rm 容器ID
例如:
docker rm 4d8dcf51a4ef
//批量删除容器
docker ps -a | awk '{print "docker rm "$1}' | bash
3. Operations for private warehouses
Create a private warehouse
Modify the /etc/docker/daemon.json configuration file
Note: Be sure to add an English symbol comma after the above brackets, otherwise an error will be reported
vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.2.4:5000"], 这里添加
"registry-mirrors": ["阿里云加速地址"]
}
systemctl restart docker.service
docker create -it registry /bin/bash
docker ps -a
docker start 8480e3f87c8e
Mounting the container
Note: docker run will automatically create related directories and re-create the container
docker run -d -p 宿主机端口:容器内部端口 -v 宿主机目录:容器内目录 镜像
例:
docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry
Tagging
docker tag nginx:latest 192.168.2.4:5000/nginx
Upload the image to the private warehouse
docker push 192.168.2.4:5000/nginx
Get a list of private repositories
curl -XGET http://192.168.2.4:5000/v2/_catalog
//显示上传成功
(""repositories":[""nginx"")
Download of private repositories
先移除原有的
docker rmi 192.168.2.4:5000/nginx
再下载测试下
docker pull 192.168.2.4:5000/nginx
4. Data volume and data volume container
Data volume
Executing the docker run command will automatically create the relevant directory
docker run -v /var/www:/data1 --name test -it centos:7 /bin/bash
cd /data1/
touch test01
返回宿主机进行查看
ls /var/www/
Data volume container
//数据卷容器
docker run --name sjj -v /data1 -v /data2 -it centos:7 /bin/bash
//新容器挂载数据卷容器juan
docker run -it --volumes-from sjj --name test2 centos:7 /bin/bash
Note:
The difference between data volume and data volume container. The
data volume is mounted on the host. The
data volume container is mounted between the container and the container through –volumes-from
5. Port mapping
Random port mapping
docker run -d -P nginx
Visit
192.168.2.4:49153 in the browser to
directly access the nginx homepage
Specify port mapping
docker run -d -p 40000:80 nginx
6. Container interconnection
//创建并运行容器取名web1,端口号自动映射
docker run -itd -P --name web1 centos /bin/bash
//创建并运行容器取名web2,链接到web1和其通信进web22容器 ping web1
docker run -itd -P --name web2 --link web1:web1 centos /bin/bash
--link name:alias --link容器名:别名