简体   繁体   English

在没有 Docker Compose 的情况下连接 Rails 和 Postgres 容器

[英]Connecting Rails and Postgres containers without Docker Compose

I know that Docker Compose (compose) is a tool that among other things makes setting up interconnectivity between containers much easier, but I am trying to practice doing that without compose to see what that process is like (and gain a deeper appreciation for compose in the long run).我知道 Docker Compose (compose) 是一种工具,除其他外,它使在容器之间建立互连变得更加容易,但我正在尝试在没有 compose 的情况下练习这样做,以了解该过程是什么样的(并更深入地了解 compose in从长远来看)。

Is the reason it is hard to run an app (in my case rails) + a sql server without compose because they are isolated units, so they don't know about each others file system and so don't know how to locate one another and that's why we use compose?是很难运行一个应用程序(在我的情况下是rails)+一个没有组合的sql服务器的原因吗,因为它们是独立的单元,所以他们不知道彼此的文件系统,所以不知道如何定位彼此这就是我们使用 compose 的原因? Is there another reason?还有别的原因吗?

How would you approach connecting the containers without using compose?您将如何在不使用 compose 的情况下连接容器? If it's helpful I posted some metadata about the two containers below.如果有帮助,我发布了一些关于下面两个容器的元数据。

When run docker inspect on my postgres container I see an ENV and network config of:在我的 postgres 容器上运行 docker 检查时,我看到一个 ENV 和网络配置:

"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/12/bin",
"PGDATA=/var/lib/postgresql/data"
]

"Networks": {
"bridge": {
"NetworkID": "e68fdafe2110067389853515b05b09cd0ce75c500190fae85abff82417794b7f",
"EndpointID": "0d5564c073286bf7c1d708d52146653cf75705f34c1e175963eba4c158424399",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"MacAddress": "02:42:ac:11:00:02",
}
}

And for the rails container:对于 rails 容器:

"Env": [
"PATH=/usr/local/bundle/bin:/usr/local/bundle/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
]

"Networks": {
"bridge": {
"NetworkID": "e68fdafe2110067389853515b05b09cd0ce75c500190fae85abff82417794b7f",
"EndpointID": "e76ad694f04b7ca07e388ab39ab81b00409500d4c175b5ce9f90a8c740da7246",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"MacAddress": "02:42:ac:11:00:03",
}
}

Docker compose is handy when you have bunch of containers and you want to start/stop whole environment by using single commands like docker-compose up or docker-compose down . Docker 当你有一堆容器并且你想通过使用docker-compose updocker-compose down等单个命令来启动/停止整个环境时,compose 非常方便。 And as you noted it also creates some common stuff to help containers talk to each other - like networks.正如您所指出的,它还创建了一些常见的东西来帮助容器相互通信——比如网络。

If you want to make it hard to yourself you can always not use compose and start your containers manually and connect them to a common network:如果你想让自己变得困难,你总是不能使用 compose 并手动启动你的容器并将它们连接到一个公共网络:

docker network create mynetwork

this will create a network named mynetwork with default bridge driver.这将使用默认bridge驱动程序创建一个名为mynetwork的网络。 Next you will have to run your containers and connect them to this network:接下来你必须运行你的容器并将它们连接到这个网络:

docker container run --network mynetwork --name container1 image1
docker container run --network mynetwork --name container2 image2

and this is similiar to what compose is doing - you can access those containers over this network by DNS names - container1 and container2 .这与 compose 所做的类似 - 您可以通过 DNS 名称 - container1container2通过此网络访问这些容器。

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

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