观看视频教程

Docker 是开发员和管理员用于分布式应用的平台,能够实现在容器中运行应用程序。能够让您使用您的操作系统可能不支持而需要编译的特定软件,例如 Redis 或 MongoDB,或软件特定的版本。

Docker 是Plesk 的一个扩展。Plesk 能够让您运行和管理基于某些特定 Docker 镜像的容器,支持不仅在本地主机上使用 Docker 还支持在指定的远程机器上使用。

要求和局限性

  • 在 Plesk 中使用Docker需运行以下操作系统:CentOS 7、RedHat 7、Debian 8、Debian 9、Ubuntu 14.04和Ubuntu 16.04。

    在 Plesk for Windows 中,您可以使用安装在远程机器上的 Docker(请参阅 使用远程的 Docker )。

  • 您可以在于 Windows Server 2008 上运行的 Plesk 里使用 Docker。

  • 您无法在于 Docker 容器内部署的 Plesk 中使用 Docker。

  • 要在Plesk中使用远程的Docker服务,需要额外的许可证。可 单独 购买也可将其作为 Hosting Pack 的一部分进行购买。

  • Docker 只能在x64 系统上运行。

  • 不能迁移或备份 Plesk 中的 Docker 容器。但是您可以备份容器使用的数据(请查看 卷宗映射 )并下载快照。

  • 支持安装有 Update 1 Hotfix 1 (7.0.1-686) 或更新版本的Virtuozzo 7。注意从此次更新起,创建基于CentOS 7的新容器会默认启用防火墙,因为 Virtuozzo 着力推动增强安全性。Plesk 管理员应手动配置防火墙以确保将 Plesk正常运行所需的端口 打开。

How to Start Using Docker

Install the Docker extension in Plesk.

To start using it, go to Docker in the left navigation panel.

The Image Catalog

In the catalog (Docker > Docker Image Catalog), Plesk displays images from https://hub.docker.com/explore. By default, only recommended images are displayed. To view more images, use the search box or filters.

For each application, multiple versions can be available. You can run a specific version by selecting the appropriate tag, as shown below:

image-Docker-Containers

To use filters:

  1. Click the arrow image-arrow next to the search box.

  2. Specify both or one of the following: image name and repository.

    The repositories that you can select:

    • Local repository - contains local images. These are images that have already been downloaded and are now stored on the server with Docker. For details, see Managing Local Images further in this section.
    • Docker Hub - https://hub.docker.com/explore.

To run a container:

  1. Go to Docker > Docker Image Catalog.

  2. Use the search box or filters to quickly find applications in the catalog.

  3. To view the application description and documentation on Docker Hub, click the image name. This does not apply to local images.

  4. To run a specific version, click the arrow next to the Run button, and select the application version.

  5. To run the latest version of the selected application, click Run.

    If the image is stored locally, the button appears as Run (local).

    Plesk creates a container and prompts you to specify its settings, such as environment variables, and then runs it. You can cancel running by clicking Cancel on the Settings screen. For details about the settings, see Container Settings later on this page.

  6. The container appears in the list of containers at the top of the Docker Catalog page.

    image-Docker-Container-displayed

See the log (Logs) to find out what settings the container needs to run successfully.

容器设置

Note: 如果您想要更改容器设置,您则需要停止运行容器:当您保存新设置时,Plesk 会重新创建容器。

若要编辑容器设置,请进入 设置详情 > 设置

image-Docker-container-settings

限制内存

默认可在Docker容器中使用的RAM内存是不限的。若要限制使用RAM,请清空 无限 复选框,并在 内存限制 字段输入限制值,以MB为单位。

Note: 目前还不能限制Docker 容器的CPU和磁盘使用量。

Note: Docker容器是管理员级别的对象,不受订阅级别的cgroup限制 (CPU、RAM和磁盘使用量)的控制。

自动启动

如果没有选中 系统重启后自动启动 选项,那么在系统重启后,使用该容器的网站可能会中断,您则需要手动启动容器。

端口映射

默认会启用 自动端口映射 选项,容器的内部端口会被映射到主机系统上的任意端口(例如,32768)。

若要更改主机系统上的端口,请清空选项 自动端口映射 并在 手动映射 中指定其它的外部端口。如果当您清空该复选框时没有出现 手动映射 则表示容器不显示端口。

Warning: 如果配置了端口映射,Docker会与主机系统的所有网络接口上的指定端口捆绑。也就是说可从任何地方访问该应用。Docker认定应用本身会执行验证,但其实有时候不会(例如 MySQL默认不允许匿名访问,但是redis允许)。

Plesk无法决定在指定的Docker 容器中安装哪些服务,也无法控制对这些服务的访问权限。如果您想要禁止从 Plesk外部访问应用,可使用主机系统上的防火墙进行手动设置。

卷映射

Docker 卷宗是服务器上被挂载到 Docker 容器的目录,因而您拥有可以从主机系统进行访问的永久存储。Docker 卷宗中的数据可以备份,而且更重要的是,当您停止或删除容器时不会删除数据。

有关容器中的数据的更多信息请参阅 Docker 文档

若要添加卷宗映射,请指定以下项:

  • 左边的 “目的” 字段 - 进入容器内的某个目录的绝对路径。
  • 右边的 “源” 字段 - 进入服务器中您要挂载到容器的目录的绝对路径。

若要映射更多目录,请点击 添加映射

设置环境变量

容器内的应用会使用环境变量。您可能需要添加更多变量或编辑现有的变量。Plesk 允许您添加任意数量的变量。

Operations with Containers

