一台Rocky Linux 9服务器,并有一个具有sudo 权限的非root用户
如果命令需要root权限,前面会有sudo
步骤1 - 安装Docker
Rocky Linux 9官方软件库中提供的Docker安装包可能不是最新版本,需要先更新
sudo dnf check-update
添加官方Docker仓库
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
虽然Docker没有专门的Rocky Linux仓库,但Rocky Linux是基于CentOS的,可以使用相同的仓库。添加好仓库后,安装Docker,它由三个软件包组成。
sudo dnf install docker-ce docker-ce-cli containerd.io
安装完成后,启动Docker守护程序。
sudo systemctl start docker
验证它是否正在运行
sudo systemctl status docker
输出结果应该与下面类似,显示该服务已被激活并运行
Output
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago
Docs: https://docs.docker.com
Main PID: 749 (docker)
最后,确保它在每次服务器重启时启动。
sudo systemctl enable docker
现在,安装Docker不仅给你提供了Docker服务(守护进程),还提供了docker 命令行工具,或Docker客户端
第2步 - 不使用Sudo执行Docker命令(可选)
默认情况下,运行docker 命令需要root权限--也就是说,你必须在命令前加上sudo 。它也可以由docker组中的用户运行,该组在安装Docker时自动创建。如果你试图运行docker ,但没有在前缀中加上sudo ,或者没有在docker组中,你会得到这样的输出。
Output
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
如果你想避免每次运行docker 命令时都输入sudo ,请将你的用户名添加到docker组。
sudo usermod -aG docker $(whoami)
你将需要退出Droplet,然后以同一用户身份重新登录,以启用这一变化。
如果你需要将一个用户添加到你没有登录的docker 组中,请使用明确声明该用户名。
sudo usermod -aG docker username
重启docker
sudo service docker restart
本文的其余部分假设你是以docker用户组的用户身份运行docker 命令。如果你选择不这样做,请在命令的前面加上sudo 。
验证 docker
docker
输出这些表示成功
Output
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on a container or image
kill Kill a running container
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
network Manage Docker networks
pause Pause all processes within a container
port List port mappings or a specific mapping for the CONTAINER
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart a container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop a running container
tag Tag an image into a repository
top Display the running processes of a container
unpause Unpause all processes within a container
update Update configuration of one or more containers
version Show the Docker version information
volume Manage Docker volumes
wait Block until a container stops, then print its exit code
一些问题
比如 docker 输出这些信息
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denie
查看docker组的用户信息
sudo cat /etc/group | grep docker
添加当前用户到 docker 组
sudo gpasswd -a ${USER} docker
重启
sudo service docker restart
如果提示socket文件权限不足, 则给 .sock 文件增加如下权限
也就是上面提示的那些信息
sudo chmod a+rw /var/run/docker.sock
重启
sudo service docker restart