一、安装前的基本操作环境
Docker 支持 64 位版本 CentOS 7/8,并且要求内核版本不低于 3.10。 CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2
存储层驱动)无法使用,并且部分功能可能不太稳定。
使用环境:Centos 7
用户:root(不是用root用户登陆的后面命令的前面都加上 sudo)
1.更新你的系统(我这里只是练习所以随便弄,但是生产环境需要谨慎一些)
yum -y update
2. 卸载旧版本的软件包(如果之前安装过)
列出已安装的 Docker 相关包:
使用 yum list installed | grep docker 命令查看已安装的 Docker 相关包。
yum list installed | grep docker
卸载 Docker 相关包:
使用 yum remove 命令卸载旧版本的 Docker 及其相关包。
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
注:如果是新的虚拟机,之前没安装过docker的话,这步可以忽略)
二、安装启动 Docker
1. 安装必要的依赖
yum install -y yum-utils
2. 安装 Docker 的 yum 源
然后添加安装 Docker 的 Docker YUM 源
这里有两个源,由于国内的网络问题,强烈建议使用国内的 阿里 Docker YUM 源
1. 阿里 Docker yum 镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2. 官方 Docker yum 镜像源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
3. 安装 Docker
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
4. 启动 Docker 并设置开机自启
systemctl start docker
systemctl enable docker
5. 查看运行状态
systemctl status docker
三、配置镜像加速器
1.配置 Docker 使用国内镜像源
因为我们在国内,所以配置 Docker 使用国内的镜像源,以提高拉取镜像的速度。例如,使用阿里云的镜像加速器:
创建 /etc/docker/daemon.json 文件
{
"registry-mirrors": [
"https://docker.registry.cyou",
"https://docker-cf.registry.cyou",
"https://dockercf.jsdelivr.fyi",
"https://docker.jsdelivr.fyi",
"https://dockertest.jsdelivr.fyi",
"https://mirror.aliyuncs.com",
"https://dockerproxy.com",
"https://mirror.baidubce.com",
"https://docker.m.daocloud.io",
"https://docker.nju.edu.cn",
"https://docker.mirrors.sjtug.sjtu.edu.cn",
"https://docker.mirrors.ustc.edu.cn",
"https://mirror.iscas.ac.cn",
"https://docker.rainbond.cc",
"https://<your-mirror-id>.mirror.aliyuncs.com"
]
}
我们可以在阿里云官网申请一个免费的镜像加速器。需要先登录,如果没有账号就去注册一个。
将最后一行 <your-mirror-id> 替换为自己的镜像加速器 ID。
除了最后一行其他都是本地加速器,因为直接拉取太慢了,
ACR会为每一个账号(阿里云账号或RAM用户)生成一个镜像加速器地址,配置镜像加速器前,您需要获取镜像加速器地址。
-
登录 容器镜像服务控制台。
-
在左侧导航栏选择镜像工具 > 镜像加速器
-
在镜像加速器页面获取加速器地址
2. 重启 Docker
systemctl daemon-reload
systemctl restart docker
注:systemctl daemon-reload 命令用于重新加载 systemd 的配置文件
四、镜像基本命令
1. 拉取
尝试拉取 hello-world
docker pull hello-world
拉取成功(需要等一小会)
[root@localhost ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:305243c734571da2d100c8c8b3c3167a098cab6049c9a5b066b6021a60fcb966
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest
同样的道理 例如拉取 ubuntu 和 nginx 步骤都是都是一样的
拉取 nginx
docker pull nginx
拉取 ubuntu
docker pull ubuntu
2. 查看
查看拉取的镜像
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest fec8bfd95b54 4 weeks ago 78.1MB
nginx latest 60c8a892f36f 6 weeks ago 192MB
hello-world latest d2c94e258dcb 18 months ago 13.3kB
查看镜像,容器,数据卷所占用的空间
docker system df
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 3 0 269.8MB 269.8MB (100%)
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
3. 删除
删除某个镜像
docker rmi <service-name>
例:我要将 nginx 镜像删除
docker rmi nginx
[root@localhost ~]# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:bc5eac5eafc581aeda3008b4b1f07ebba230de2f27d47767129a6a905c84f470
Deleted: sha256:60c8a892f36faf6c9215464005ee6fb8cf0585f70b113c0b030f6cb497a41876
Deleted: sha256:47984982982b32672d3b0cc6ebc1016e70916a8347c79765dc2ba09ed9afc97c
Deleted: sha256:f8fffef24ebb396c3e1721168923665f594d6b0ec1270700f642155fb51179cb
Deleted: sha256:ceff183e9da02c76af52712096cbe7e26e01909f827f18141058afbf4f7e32db
Deleted: sha256:01c22c5216c94ae4a6285e21b0ccb6bb786d437aa7eb7d3e2de8a454115d17a8
Deleted: sha256:9a980991ece0116dad7650d5af48faa2f693f9277bfd99f4fb3c8c2ce0b4e27d
Deleted: sha256:d775439dbfb804d168b7ab8501c32013896d40d66b14944d2429778d995c7fe4
Deleted: sha256:c3548211b8264f8bfa47a6727043a64f1791b82ac965a284a7ea187e971a95e2
测试
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest fec8bfd95b54 4 weeks ago 78.1MB
hello-world latest d2c94e258dcb 18 months ago 13.3kB
这里可以看到 nginx 已经成功被删掉
五、拉取并进入 Ubuntu 系统
进入 Ubuntu
docker run -it --name myubuntu1 ubuntu /bin/bash
[root@localhost ~]# docker run -it --name myubuntu1 ubuntu /bin/bash
root@f4cb5bbef333:/#
root@f4cb5bbef333:/#
root@f4cb5bbef333:/#
root@f4cb5bbef333:/#
这里可以看到已经成功进入 Ubuntu 系统了
关于容器的基本命令以及私有仓库的部署以后在为你们整理分享吧(很快会再见面的)