ubuntu下安装docker

安装

参考官网:

Uninstall old versions

Older versions of Docker were called docker, docker.io, or docker-engine. If these are installed, uninstall them:

1
sudo apt-get remove docker docker-engine docker.io containerd runc

Set up the repository

  1. Update the apt package index and install packages to allow apt to use a repository over HTTPS:
1
2
3
4
5
6
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
  1. Add Docker’s official GPG key:
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.
1
2
3
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install Docker Engine

  1. Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
1
2
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. Verify that Docker Engine is installed correctly by running the hello-world image.
1
sudo docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.

免sudo使用docker

将当前用户加入docker用户组

  • 如果还没有 docker group 就添加一个:
1
sudo groupadd docker
  • 将用户加入该 group 内。然后退出并重新登录就生效啦。
1
sudo gpasswd -a ${USER} docker
  • 重启 docker 服务
1
sudo service docker restart
  • 切换当前会话到新 group 或者重启 X 会话
1
newgrp - docker

注意:最后一步是必须的,否则因为 groups 命令获取到的是缓存的组信息,刚添加的组信息未能生效,所以 docker images 执行时同样有错。

换源

1
sudo vi /etc/docker/daemon.json

使用官方镜像源(或阿里源)

1
2
3
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

重启 docker 服务

1
sudo service docker restart

查看信息:

1
docker info
-------------end-------------