简体   繁体   中英

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).

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? Is there another reason?

How would you approach connecting the containers without using 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:

"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:

"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 . 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:

docker network create mynetwork

this will create a network named mynetwork with default bridge driver. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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