如何在Ubuntu 20.04上安装和使用Docker Compose

介绍 (Introduction)

Docker simplifies the process of managing application processes in containers. While containers are similar to virtual machines in certain ways, they are more lightweight and resource-friendly. This allows developers to break down an application environment into multiple isolated services.

Docker简化了在容器中管理应用程序流程的过程。 尽管容器在某些方面类似于虚拟机,但它们更轻巧且对资源友好。 这使开发人员可以将应用程序环境分解为多个隔离的服务。

For applications depending on several services, orchestrating all the containers to start up, communicate, and shut down together can quickly become unwieldy. Docker Compose is a tool that allows you to run multi-container application environments based on definitions set in a YAML file. It uses service definitions to build fully customizable environments with multiple containers that can share networks and data volumes.

对于依赖于几种服务的应用程序,将所有容器编排在一起以一起启动,通信和关闭可能很快变得很麻烦。 Docker Compose是一个工具,可让您基于YAML文件中设置的定义运行多容器应用程序环境。 它使用服务定义来构建具有多个可以共享网络和数据量的容器的完全可自定义的环境。

In this guide, we’ll demonstrate how to install Docker Compose on an Ubuntu 20.04 server and how to get started using this tool.

在本指南中,我们将演示如何在Ubuntu 20.04服务器上安装Docker Compose以及如何开始使用此工具。

先决条件 (Prerequisites)

To follow this article, you will need:

要阅读本文,您将需要:

第1步-安装Docker Compose (Step 1 — Installing Docker Compose)

To make sure we obtain the most updated stable version of Docker Compose, we’ll download this software from its official Github repository.

为了确保我们获得Docker Compose的最新稳定版本,我们将从其官方Github存储库中下载该软件。

First, confirm the latest version available in their releases page. At the time of this writing, the most current stable version is 1.26.0.

首先,在其发行页面中确认可用的最新版本。 在撰写本文时,最新的稳定版本是1.26.0

The following command will download the 1.26.0 release and save the executable file at /usr/local/bin/docker-compose, which will make this software globally accessible as docker-compose:

以下命令将下载1.26.0发行版并将可执行文件保存在/usr/local/bin/docker-compose ,这将使该软件可以作为docker-compose全局访问:

  • sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

    sudo curl -L“ https://github.com/docker/compose/releases/download/ 1.26.0 / docker -compose-$(uname -s)-$(uname -m)” -o / usr / local / bin / docker-compose

Next, set the correct permissions so that the docker-compose command is executable:

接下来,设置正确的权限,以使docker-compose命令可执行:

  • sudo chmod +x /usr/local/bin/docker-compose

    须藤chmod + x / usr / local / bin / docker-compose

To verify that the installation was successful, you can run:

要验证安装是否成功,可以运行:

  • docker-compose --version

    docker-compose --version

You’ll see output similar to this:

您将看到类似于以下的输出:


   
   
Output
docker-compose version 1.26.0, build 8a1c60f6

Docker Compose is now successfully installed on your system. In the next section, we’ll see how to set up a docker-compose.yml file and get a containerized environment up and running with this tool.

Docker Compose现在已成功安装在您的系统上。 在下一节中,我们将介绍如何设置docker-compose.yml文件并使用此工具启动并运行容器化环境。

第2步-设置docker-compose.yml文件 (Step 2 — Setting Up a docker-compose.yml File)

To demonstrate how to set up a docker-compose.yml file and work with Docker Compose, we’ll create a web server environment using the official Nginx image from Docker Hub, the public Docker registry. This containerized environment will serve a single static HTML file.

为了演示如何设置docker-compose.yml文件并使用Docker Compose,我们将使用来自公共Docker注册表Docker Hub的官方Nginx映像创建一个Web服务器环境。 此容器化环境将提供一个静态HTML文件。

Start off by creating a new directory in your home folder, and then moving into it:

首先在您的主文件夹中创建一个新目录,然后移入该目录:

  • mkdir ~/compose-demo

    mkdir〜 / compose-demo

  • cd ~/compose-demo

    光盘〜/ compose-demo

In this directory, set up an application folder to serve as the document root for your Nginx environment:

在此目录中,设置一个应用程序文件夹作为您的Nginx环境的文档根目录:

  • mkdir app

    mkdir 应用

Using your preferred text editor, create a new index.html file within the app folder:

使用您喜欢的文本编辑器,在app文件夹中创建一个新的index.html文件:

  • nano app/index.html

    纳米app / index.html

Place the following content into this file:

将以下内容放入此文件:

~/compose-demo/app/index.html
〜/ compose-demo / app / index.html
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Docker Compose Demo</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
</head>
<body>

    <h1>This is a Docker Compose Demo Page.</h1>
    <p>This content is being served by an Nginx container.</p>

