步骤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 <span class="token keyword">from</span> a Dockerfile
commit Create a new image <span class="token keyword">from</span> a container's changes
cp Copy files<span class="token operator">/</span>folders between a container <span class="token keyword">and</span> the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events <span class="token keyword">from</span> the server
<span class="token keyword">exec</span> Run a command <span class="token keyword">in</span> a running container
export Export a container's filesystem <span class="token keyword">as</span> a tar archive
history Show the history of an image
images List images
<span class="token keyword">import</span> Import the contents <span class="token keyword">from</span> a tarball to create a filesystem image
info Display system<span class="token operator">-</span>wide information
inspect Return low<span class="token operator">-</span>level information on a container <span class="token keyword">or</span> image
kill Kill a running container
load Load an image <span class="token keyword">from</span> a tar archive <span class="token keyword">or</span> STDIN
login Log <span class="token keyword">in</span> to a Docker registry
logout Log out <span class="token keyword">from</span> a Docker registry
logs Fetch the logs of a container
network Manage Docker networks
pause Pause <span class="token builtin">all</span> processes within a container
port List port mappings <span class="token keyword">or</span> a specific mapping <span class="token keyword">for</span> the CONTAINER
ps List containers
pull Pull an image <span class="token keyword">or</span> a repository <span class="token keyword">from</span> a registry
push Push an image <span class="token keyword">or</span> a repository to a registry
rename Rename a container
restart Restart a container
rm Remove one <span class="token keyword">or</span> more containers
rmi Remove one <span class="token keyword">or</span> more images
run Run a command <span class="token keyword">in</span> a new container
save Save one <span class="token keyword">or</span> more images to a tar archive
search Search the Docker Hub <span class="token keyword">for</span> images
start Start one <span class="token keyword">or</span> more stopped containers
stats Display a live stream of container<span class="token punctuation">(</span>s<span class="token punctuation">)</span> 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 <span class="token builtin">all</span> processes within a container
update Update configuration of one <span class="token keyword">or</span> more containers
version Show the Docker version information
volume Manage Docker volumes
wait Block until a container stops<span class="token punctuation">,</span> then <span class="token keyword">print</span> its exit code
一些问题
比如 docker 输出这些信息
permission denied <span class="token keyword">while</span> trying to connect to the <span class="token constant">Docker</span> daemon socket at unix<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token operator">/</span>var<span class="token operator">/</span>run<span class="token operator">/</span>docker<span class="token punctuation">.</span>sock<span class="token punctuation">:</span> <span class="token constant">Get</span> <span class="token string">"http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json"</span><span class="token punctuation">:</span> dial unix <span class="token operator">/</span>var<span class="token operator">/</span>run<span class="token operator">/</span>docker<span class="token punctuation">.</span>sock<span class="token punctuation">:</span> connect<span class="token punctuation">:</span> permission denie
查看docker组的用户信息
sudo cat <span class="token operator">/</span>etc<span class="token operator">/</span><span class="token keyword">group</span> <span class="token operator">|</span> 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
文章评论