如何在Ubuntu20.04上安装和使⽤Docker
介绍 (Introduction)
is an application that simplifies the process of managing application processes in containers. Containers let you run your applications in resource-isolated processes. They’re similar to virtual machines, but containers are more portable, more resource-friendly, and more dependent on the host operating system.
For a detailed introduction to the different components of a Docker container, check out .
有关Docker容器的不同组件的详细介绍,请查看 。
In this tutorial, you’ll install and use Docker Community Edition (CE) on Ubuntu 20.04. You’ll install Docker itself, work with containers and images, and push an image to a Docker Repository.
在本教程中,您将在Ubuntu 20.04上安装和使⽤Docker Community Edition(CE)。 您将安装Docker本⾝,使⽤容器和映像,然后将映像推送到Docker存储库。
先决条件 (Prerequisites)
To follow this tutorial, you will need the following:
要遵循本教程,您将需要以下内容:
One Ubuntu 20.04 server set up by following , including a sudo non-root user and a firewall.
按照设置⼀台Ubuntu 20.04服务器,包括sudo⾮root⽤户和防⽕墙。
An account on if you wish to create your own images and push them to Docker Hub, as shown in Steps 7 and 8.
如果要创建⾃⼰的映像并将其推送到Docker Hub,请在上创建⼀个帐户,如步骤7和8所⽰。
第1步-安装Docker (Step 1 — Installing Docker)
The Docker installation package available in the official Ubuntu repository may not be the latest version. To ensure we get the latest version, we’ll install Docker from the official Docker repository. To do that, we’ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.
官⽅Ubuntu存储库中提供的Docker安装软件包可能不是最新版本。 为确保获得最新版本,我们将从官⽅Docker存储库中安装Docker。为此,我们将添加⼀个新的软件包源,从Docker添加GPG密钥以确保下载有效,然后安装该软件包。
First, update your existing list of packages:
⾸先,更新您现有的软件包列表:
sudo apt update
sudo apt更新
Next, install a few prerequisite packages which let apt use packages over HTTPS:
接下来安装这让⼀些必备软件包apt使⽤的包装通过HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
sudo apt安装apt-transport-https ca-certificates curl软件-属性-常见
Then add the GPG key for the official Docker repository to your system:
然后将官⽅Docker存储库的GPG密钥添加到您的系统中:
curl -fsSL download.docker/linux/ubuntu/gpg | sudo apt-key add -
curl -fsSL download.docker/linux/ubuntu/gpg | sudo apt键添加-
Add the Docker repository to APT sources:
将Docker存储库添加到APT源:
sudo add-apt-repository "deb [arch=amd64] download.docker/linux/ubuntu focal stable"
sudo add-apt-repository“ deb [arch = amd64] download.docker/linux/ubuntu 焦点稳定版”
Next, update the package database with the Docker packages from the newly added repo:
接下来,使⽤新添加的存储库中的Docker软件包更新软件包数据库:
sudo apt update
sudo apt更新
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
确保要从Docker存储库⽽不是默认的Ubuntu存储库进⾏安装:
apt-cache policy docker-ce
apt-cache策略docker-ce
You’ll see output like this, although the version number for Docker may be different:
您将看到这样的输出,尽管Docker的版本号可能不同:
Output of apt-cache policy docker-ce
apt-cache策略docker-ce的输出
docker-ce:
Installed: (none)
Candidate: 5:19.03.9~3-0~ubuntu-focal
Version table:
5:19.03.9~3-0~ubuntu-focal 500
500 download.docker/linux/ubuntu focal/stable amd64 Packages
Notice that docker-ce is not installed, but the candidate for installation is from the Docker repository for Ubuntu 20.04 (focal).
请注意,尚未安装docker-ce ,但安装的候选⼈来⾃Ubuntu 20.04的Docker信息库( focal )。
Finally, install Docker:
最后,安装Docker:
sudo apt install docker-ce
sudo apt安装docker-ce
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
现在应该安装Docker,启动守护程序,并启⽤启动过程。 检查它是否正在运⾏:
sudo systemctl status docker
sudo systemctl状态泊坞窗
The output should be similar to the following, showing that the service is active and running:
输出应类似于以下内容,表明该服务处于活动状态并且正在运⾏:
Output
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-05-19 17:00:41 UTC; 17s ago
TriggeredBy: ● docker.socket
Docs: docs.docker
Main PID: 24321 (dockerd)
Tasks: 8
Memory: 46.4M
CGroup: /system.slice/docker.service
└─24321 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Installing Docker now gives you not just the Docker service (daemon) but also the docker command line utility, or the Docker client. We’ll explore how to use the docker command later in this tutorial.
现在,安装Docker不仅可以为您提供Docker服务(守护程序),还可以为您提供docker命令⾏实⽤程序或Docker客户端。 在本教程的后⾯,我们将探讨如何使⽤docker命令。
步骤2 —在不使⽤Sudo的情况下执⾏Docker命令(可选) (Step 2 — Executing the Docker Command Without Sudo (Optional))
By default, the docker command can only be run the root user or by a user in the docker group, whic
h is automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, you’ll get an output like this:
默认情况下,只能以root⽤户或由docker组中的⽤户运⾏docker命令,⽽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'.
If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
如果要避免在每次运⾏docker命令时键⼊sudo ,请将⽤户名添加到docker组:
sudo usermod -aG docker ${USER}
sudo usermod -aG泊坞窗$ {USER}
To apply the new group membership, log out of the server and back in, or type the following:
要应⽤新的组成员⾝份,请注销服务器并重新登录,或键⼊以下命令:
su - ${USER}
su-$ {USER}
You will be prompted to enter your user’s password to continue.
系统将提⽰您输⼊⽤户密码以继续。
Confirm that your user is now added to the docker group by typing:
通过输⼊以下命令确认您的⽤户已添加到docker组:
id -nG
id -nG
Output
sammy sudo docker
If you need to add a user to the docker group that you’re not logged in as, declare that username explicitly using:
如果需要将⽤户以未登录的⾝份添加到docker组中,请使⽤以下命令明确声明该⽤户名:
sudo usermod -aG docker username
sudo usermod -aG码头⽤户名
The rest of this article assumes you are running the docker command as a user in the docker group. If you choose not to, please prepend the commands with sudo.
本⽂的其余部分假设您以docker组中的⽤户⾝份运⾏docker命令。 如果您选择不这样做,请在命令前加上sudo 。
Let’s explore the docker command next.
接下来让我们探索docker命令。
第3步—使⽤Docker命令 (Step 3 — Using the Docker Command)
Using docker consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:
使⽤docker包括向其传递⼀系列选项和命令,后跟参数。 语法采⽤以下形式:
docker [option] [command] [arguments]
码头⼯⼈[选项] [命令] [参数]
To view all available subcommands, type:
要查看所有可⽤的⼦命令,请键⼊:
docker
码头⼯⼈
As of Docker 19, the complete list of available subcommands includes:
从Docker 19开始,可⽤⼦命令的完整列表包括:
ubuntu安装教程Output
attach Attach local standard input, output, and error streams 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 to files or directories 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 Docker objects
kill Kill one or more running containers
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
pause Pause all processes within one or more containers
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 one or more containers
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 (streamed to STDOUT by default) 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 one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
To view the options available to a specific command, type:
要查看特定命令可⽤的选项,请键⼊:
docker docker-subcommand --help
docker docker-subcommand-帮助
To view system-wide information about Docker, use:
要查看有关Docker的系统范围信息,请使⽤:
docker info
码头⼯⼈信息
发布评论