</body>
</html>

Save and close the file when you’re done. If you are using nano, you can do that by typing CTRL+X, then Y and ENTER to confirm.

完成后,保存并关闭文件。 如果您使用的是nano ,则可以通过键入CTRL+X ,然后按YENTER进行确认。

Next, create the docker-compose.yml file:

接下来,创建docker-compose.yml文件:

  • nano docker-compose.yml

    纳米docker-compose.yml

Insert the following content on your docker-compose.yml file:

docker-compose.yml文件中插入以下内容:

docker-compose.yml
docker-compose.yml
version: '3.7'
services:
  web:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./app:/usr/share/nginx/html

The docker-compose.yml file typically starts off with the version definition. This will tell Docker Compose which configuration version we’re using.

docker-compose.yml文件通常以version定义开始。 这将告诉Docker Compose我们正在使用哪个配置版本。

We then have the services block, where we set up the services that are part of this environment. In our case, we have a single service called web. This service uses the nginx:alpine image and sets up a port redirection with the ports directive. All requests on port 8000 of the host machine (the system from where you’re running Docker Compose) will be redirected to the web container on port 80, where Nginx will be running.

然后,我们有了services模块,在其中设置了该环境的一部分服务。 在我们的例子中,我们有一个称为web服务。 该服务使用nginx:alpine映像,并使用ports指令设置端口重定向。 主机端口8000 (运行Docker Compose的系统)上的所有请求都将重定向到端口80上的web容器,Nginx将在该端口上运行。

The volumes directive will create a shared volume between the host machine and the container. This will share the local app folder with the container, and the volume will be located at /usr/share/nginx/html inside the container, which will then overwrite the default document root for Nginx.

volumes指令将在主机和容器之间创建一个共享卷 。 这将与容器共享本地app文件夹,并且该卷将位于容器内的/usr/share/nginx/html中,然后将覆盖Nginx的默认文档根目录。

Save and close the file.

保存并关闭文件。

We have set up a demo page and a docker-compose.yml file to create a containerized web server environment that will serve it. In the next step, we’ll bring this environment up with Docker Compose.

我们已经设置了一个演示页面和一个docker-compose.yml文件,以创建将为其提供服务的容器化Web服务器环境。 在下一步中,我们将使用Docker Compose搭建此环境。

第3步—运行Docker Compose (Step 3 — Running Docker Compose)

With the docker-compose.yml file in place, we can now execute Docker Compose to bring our environment up. The following command will download the necessary Docker images, create a container for the web service, and run the containerized environment in background mode:

有了docker-compose.yml文件,我们现在可以执行Docker Compose来启动环境。 以下命令将下载必要的Docker映像,为web服务创建容器,并在后台模式下运行容器化环境:

  • docker-compose up -d

    docker-compose up -d

Docker Compose will first look for the defined image on your local system, and if it can’t locate the image it will download the image from Docker Hub. You’ll see output like this:

Docker Compose首先会在本地系统上查找定义的映像,如果找不到该映像,它将从Docker Hub下载该映像。 您将看到如下输出:


   
   
Output
Creating network "compose-demo_default" with the default driver Pulling web (nginx:alpine)... alpine: Pulling from library/nginx cbdbe7a5bc2a: Pull complete 10c113fb0c77: Pull complete 9ba64393807b: Pull complete c829a9c40ab2: Pull complete 61d685417b2f: Pull complete Digest: sha256:57254039c6313fe8c53f1acbf15657ec9616a813397b74b063e32443427c5502 Status: Downloaded newer image for nginx:alpine Creating compose-demo_web_1 ... done

Your environment is now up and running in the background. To verify that the container is active, you can run:

现在,您的环境已在后台启动并运行。 要验证该容器是否处于活动状态,可以运行:

  • docker-compose ps

    码头工人组成ps

This command will show you information about the running containers and their state, as well as any port redirections currently in place:

此命令将向您显示有关正在运行的容器及其状态以及当前已进行的任何端口重定向的信息:


   
   
Output
Name Command State Ports ---------------------------------------------------------------------------------- compose-demo_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp

You can now access the demo application by pointing your browser to either localhost:8000 if you are running this demo on your local machine, or your_server_domain_or_IP:8000 if you are running this demo on a remote server.

现在,如果您在本地计算机上运行此演示,则可以通过将浏览器指向localhost:8000来访问该演示应用程序,如果在远程服务器上运行此演示,则可以将your_server_domain_or_IP :8000来访问该演示应用程序。

You’ll see a page like this:

您会看到这样的页面:

Because the shared volume you’ve set up within the docker-compose.yml file keeps your app folder files in sync with the container’s document root. If you make any changes to the index.html file, they will be automatically picked up by the container and thus reflected on your browser when you reload the page.

