简体   繁体   English

将 Docker 图像保存 IP 地址从 Windows 转移到 ZEDC9F0A5A5D57797BF68E373647438

[英]Transfering Docker image saving IP address from Windows to Linux

I'm very new to Docker (in fact I've been only using it for one day) so maybe I'm misunderstanding some basic concept but I couldn't find a solution myself.我对 Docker 很陌生(实际上我只使用了一天)所以也许我误解了一些基本概念,但我自己找不到解决方案。

Here's the problem.这就是问题所在。 I have an ASP.NET Core server application on a Windows machine.我在 Windows 机器上有一个 ASP.NET 核心服务器应用程序。 It uses MongoDB as a datastore.它使用 MongoDB 作为数据存储。 Everything works fine.一切正常。 I decided to pack all this stuff into Docker containers and put it to a Linux (Ubuntu Server 18.04) server.我决定将所有这些东西打包到 Docker 容器中,然后放到 Linux(Ubuntu Server 18.04)服务器中。 I've packed mongo to a container so now its PUBLISHED IP:PORT value is 192.168.99.100:32772 I've hardcoded this address to my ASP.NET server and also packed it to a container (IP 192.168.99.100:5000).我已经将 mongo 打包到一个容器中,所以现在它的 PUBLISHED IP:PORT 值是 192.168.99.100:32772 我已将此地址硬编码到我的 ASP.NET 服务器中,并将其打包到一个容器中。10008 9.1008.9。 Now if I run my server and mongo containers together on my Windows machine, they work just fine.现在,如果我在 Windows 机器上同时运行我的服务器和 mongo 容器,它们就可以正常工作。 The server connects to a container with the database and can do whatever it needs.服务器连接到带有数据库的容器,可以做任何它需要的事情。

But when I transfer both containers to Ubuntu and run them, the server cannot connect to the database because this IP address is not available there.但是当我将两个容器转移到 Ubuntu 并运行它们时,服务器无法连接到数据库,因为该 IP 地址在那里不可用。 I've beed googling for a few hours to find a solution and still I'm struggling with it.我已经在谷歌上搜索了几个小时以找到解决方案,但我仍然在努力解决它。 What is the correct way to go about thes IP addresses? go 关于这些 IP 地址的正确方法是什么? Is it possible to set an IP that will be the same for a container regardless of environment?无论环境如何,是否可以设置对容器相同的 IP?

I recommend using docker-compose for the purpose you described above.我建议将 docker-compose 用于上述目的。

With docker-compose, you can access the database via a service name instead of an IP (which potentially is not available on another system).使用 docker-compose,您可以通过服务名称而不是 IP(可能在其他系统上不可用)来访问数据库。 Here two links to get started这里有两个开始的链接


Updated answer (10.11.2019)更新答案 (10.11.2019)

Here a concrete example for your asp.net app:这是您的 asp.net 应用程序的具体示例:

docker-compose.yaml docker-compose.yaml

version: "3"
services:
  frontend:
    image: fqdn/aspnet:tag
    ports:
    - 8080:80
    links:
    - database
  database:
    image: mongo
    environment:
      MONGO_INITDB_DATABASE: "mydatabase"
      MONGO_INITDB_ROOT_USERNAME: "root"
      MONGO_INITDB_ROOT_PASSWORD: "example"
    volumes:
    - myMongoVolume:/data/db

volumes:
  myMongoVolume: {}

From the frontend container, you can reach the mongo db container via the service name "database" (instead of an IP).从前端容器,您可以通过服务名称“数据库”(而不是 IP)访问 mongo db 容器。 Due to the link definition in the frontend service, the frontend service will start after the linked service (database).由于前端服务中的链接定义,前端服务将在链接服务(数据库)之后启动。

Through volume definition, the mongo database will be stored in a volume that persists independently from the container lifecycle.通过卷定义,mongo 数据库将存储在独立于容器生命周期的卷中。

Additionally, I assume you want to reach the asp.net application via the host IP.此外,我假设您想通过主机 IP 访问 asp.net 应用程序。 I do not know the port that you expose in your application so I assume the default port 80. Via the ports section in the frontend, we define that container port 80 is exposed as port 8080 on the host IP.我不知道您在应用程序中公开的端口,因此我假设默认端口为 80。通过前端的端口部分,我们将容器端口 80 定义为主机 IP 上的端口 8080。 eg you can open your browser and type your host IP and port 8080 eg 127.0.0.1:8080 for localhost and reach your application.例如,您可以打开浏览器并输入您的主机 IP 和端口 8080,例如 127.0.0.1:8080 用于 localhost 并访问您的应用程序。

With docker-compose installed, you can start your app, which consists of your frontend and database service via安装 docker-compose 后,您可以通过以下方式启动您的应用程序,该应用程序由您的前端和数据库服务组成

docker-compose up

Available command options for docker-compose can be found here docker-compose的可用命令选项可在此处找到

Install instructions for docker-compose docker-compose安装说明


Updated answer (10.11.2019, v2)更新答案(10.11.2019,v2)

From the comment section来自评论区

Keep in mind that you need to connect via the servicename (eg database) and the correct port.请记住,您需要通过服务名称(例如数据库)和正确的端口进行连接。 For MongoDB that port is 27017. That would tanslate to database:27017 in your frontend config对于 MongoDB,该端口为 27017。这将转换为前端配置中的 database:27017

Q: will mongo also be available from the outside in this case?问:在这种情况下,mongo 是否也可以从外部使用?

A: No, since the service does not contain any port definition the database itself will not be directly reachable.答:不,由于服务不包含任何端口定义,因此无法直接访问数据库本身。 From a security standpoint, this is preferable.从安全的角度来看,这是更可取的。

Q: could you expain this问:你能解释一下吗

volumes:
  myMongoVolume: {}     

A: in the service definition for your database service, we have specified a volume to store the database itself to make the data independent from the container lifecycle.答:在您的数据库服务的服务定义中,我们已经指定了一个卷来存储数据库本身,以使数据独立于容器生命周期。 However just by defining a volume in the service section the volume will not be created.然而,仅仅通过在服务部分定义一个卷,该卷将不会被创建。 Through the definition in the volume section, we create the volume myMongoVolume with the default settings (indicated through {} ).通过卷部分中的定义,我们使用默认设置(通过{}指示)创建卷myMongoVolume If you would like to customize your volume you can do so in this volumes section of your docker-compose.yaml.如果您想自定义您的卷,您可以在 docker-compose.yaml 的此卷部分中进行。 More information regarding volumes can be found here可以在此处找到有关卷的更多信息

eg if you would like to use a specific storage driver for your volume or use an external storage.例如,如果您想为您的卷使用特定的存储驱动程序或使用外部存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM