迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

在 Docker Compose 中添加网络模式

作者:迹忆客 最近更新:2023/04/17 浏览次数:

默认情况下,单个网络由 Docker Compose 在我们的应用程序中创建,并将每个容器作为服务添加到那里。 网络上的每个容器都可以被单个网络上的容器访问和找到。

我们可以通过在 Docker Compose 中定义网络模式来配置我们的网络。 本文将讨论如何在 Docker Compose 中使用网络模式定义和设置网络。


在 Docker 中收集网络信息

如果我们想配置一个通过特定容器或服务的网络模式,我们需要先收集额外的信息。 运行命令 docker network ls 将使我们能够列出当前的 Docker 网络。

它应该类似于下面的输出。

NETWORK ID          NAME                         DRIVER
17cc61428fef        bridge                       bridge
098522f7fce0        sample_default               bridge
1ce3c472afc6        test_default                 bridge
8fd07e456e6c        host                         host
3b5787919641        none                         null

如果我们必须通过上面提供的前一个命令知道我们的容器、服务或网络的名称或 ID,则此命令将很方便。


在 Docker Compose 中配置网络模式

在 Docker Compose 版本 3 中,我们可以通过提供 network_mode: 参数及其值在我们的 YAML 文件中使用网络模式。 因此,请确保您使用 version: 参数和值 3 或 3.0 指定我们使用的 Docker compose 版本。

例子:

version: "3"
services:
  app:
    network_mode: "host"

在上面的示例中,我们使用示例主机作为我们的网络模式,但我们可以指定容器或服务来代替。 从 Docker 官方文档中,我们可以在定义服务的网络模式时使用以下多个值:

例子:

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

由于我们之前收集了信息,因此在定义网络模式时可以使用网络名称或 ID。

例子:

network_mode: "container:sample_default"
network_mode: "container:1ce3c472afc6"

在 Docker Swarm 中配置网络模式

运行 Docker 应用程序的物理机或虚拟机的集合称为 Docker Swarm。 名为 Docker Swarm 的容器编排解决方案允许用户控制分布在不同主机上的许多容器。

一旦一组机器聚集在一起,我们仍然可以运行我们习惯的 Docker 命令,但我们集群中的设备现在将执行它们。

不幸的是,Docker Compose 和 Swarm 不能很好地结合。 因此,我们需要使用 networks 参数手动定义网络,而不是使用 network_mode 参数,它应该如下所示。

例子:

version: "3.0"
services:
    app:
        networks:
            - host

networks:
    host:
        name: [Your Network Mode Value]
        external: true

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

发布时间:2025/03/26 浏览次数:202 分类:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便