了解Docker编写mongodb和rails应用程序 [英] understanding Docker compose mongodb and rails application
问题描述
在Rails应用程序中有ruby,该应用程序使用mongodb和postgresql数据库.当我在本地运行它时,一切正常,但是,当我尝试在远程容器中打开时,它会引发错误消息
there is ruby on rails application which uses mongodb and postgresql databases. When I run it locally everything works fine, however when I try to open in a remote container, it throws error message
2021-03-14T20:22:27.985 + 0000失败:连接到数据库服务器时出错:没有可访问的服务器
docker-compose.yml文件定义以下服务:
the docker-compose.yml file defines following services:
redis mongodb db rails
我使用以下命令启动远程容器:
I start remote containers with following command:
docker-compose build
-构建成功
docker-compose up -d
-容器已启动并正在运行
docker-compose up -d
- containers are up and running
当我连接到 rails
容器并尝试做
when I connect to the rails
container and try to do
捆绑执行耙aws:restore_db
错误.我不知道这是怎么回事. mongodb
容器已启动并正在运行.
error mentioned above is thrown. I don't know what is wrong here. The mongodb
container is up and running.
docker-compose.yml如下:
the docker-compose.yml is mentioned below:
version: '3.4'
services:
redis:
image: redis:5.0.5
mongodb:
image: mongo:3.6.13
volumes:
- mongo-data:/data/db
db:
image: postgres:11.3
volumes:
- db-data:/var/lib/postgresql/data
rails:
build: .
image: proj:latest
depends_on:
- db
- mongodb
- redis
volumes:
- .:/proj
ports:
- "3000:3000"
tty: true
stdin_open: true
env_file:
- .env/development.env
volumes:
db-data:
mongo-data:
这是我启动所有四个远程容器的方式:
this is how I start all four remote containers:
$ docker-compose up -d
Starting proj_db_1 ... done
Starting proj_redis_1 ... done
Starting proj_mongodb_1 ... done
Starting proj_rails_1 ... done
请帮助我了解远程容器之间应该如何交互.
please help me to understand how remote containers should interact with each other.
推荐答案
您的配置应按名称指向服务,而不是本地主机上的端口.例如,如果您要以 localhost:6380
或 127.0.0.1:6380
的身份连接到 redis
,现在您需要使用 redis:6380
Your configuration should point to the services by name and not to a port on localhost. For example, if you ware connecting to redis
as localhost:6380
or 127.0.0.1:6380
, now you need to use redis:6380
如果仍然不能解决问题,则可以尝试在容器之间添加链接,以使作为解决方案提供给它们的名称成为可能.因此该文件将如下所示:
If this is still not helping, you can try to add links between containers in order the names given to them as services to be resolved. So the file will look something like this:
version: '3.4'
services:
redis:
image: redis:5.0.5
networks:
- front-end
links:
- "mongodb:mongodb"
- "db:db"
- "rails:rails"
mongodb:
image: mongo:3.6.13
volumes:
- mongo-data:/data/db
networks:
- front-end
links:
- "redis:redis"
- "db:db"
- "rails:rails"
db:
image: postgres:11.3
volumes:
- db-data:/var/lib/postgresql/data
networks:
- front-end
links:
- "redis:redis"
- "mongodb:mongodb"
- "rails:rails"
rails:
build: .
image: proj:latest
depends_on:
- db
- mongodb
- redis
volumes:
- .:/proj
ports:
- "3000:3000"
tty: true
stdin_open: true
env_file:
- .env/development.env
networks:
- front-end
links:
- "redis:redis"
- "mongodb:mongodb"
- "db:db"
volumes:
db-data:
mongo-data:
networks:
front-end:
这些链接将允许在容器中定义主机名.
The links will allow for a hostnames to be defined in the containers.
link
标志是旧的,在新版本的docker-engine中,用户定义的网络不需要.另外,在部署docker swarm的情况下,链接将被忽略.但是,由于存在旧版本的Docker和docker-compose安装,这是尝试进行故障排除的一件事.
The
link
flag is legacy, and in new versions of docker-engine it's not required for user defined networks. Also, the links will be ignored in case of docker swarm deployment. However since there are sill old installations of Docker and docker-compose, this is one thing to try in troubleshooting.
这篇关于了解Docker编写mongodb和rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!