Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。
Docker 包括三个基本概念:
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
设置稳定版本的库
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
安装完后启动
sudo systemctl start docker
查看docker版本
[root@test ~]# docker -v
Docker version 24.0.4, build 3713ee1
查看docker状态
[root@test ~]# sudo systemctl start docker
[root@test ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2023-07-11 07:22:15 UTC; 21s ago
Docs: https://docs.docker.com
Main PID: 2222 (dockerd)
Tasks: 11
Memory: 29.9M
CGroup: /system.slice/docker.service
└─2222 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Docker 官方和国内很多云服务商都提供了国内加速器服务,比如:
# 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
之后重新启动服务
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
拉取hello world
$ docker pull hello-world:latest
[root@test ~]# docker pull hello-world:latest
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:a13ec89cdf897b3e551bd9f89d499db6ff3a7f44c5b9eb8bca40da20eb4ea1fa
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
看本地仓库是否有这个库
[root@test ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 2 months ago 13.3kB
运行这个镜像的实例,即容器
[root@test ~]# docker run 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 run hello-world
也是可以的;它会先检查本地是否有这个镜像,没有的话会先从指定仓库中拉取
启动docker服务:systemctl start docker
停止docker服务:systemctl stop docker
重启docker服务:systemctl restart docker
查看docker服务状态:systemctl status docker
设置docker开机启动:systemctl enable docker
取消docker开机启动:systemctl disable docker
脚本安装
#!/bin/bash
# 移除掉旧的版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
# 删除所有旧的数据
sudo rm -rf /var/lib/docker
# 安装依赖包
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
# 添加源,使用了阿里云镜像
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 配置缓存
sudo yum makecache fast
# 安装最新稳定版本的docker
sudo yum install -y docker-ce
# 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF
# 启动docker引擎并设置开机启动
sudo systemctl start docker
sudo systemctl enable docker
# 配置当前用户对docker的执行权限
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker