如何将Docker容器添加到现有Docker网络 [英] How to add a docker container to an existing docker network

查看:2585
本文介绍了如何将Docker容器添加到现有Docker网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Docker面临以下挑战:我已经启动并运行了一个由docker-compose管理的postgres服务,并且工作正常。

I have the following challenge with docker: I have a postgres service up and running which is managed by a docker-compose and works just fine.

现在,我想构建其他但隔离的docker服务,这些服务连接到此postgres数据库(例如,执行分析任务)并可以从那里读取和写入数据。

Now, I want to build additional but isolated docker services that connect to this postgres database (an do an analytical task for instance) and can read and write data from there.

我知道我可以只增加docker-compose文件,但是我想保持它的整洁。因此,我的想法是将每个其他服务放入其自己的docker映像中,并将其连接到postgres数据库。这使我可以根据需要使用其他泊坞窗服务。

I understand that I could just grow my docker-compose file, however I want to keep this slim and tidy. So, my idea is to put each additional service in its own docker image and connect this to the postgres database. This allows me to use the additional docker services as I need them.

我面临的挑战是:如何从Docker容器X连接到在另一个Docker网络中运行的postgres数据库?

The challenge I have with this, is: how do I connect from docker container X to the postgres database that's running in the other docker network?

感谢您的帮助!

推荐答案

在同一个网络中放置容器很简单:

Placing containers in the same network is trivial:


docker run --name my_new_container -network my_docker_network_name my_image_name

找出正确的网络名称:

现在,泊坞窗compose基于运行该文件夹的应用程序文件夹创建一个网络。运行 docker network ls 列出所有网络。

Now, docker compose creates a network based on the app folder it was run in. Run docker network ls to list all networks.

希望这会有所帮助。

这篇关于如何将Docker容器添加到现有Docker网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