如何在 Ubuntu 上安装 Docker
Docker
![](/data/attachment/album/202401/03/224510gfpbdwbdvbmverbg.jpg)



> 
> 了解在 Ubuntu 上安装 Docker 的两种官方方法。一种很简单，但可能会给你一个稍旧的版本。另一种方法稍显复杂，但能提供最新的稳定版本。
> 
> 
> 


使用 Docker 意味着开启一个新的计算领域，但如果你刚刚开始使用 Docker，安装可能看起来是一项艰巨的任务。


在 Ubuntu 上安装 Docker 有两种推荐的方法：


* **从 Ubuntu 的仓库安装 Docker**：简单的单行命令，但提供旧版本
* **使用 Docker 的官方仓库**：工作量稍多，但提供了最新的稳定版本


我将在本教程中讨论它们。


### 方法 1：使用 Ubuntu 的仓库安装 Docker


如果你不在意旧版本，并且不想设置和管理仓库，那么这是最适合你的方法。


从更新仓库索引开始：



```
sudo apt update

```

现在，你可以使用以下命令在 Ubuntu 中安装 Docker 以及 [Docker Compose](https://linuxhandbook.com/docker-compose-quick-start/)：



```
sudo apt install docker.io docker-compose

```


> 
> ? Docker 包被命名为 `docker.io`，因为在 Docker 出现之前就已经存在一个名为 docker（用于 Dockerlet 应用）的过渡包。因此，Docker 包必须被命名为其他名称。
> 
> 
> 


安装完成后，你可以使用以下命令检查安装的版本：



```
docker -v

```

![](/data/attachment/album/202401/03/224537c58e85lc5aear1ki.png)


如你所见，它给了我 Docker 24.0.5。


### 方法 2：在 Ubuntu 中安装最新稳定版本的 Docker


如果你想要 Docker 的最新稳定版本，那么你可以从其官方仓库安装 Docker。


#### 步骤 1：删除任何现有的 Docker 包


但在跳到安装部分之前，有必要删除所有以前安装的 Docker。


要 [卸载以前的 Docker](https://learnubuntu.com/uninstall-docker/)，请使用以下命令。



```
sudo apt remove docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc

```

#### 步骤 2：安装依赖项


第一步是安装一些必要的软件包，这些软件包将用于在本教程后面安装 Docker：



```
sudo apt install ca-certificates curl gnupg lsb-release

```

你可能已经安装了部分或全部这些软件包，但确认安装没有什么坏处。上面的命令不会伤害你。


#### 步骤 3：添加 Docker 仓库的 GPG 密钥并将其添加到 sources.list


现在，通过 `apt` 包管理器创建一个具有特殊权限的目录，用于存储加密密钥以进行包验证：



```
sudo install -m 0755 -d /etc/apt/keyrings

```

接下来，如下所示 [使用 curl 命令](https://learnubuntu.com/install-curl/) 下载并导入 Docker 的 GPG 密钥环：



```
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

```

下载 GPG 密钥后，[使用 chmod 命令更改文件权限](https://learnubuntu.com/chmod-command/)，以便系统上的每个用户都可以读取 GPG 密钥：



```
sudo chmod a+r /etc/apt/keyrings/docker.gpg

```

最后，将 Docker 仓库添加到 `sources.list.d` 文件中：



```
echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
    $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

```

就是这样！


#### 步骤 4：安装 Docker 和 Docker Compose


现在，要使你对系统所做的更改生效，请更新系统仓库：



```
sudo apt update

```

使用以下命令获取最新版本的 Docker 以及其他 Docker 组件和依赖项：



```
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

```


> 
> ? 虽然 [docker.io](http://docker.io) 包安装了大部分必需的 Docker 组件，但你需要在此处单独进行安装。
> 
> 
> 


要检查已安装的版本，请使用以下命令：



```
docker -v

```

![](/data/attachment/album/202401/03/224537zmozl44mjhtjr069.png)


测试安装的另一个方法是在 Docker 中使用 “Hello World” 镜像。


让我告诉你如何做。


### 使用 hello-world 镜像来验证 Docker 安装


运行 “Hello World” 程序是我们启动任何编程之旅时都遵循的标准做法，Docker 也是如此。


Docker 为你提供了一个 “Hello World” 镜像，你可以使用它来测试安装。


要安装并运行 “Hello World” 镜像，只需使用以下命令：



```
sudo docker run hello-world

```

![](/data/attachment/album/202401/03/224538r2tddfunnaaau6zn.png)


某些用户在执行上述命令时可能会收到错误消息“无法连接到 Docker 守护进程”：


![](/data/attachment/album/202401/03/224538tnwvx234nv2vo554.png)


在这种情况下，**重新启动系统**并再次尝试安装 Docker Hello World 镜像，它将正常工作。


### ? 额外提示：在 Ubuntu 中使用 Docker 而无需 sudo


如果你注意到了，我使用 sudo 来拉取 Docker 镜像，这并不是使用 Docker 最方便的方式。


如果你尝试在不使用 sudo 的情况下使用 Docker，则会出现错误：


![](/data/attachment/album/202401/03/224538pex5b58wggegsc5b.png)


好吧，在本节中，我将向你展示如何在没有 sudo 的情况下使用 Docker。



> 
> ? 要执行所示步骤，必须具有超级用户权限！
> 
> 
> 


第一步是使用以下命令 [创建一个名为 docker 的新组](https://learnubuntu.com/add-group/)：



```
sudo groupadd docker

```

完成后，使用以下命令将用户添加到 `docker` 组：



```
sudo usermod -aG docker <用户名>

```


> 
> ? 确保你只添加具有超级用户权限的用户。
> 
> 
> 


现在，注销并重新登录以使更改生效。但如果你使用的是虚拟机，那么必须重新启动。


就是这样！从现在开始，你可以使用 `docker` 命令而不需要 sudo，就像我运行 Docker Hello World 镜像一样：


![](/data/attachment/album/202401/03/224538ppv7jqvlz5v75qrf.png)


就这样吧。


### 这是安装 Docker 后要做的事情


如果你刚刚开始，请参阅每个用户都必须了解的 [基本 Docker 命令列表](https://linuxhandbook.com/essential-docker-commands/)：



> 
> **[21 个基本 Docker 命令](https://linuxhandbook.com/essential-docker-commands/)**
> 
> 
> 


我希望你觉得会有帮助。


*（题图：DA/759e483b-0be8-409e-bc58-ed19d99cbdd4）*




---


via: <https://itsfoss.com/install-docker-ubuntu/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Using Docker means opening a new realm of computing but if you are just getting started with Docker, the installation may seem a huge task.

There are two recommended ways of installing Docker on Ubuntu:

**Installing Docker from Ubuntu's repository**: Easy, single line command but gives a little old version**Using Docker's official repository:**Slightly more work but gives the most recent stable release

And I will be discussing both of them in this tutorial.

## Method 1: Install Docker using Ubuntu's repository

If you don't care about having a little older version and don't want to get into setting and managing repositories, then this is the best method for you.

Start with updating the repository index:

`sudo apt update `


Now, you can use the following command to install Docker as well as [Docker Compose](https://linuxhandbook.com/docker-compose-quick-start/) in Ubuntu:

`sudo apt install docker.io docker-compose`


Once you are done with the installation, you can check the installed version using the following command:

`docker -v`


![Check the installed version of docker in Ubuntu](https://itsfoss.com/content/images/2023/12/Check-the-installed-version-of-docker-in-Ubuntu.png)

As you can see, it gave me Docker version 24.0.5.

## Method 2: Install the latest stable version of Docker in Ubuntu

If you want the most recent stable version of Docker, then you can install Docker from their official repository.

### Step 1: Remove any existing Docker packages

But before you jump to the installation part, it is necessary to remove any old installation of Docker.

To [uninstall the old Docker installation](https://learnubuntu.com/uninstall-docker/), use the following command.

`sudo apt remove docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc`


### Step 2: Install dependencies

The first step is to install some essential packages which will be used to install Docker later in this tutorial:

`sudo apt install ca-certificates curl gnupg lsb-release`


You may already have some or all of these packages installed but no harm in ensuring that. The above command won't harm you.

### Step 3: Add GPG key of Docker repository and add it to sources.list

Now, create a directory with special permissions suitable for storing cryptographic keyrings by the apt package manager for package verification:

`sudo install -m 0755 -d /etc/apt/keyrings`


Next, [use the curl command](https://learnubuntu.com/install-curl/) as shown below to download and import GPG keyring for Docker:

`curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg`


After downloading the GPG keyring, [change the file permissions using the chmod command](https://learnubuntu.com/chmod-command/) so every user on the system can read the GPG keyring:

`sudo chmod a+r /etc/apt/keyrings/docker.gpg`


Finally, add the Docker repository to the `sources.list.d`

file:

```
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
```


That's it!

### Step 4: Installing Docker and Docker Compose

Now, to take effect from the changes you've made to the system, update the system repository:

`sudo apt update`


Grab the latest version of Docker along with other Docker components and dependencies using the following command:

`sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin`


To check the installed version, use the following command:

`docker -v`


![Install the latest version of Docker in Ubuntu using Docker's official repository](https://itsfoss.com/content/images/2023/12/Install-the-latest-version-of-Docker-in-Ubuntu-using-Docker-s-official-repository.png)

Another great way to test the installation is to use the Hello World image in Docker.

Lemme show you how to do it.

## Use a hello-world image to verify the Docker installation

Running a Hello World program is a standard practice that we all follow to kick-start any programming journey and the same goes for Docker.

Docker provides you with a Hello World image that you can use to test the installation.

To install and run the Hello World image, simply use the following:

`sudo docker run hello-world`


![Run hello world docker image in Ubuntu](https://itsfoss.com/content/images/2023/12/Run-hello-world-docker-image-in-Ubuntu.png)

Some users may get an error while executing the above command saying "Cannot connect to the Docker daemon":

![docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?](https://itsfoss.com/content/images/2023/12/Docker-error.png)

In that case, **reboot your system** and try again to install the Docker Hello World image and it'll work just fine.

## 💡Bonus Tip: Use docker without sudo in Ubuntu

If you noticed, I used sudo to pull the docker image which is not the most convenient way to use the Docker.

If you try to use Docker without sudo, it will give you an error:

![Docker sudo error in Ubuntu](https://itsfoss.com/content/images/2023/12/Docker-sudo-error-in-Ubuntu.png)

Well, in this section, I will show you how you can use Docker without sudo.

The first step is to [create a new group](https://learnubuntu.com/add-group/) named `Docker`

using the following:

`sudo groupadd docker`


Once done, add the user to the Docker group using the following:

`sudo usermod -aG docker <username>`


Now, log out and log back in to take effect from the changes. But if you are using a VM then reboot is a must.

That's it! From now on, you can use the docker commands without sudo like I did to run the Docker Hello World image:

![Use docker without sudo in Ubuntu](https://itsfoss.com/content/images/2023/12/Use-docker-without-sudo-in-Ubuntu.png)

There you go.

## Here's what to do after installing Docker

If you are just getting started, then refer to the [list of essential Docker commands](https://linuxhandbook.com/essential-docker-commands/) that every user must know:

[21 Essential Docker Commands [Explained With Examples]A compilation of 21 executable and informative Docker commands for your quick reference.](https://linuxhandbook.com/essential-docker-commands/)![](https://linuxhandbook.com/content/images/2021/05/Essential-Docker-Commands.png)


![](https://linuxhandbook.com/content/images/2021/05/Essential-Docker-Commands.png)

I hope you will find this helpful.