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

  1. 从软件仓库安装:主流方案。官方推荐。
  2. 下载 RPM 包安装:不方便联网的情况下,适合。
  3. 使用快捷脚本安装:用于学习不错。本地开发调试尚可,但请先检查脚本以免开源投毒。千万莫用于生产环境,否则 后果自负

1. 从软件仓库安装

  1. 设置docker仓库的yum-config-manager命令包含在yum-utils所以要先安装它。
  2. dockerdevice mapper (储驱动程序) 依赖 device-mapper-persistent-datalvm2所以一并安装。
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 包安装

  1. 进入 https://download.docker.com/linux/centos/ 并找到你的 CentOS 版本。 然后进入 x86_64/stable/Packages/ 下载要安装的 Docker版本 对应的 .rpm 文件。
  2. /path/to/ 换成你自己的下载目录,然后安装 Docker Engine
 sudo yum install /path/to/package.rpm

3. 使用快捷脚本安装 后果自负

  1. 下载脚本并执行。带 DRY_RUN=1 执行脚本。学习(检查)脚本都做了什么。确保安全再真正执行。
curl -fsSL https://get.docker.com -o get-docker.sh
DRY_RUN=1 sh ./get-docker.sh
  1. 执行安装脚本。(强烈建议备份好本地数据,哪怕是学习环境,最好也先做个系统快照。)
 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

参考资料

官方文档 Install Docker Engine on CentOS
菜鸟教程 CentOS Docker 安装

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值