因为您在docker-compose.yml文件中设置的共享卷使您的app文件夹文件与容器的文档根目录保持同步。 如果您对index.html文件进行任何更改,它们将被容器自动拾取,并在您重新加载页面时反映在浏览器中。

In the next step, you’ll see how to manage your containerized environment with Docker Compose commands.

在下一步中,您将看到如何使用Docker Compose命令管理您的容器化环境。

第4步—熟悉Docker Compose命令 (Step 4 — Getting Familiar with Docker Compose Commands)

You’ve seen how to set up a docker-compose.yml file and bring your environment up with docker-compose up. You’ll now see how to use Docker Compose commands to manage and interact with your containerized environment.

您已经了解了如何设置docker-compose.yml文件并docker-compose.yml docker-compose up 。 现在,您将看到如何使用Docker Compose命令来管理您的容器化环境并与之交互。

To check the logs produced by your Nginx container, you can use the logs command:

要检查Nginx容器生成的日志,可以使用logs命令:

  • docker-compose logs

    docker-撰写日志

You’ll see output similar to this:

您将看到类似于以下的输出:


   
   
Output
Attaching to compose-demo_web_1 web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up web_1 | 172.22.0.1 - - [02/Jun/2020:10:47:13 +0000] "GET / HTTP/1.1" 200 353 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" "-"

If you want to pause the environment execution without changing the current state of your containers, you can use:

如果要暂停环境执行而不更改容器的当前状态,则可以使用:

  • docker-compose pause

    docker-compose暂停

   
   
Output
Pausing compose-demo_web_1 ... done

To resume execution after issuing a pause:

要在发出暂停后恢复执行:

  • docker-compose unpause

    docker-compose取消暂停

   
   
Output
Unpausing compose-demo_web_1 ... done

The stop command will terminate the container execution, but it won’t destroy any data associated with your containers:

stop命令将终止容器的执行,但不会破坏与容器关联的任何数据:

  • docker-compose stop

    docker-compose stop

   
   
Output
Stopping compose-demo_web_1 ... done

If you want to remove the containers, networks, and volumes associated with this containerized environment, use the down command:

如果要删除与此容器化环境关联的容器,网络和卷,请使用down命令:

  • docker-compose down

    码头工人组成

   
   
Output
Removing compose-demo_web_1 ... done Removing network compose-demo_default

Notice that this won’t remove the base image used by Docker Compose to spin up your environment (in our case, nginx:alpine). This way, whenever you bring your environment up again with a docker-compose up, the process will be much faster since the image is already on your system.

请注意,这不会删除Docker Compose用来启动环境的基本映像(在我们的示例中为nginx:alpine )。 这样,每当您通过docker-compose up再次启动环境时,该过程就会更快,因为映像已在您的系统上。

In case you want to also remove the base image from your system, you can use:

如果您还想从系统中删除基本映像,则可以使用:

  • docker image rm nginx:alpine

    docker图像rm nginx:alpine

   
   
Output
Untagged: nginx:alpine Untagged: nginx@sha256:b89a6ccbda39576ad23fd079978c967cecc6b170db6e7ff8a769bf2259a71912 Deleted: sha256:7d0cdcc60a96a5124763fddf5d534d058ad7d0d8d4c3b8be2aefedf4267d0270 Deleted: sha256:05a0eaca15d731e0029a7604ef54f0dda3b736d4e987e6ac87b91ac7aac03ab1 Deleted: sha256:c6bbc4bdac396583641cb44cd35126b2c195be8fe1ac5e6c577c14752bbe9157 Deleted: sha256:35789b1e1a362b0da8392ca7d5759ef08b9a6b7141cc1521570f984dc7905eb6 Deleted: sha256:a3efaa65ec344c882fe5d543a392a54c4ceacd1efd91662d06964211b1be4c08 Deleted: sha256:3e207b409db364b595ba862cdc12be96dcdad8e36c59a03b7b3b61c946a5741a

Note: Please refer to our guide on How to Install and Use Docker for a more detailed reference on Docker commands.

注意 :请参考我们的《 如何安装和使用Docker指南》, 获取有关Docker命令的更详细的参考。

结论 (Conclusion)

In this guide, we’ve seen how to install Docker Compose and set up a containerized environment based on an Nginx web server image. We’ve also seen how to manage this environment using Compose commands.

在本指南中,我们已经了解了如何安装Docker Compose以及如何基于Nginx Web服务器映像设置容器化环境。 我们还看到了如何使用Compose命令管理此环境。

For a complete reference of all available docker-compose commands, check the official documentation.

有关所有可用的docker-compose命令的完整参考,请参阅官方文档

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值