Docker 学习笔记 - 安装、运行、卸载
基础概念/名词解释
名词 | 说明 |
---|---|
image 镜像 | docker封装好的一个模板,里面是一个可运行的服务,以及运行此服务所需要的依赖环境。 |
container 容器 | 镜像运行起来后就成为一个容器。干活的都在容器里。(有点类 与实例 的意思) |
准备工作
我们在安装的是:Docker Engine > Server
我的系统是 CentOS7
账号使用的 root
- 官方做法第一步先删除旧版本。(我是
root
所以就不加sudo
了)
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
如果yum
说找不到它们,说明系统是干净的。(如果担心有历史残留或奇怪的问题可以先试下卸载)
[root@dev-192-168-10-106 ~]# yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
安装
你可以根据你的需要,以不同的方式安装Docker Engine
- 从软件仓库安装:主流方案。官方推荐。
- 下载 RPM 包安装:不方便联网的情况下,适合。
- 使用快捷脚本安装:用于学习不错。本地开发调试尚可,但请先检查脚本以免
开源投毒
。千万莫用于生产环境,否则 后果自负。
1. 从软件仓库安装
- 设置docker仓库的
yum-config-manager
命令包含在yum-utils
所以要先安装它。 docker
的device mapper
(储驱动程序) 依赖device-mapper-persistent-data
和lvm2
所以一并安装。
yum install -y yum-utils device-mapper-persistent-data lvm2
设置仓库地址,国内的我等用阿里的源肯定比官网快。
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装最新的Docker,途中有询问yes/no
,没意见就按y
即可。(不想看也可以加 -y
执行)
yum install docker-ce docker-ce-cli containerd.io
2. 下载 RPM 包安装
- 进入 https://download.docker.com/linux/centos/ 并找到你的
CentOS
版本。 然后进入x86_64/stable/Packages/
下载要安装的Docker版本
对应的.rpm
文件。 - 把
/path/to/
换成你自己的下载目录,然后安装Docker Engine
。
sudo yum install /path/to/package.rpm
3. 使用快捷脚本安装 后果自负
- 下载脚本并执行。带
DRY_RUN=1
执行脚本。学习(检查)脚本都做了什么。确保安全再真正执行。
curl -fsSL https://get.docker.com -o get-docker.sh
DRY_RUN=1 sh ./get-docker.sh
- 执行安装脚本。(强烈建议备份好本地数据,哪怕是学习环境,最好也先做个系统快照。)
sudo sh get-docker.sh
启动 Docker 服务
安装完后我们要启动服务才能用。
systemctl enable docker # 开机启动
systemctl start docker # 启动服务
systemctl status docker # 查看状态
运行 hello-world
运行 hello-world
镜像,测试 Docker Engine
是否安装正确。
docker run hello-world
执行效果:
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally # 本地找不到
latest: Pulling from library/hello-world # 从线上拉取
2db29710123e: Pull complete # 拉取完成,下面是些信息
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest
# 这下面就是 hello-world 镜像的运行效果了:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Docker 常用命令
命令 | 说明 | 例子 |
---|---|---|
docker version | 查看版本 | |
docker images | 查看本地镜像 | |
docker ps | 查看容器 | |
docker run | 运行镜像为容器(本地没有的镜像会自动从仓库拉取) | docker run hello-world |
docker pull | 手动拉镜像到本地 | docker pull hello-world |
卸载
卸载:引擎、命令行工具、容器包
yum remove docker-ce docker-ce-cli containerd.io
主机上的映像、容器、卷或自定义配置文件不会自动删除。需要自己动手。
rm -rf /var/lib/docker
rm -rf /var/lib/containerd