由于最近一段时间需要使用到docker, 之前计算是9月8日左右了解完相关的知识点,但是由于一些原因,到现在还没有整体梳理一遍.
1. docker 是什么?
docker 是一个轻量级,容器化,可移植的镜像。可用于开发测试发布隔离处理。docker是使用GO语言实现的。
docker engine 是一个C/S应用程序。
主要有3大块组成。
-
- docker daemon
-
- rest API
-
- docker cli
- docker cli
Docker architecture:
https://docs.docker.com/get-started/overview/
涉及到这几个概念:
docker daemon, docker client ,docker registries, images, container .
其中 images 和container 特别说明。
images:静态不可修改的,可以理解为Java 中Class的概念,雷同
官网说明:
An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.
contaner:images运行的实例, 可以理解为Java 中Object对象实例
A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.
A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.
使用到的底层技术
-
- namespaces --> container 容器隔离
-
- control group --> 用于控制container 资源分配
-
- union file system --> container layer 文件系统格式
-
- container format --> container 存储格式
Docker Engine combines the namespaces, control groups, and UnionFS into a wrapper called a container format. The default container format is libcontainer.
2 centos7 安装docker
具体可参考官网 https://docs.docker.com/install/linux/docker-ce/centos/, 这边使用在线安装
- 1 先卸载系统中存在的docker相关内容
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
- 2 安装必要的依赖
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
- 3 设置docker仓库,可以使用官网,或则使用阿里云仓库, 这一步根据国内情况,跟官网有点不一样
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
这个使用官网的地址,可以使用阿里云的加速器
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
- 4 安装docker
yum install -y docker-ce docker-ce-cli containerd.io
- 5 启动docker
systemctl start docker
- 6 检测是否安装成功
docker --version
- 7 卸载docker
## 卸载服务
yum remove docker-ce docker-ce-cli containerd.io
### 删除容器文件,持久化相关文件
rm -rf /var/lib/docker
2. docker 基础命令
systemctl start docker 启动docker 服务
docker pull 拉取镜像到本地
docker run 根据某个镜像创建容器
-d 让容器在后台运行,其实就是一个进程
--name 给容器指定一个名字
-p 将容器的端口映射到宿主机的端口
docker exec -it 进入到某个容器中并交互式运行
docker ps 查看当前活跃的容器
docker images 查看本地的镜像