简体   繁体   English

Docker-compose:如何连接到mongo容器

[英]Docker-compose: How to connect to mongo container

I am trying to run my simple Ruby app in Docker container (I am a Windows 10 user). 我正在尝试在Docker容器中运行我的简单Ruby应用程序(我是Windows 10用户)。 This is just a bunch of Ruby and bash scripts that operate on Mongo database. 这只是一堆在Mongo数据库上运行的Ruby和bash脚本。 I am using docker-compose to compose 3 containers - one for mongo database (using official mongo image), one for my app and one to populate the database. 我正在使用docker-compose来组成3个容器-一个用于mongo数据库(使用官方mongo映像),一个用于我的应用程序,另一个用于填充数据库。

Then I run docker-compose build and docker-compose up commands. 然后我运行docker-compose builddocker-compose up命令。 Mongo wakes up fine and it accepts data import. Mongo可以很好地唤醒并接受数据导入。 Then I open another Powershell window and run docker run -it <name> /bin/bash to get an interactive shell and run my script. 然后我打开另一个Powershell窗口并运行docker run -it <name> /bin/bash以获取一个交互式shell并运行我的脚本。 Unfortunately, they can't connect to mongo for some reason. 不幸的是,由于某种原因,他们无法连接到mongo。

My connection string looks like this: 我的连接字符串如下所示:

@client = Mongo::Client.new([ 'mongodb:27017' ], :database => 'test')

I tried different versions of it, like mongodb , mongodb://mongodb:27017 , mongodb://mongo:27017' but nothing seems to work. 我尝试了它的不同版本,例如mongodbmongodb://mongodb:27017mongodb://mongo:27017'但似乎没有任何效果。 When I run my script it runs for several seconds and then gives an error on the first line where I perform any operation on the database. 当我运行脚本时,它会运行几秒钟,然后在我对数据库执行任何操作的第一行给出错误。 Mongo doesn't show any connection attempts. Mongo没有显示任何连接尝试。

What am I doing wrong? 我究竟做错了什么?

Dockerfile Docker文件

FROM ruby:2.5
WORKDIR /usr/src/app
COPY Gemfile Gemfile.lock ./
RUN bundle install
COPY . .

Mongo-seed Dockerfile Mongo种子Dockerfile

FROM mongo
COPY test.csv /test.csv
CMD mongoimport --host mongodb -d test -c places --type csv --file /test.csv --headerline

docker-compose.yml docker-compose.yml

version: '3'
services:
  mongodb:
    container_name: mongodb
    image: mongo:latest
    volumes:
      - mongodata:/data/db
    ports:
      - 27017:27017
  web:
    build: .
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    links:
      - mongodb
    depends_on:
      - mongodb
    tty: true
  mongo-seed:
    build: ./mongo-seed
    links:
      - mongodb
volumes:
  mongodata:

Instead of spawning a new container with docker run connect to the shell of the already running one with docker exec -ti <name> bash . 可以使用docker exec -ti <name> bash连接到已经运行的容器的外壳,而不是使用docker run产生一个新容器。 Now you should be able to ping your database with ping mongodb or to execute your script. 现在,您应该能够使用ping mongodb ping您的数据库或执行您的脚本。

The reason for this is that the new container you create isn't spawned in the same network as your database. 这样做的原因是,您创建的新容器没有与数据库位于同一网络中。

https://docs.docker.com/compose/networking/ https://docs.docker.com/engine/reference/commandline/network_connect/ https://docs.docker.com/compose/networking/ https://docs.docker.com/engine/reference/commandline/network_connect/

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

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