迹忆客 EN >

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

使用 Docker 网络主机命令

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

Docker 容器通过利用在安装 Docker 期间创建的网络驱动程序来工作。 我们可用的默认驱动程序包括网桥和主机网络。

当我们在不指定网络的情况下创建容器时,它们会被添加到桥接网络中,如果我们想向主机网络或自定义网络添加网络命令,我们使用 --network 命令。

在本文中,我们将学习如何使用 --network 命令将容器添加到主机网络。 如果我们不使用此命令指定网络,我们还将了解如何将容器添加到默认网络。


创建 Nginx 项目

打开 WebStorm IDEA 并选择文件 > 新建 > 项目。 在打开的窗口中,选择 Empty Project 并将项目名称从 untitled 更改为 docker-network-host 或使用任何首选名称。

最后,按标有创建的按钮生成项目。

在当前文件夹中创建一个名为 index.html 的文件,并将以下代码复制并粘贴到该文件中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Welcome to docker networking !</h1>
</body>
</html>

在这个文件中,我们创建了一个简单的网页,当我们访问容器中运行的应用程序时,该网页会显示一个标题。 这将有助于验证我们的容器是否正在运行 Nginx 应用程序。


定义镜像

创建一个名为 Dockerfile 的文件,并将以下指令复制并粘贴到该文件中。 请注意,我们通常使用具有此名称的文件从现有图像创建自定义图像。

FROM nginx:alpine
ADD . /usr/share/nginx/html
  1. FROM - 设置基础图像,使用后续说明在其上创建我们的自定义图像。
  2. ADD - 将当前文件夹中的文件和文件夹复制到图像文件系统目标 /usr/share/nginx/html。

构建镜像

使用上面定义的 Dockerfile 构建我们的自定义图像。 使用键盘快捷键 ALT+F12 打开一个新的终端窗口,并使用此命令构建映像。

~/WebstormProjects/docker-network-host$ docker build --tag docker-network-host:latest .

该命令依次执行Dockerfile创建带有标签的镜像,我们可以在终端窗口查看每条正在运行的指令,如下。

=> [1/2] FROM docker.io/library/nginx:alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c37408642bf5d               0.1s
 => => resolve docker.io/library/nginx:alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c37408642bf5d               0.1s
 => CACHED [2/2] ADD . /usr/share/nginx/html

在默认网络驱动程序中运行容器

要在默认网络中运行我们应用程序的容器,请在终端窗口中执行以下命令。 请注意,此容器将添加到桥接网络中。

~/WebstormProjects/docker-network-host$ docker run --name docker-network-bridge-test -d -p 8000:80 docker-network-host:latest

此命令运行一个名为 docker-network-bridge-test 的容器,该容器在容器中侦听端口 80 并在主机上公开端口 8000。 要验证此容器是否已添加到桥接网络,请执行以下命令来检查桥接网络。

~/WebstormProjects/docker-network-host$ docker network inspect bridge

输出:

[
    {
        "Name": "bridge",
        "Id": "45773c7633cf28baa742ceca9c054a8dd6b4ea609dd9944d7ae12bdb57e86bcd",
        "Created": "2022-10-06T07:53:45.935633743Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "5e452da9b047ce94ff23b4841d1cb3358f34b037ef566c2f12a9eb57012a5f85": {
                "Name": "docker-network-bridge-test",
                "EndpointID": "d3491eb0c518d91be71b170e8ca0a077e07781bffbe20f3e1a1fd415eef9c288",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

在返回的 JSON 中,Containers 属性有一个名为 docker-network-bridge-test 的容器,并分配了一个 MacAddress 和一个 IPv4Address。 这与本节开头创建的容器相同。

要在浏览器上提供此应用程序,请向 localhost:8000 (http://localhost:8000/) 发出请求并确保它返回包含文本 Welcome to docker networking ! 的标题。


在主机网络中运行容器

术语主机网络意味着容器在我们主机的网络上运行,因为主机网络无法容器化容器网络。 要在主机网络中运行容器,请在终端窗口中执行以下命令。

~/WebstormProjects/docker-network-host$ docker run --rm -d --network host --name docker-network-host-test docker-network-host:latest

此命令使用值为 host 的 --network 命令在主机网络中运行名为 docker-network-host-test 的容器。 要验证此容器是否已添加到主机网络,请在终端窗口中执行以下命令。

~/WebstormProjects/docker-network-host$ docker network inspect host

输出:

[
    {
        "Name": "host",
        "Id": "4ce9d3806cd88f9d9ea446272f4338f7b1f5e7098d4d0bc2a11090c1759d1880",
        "Created": "2022-09-19T07:51:50.247290701Z",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "22d15c7eee861ec7fdfd22936c30dfe1d17b2dde52ada93256f3e30943a3ed80": {
                "Name": "docker-network-host-test",
                "EndpointID": "7bd01907c8b86a881b86ae9e9c8ad8c1d3aa9f0583adebb7d41ae15bd565fabe",
                "MacAddress": "",
                "IPv4Address": "",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

在返回的 JSON 中,Containers 属性有一个名为 docker-network-host-test 的容器,请注意,在这种情况下,该容器没有分配 MacAddress 或 IPv4Adress,因为它在主机网络上运行。 这与本节开头创建的容器相同。

要在浏览器上提供此应用程序,请向 localhost:80 (http://localhost:80/) 发出请求并确保它返回包含文本 Welcome to docker networking ! 的标题。 但是,请确保主机网络上的端口 80 已打开以接受请求。


总结

在文中,我们学习了如何在默认网络驱动桥和主机网络驱动上运行容器。 请注意,我们使用 --network 命令在默认网络以外的其他网络上添加容器。

为确保您的应用程序在新网络上运行,请确保使用的端口已打开以供连接,并且其他进程未使用该端口。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便