You can do the following with containers:

  • Restart (Restart) or stop (Stop) a container. In these cases, the container will be recreated with the current settings.

    Note: If you did not save the data to the mounted volumes (see the Volume Mapping section below), the data will be lost.

  • View logs and resource consumption (Details).

  • Change container settings, such as environment variables or volume mapping (Settings or Details > Settings).

    In Plesk on CentOS, you can set a memory limit for a container (Settings > Memory limit).

  • Rename a container (Settings > Container name).

  • Recreate a container using the same or another version of the image (Details > Recreate).

  • Create an image based on a container with your custom settings (Details > Save as Image).

  • Take a snapshot of a container (Details > Download a Snapshot).

  • Remove a container (Remove or Details > Remove).

Recreating a Container

Usually, you need to recreate a container when you want to update the application to a newer version. In fact, you can rebuild a container using any application version available in the catalog, not only a newer one.

Custom settings are preserved during recreation. To preserve data used by the application inside a container, you should specify volume mapping before you recreate a container. Volume mapping enables you to have access to directories used inside a container (see Volume Mapping in container settings).

To recreate a container:

  1. Go to Docker and click Details below the container you want to recreate.
  2. Click Recreate in the container settings and specify the image version, and whether to use default environment variables.

Using Remote Docker

By default, Plesk uses Docker installed as a Plesk component. However, you can use one or more Docker services installed outside of Plesk. Note that you can use only one service at a time. The hostname of the selected server is displayed in the title of Docker Catalog in Plesk.

Note: Managing remote Docker services requires a Plesk license key add-on. Without this add-on, you can only manage the local Docker service, which is running on the Plesk server.

Configuring Remote Services

Configure the remote server that runs Docker as described in the Docker documentation to use it as a remote server in Plesk.

Managing Remote Services

The following steps are applicable to both Plesk for Linux and for Windows.

To start using Docker that runs on a remote host:

  1. Go to Tools & Settings > Docker (under Server Management).
  2. Click Add Server and specify the settings of the remote server with Docker.
  3. To start using this Docker service in Plesk, leave Set active selected.

The link to Docker will appear in the left navigation panel.

To switch between Docker services:

  1. Go to Tools & Settings > Docker (under Server Management).
  2. In the list of servers, select the server that you are going to use and click Set Active.

Alternatively, you can set the server as active while editing its settings.

image-set-active

Creating Images with Custom Settings

If you want to create a new image based on the changes that you have made to a container, use the Save as Image command. This takes a snapshot of your container, which appears as a new image in the image catalog. In this way, you can create images with custom settings such as environment variables.

To create an image from one of your containers:

Go to Docker > Details under the container name > Save as Image, and optionally specify:

  • Image name. By default, an ID will be generated and used as the name.
  • Tag. You can specify the image version here. By default, the version will be “latest”.

The created image appears in the image catalog and is marked Local image.

Managing Local Images

Local images are images that are stored by Docker on a local disk, so there is no need to download them from the Image Catalog.

An image becomes local in the following situations:

  • You have selected any version (tag) of an image and the image has started downloading. If you either later run a container or cancel running (in the Settings screen), the image is saved locally.
  • You upload an image to Docker catalog in Plesk (Upload Image in Docker Image Catalog).
  • You have created a custom image from a container (see Creating Images with Custom Settings).
  • You have built an image using the command line interface.

If Docker has at least one downloaded version from a group of versions belonging to an image, this image is marked Local image in the catalog. Run (local) means that the latest version was already downloaded. Plesk also shows how many local images exist for a product.

image-local-image

To view local images and remove outdated local images:

  1. Go to Docker > Docker Image Catalog.

  2. To find all local images, click the arrow next to the search box and select Local in the Repository filter.

  3. To view all local images of a specific product, click the link under the product name. All local images’ tags and occupied disk space will be displayed.

    image-remove-images

  4. To remove all images, click Clean All Images.

  5. To remove a specific image, click the Remove image-docker-icon-remove icon next to the image that you want to remove.

Setting up nginx to Proxy Requests from Domains to a Container

Some Docker containers expose ports so that applications in containers could be accessible via those ports.

When you use an application in Docker container on your website, you may find it inconvenient to specify the non-standard port in its URL. To avoid inconvenience, you can set up nginx to proxy requests from domains to that port, so domains can use a standard port (such as 80) and there is no need to explicitly specify the port in URL.

Requirements

  • nginx must be running in Plesk.
  • You must map the port inside a container to a port on the host system (for example, 32768) manually (Docker > select a container > Settings > clear the option Automatic port mapping).

After you have mapped the port inside a container manually to some port on your system (for example, 32768), you can set up nginx to proxy requests from domains to that port, so domains can use a standard port on nginx (for example, 80). To make this possible, add a rule for nginx in the domain settings in the following way:

Go to Websites & Domains > the domain > Proxy Rules > Add Rule and specify the following:

  • URL. Specify the URL of the website that uses an application running in a container. It can be either the main website or a part of it.
  • Container. Select the application running as a Docker container.
  • Port. Select one of the mappings that was specified in the container settings (a port inside a container mapped to a port on your system). Nginx will proxy requests to the port on the system.

 Proxy rules are implemented in the web server configuration, e.g., in the website’s file  nginx.conf (in /var/www/vhosts/system/$domain/conf/):

#extension docker begin
location ~ ^/.* {
    proxy_pass http://0.0.0.0:9080;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
}
#extension docker end

Proxy rules should work fine on servers behind NAT.

Note: Docker containers connected via Proxy Rules to a website are not counted towards the subscription’s disk space usage. An exception to this would be when a website directory is mounted to a Docker container as a volume, as in this case all files located in the container will be counted towards the disk space usage of the website.