3

I'm trying to use Docker to containerize a web application that uses a Flask web server and a MongoDB database.

Within the Flask server, I attempt to connect to Mongo using an environment variable named MONGO_URI:

db = MongoClient(os.environ['MONGO_URI'], connect=False)['cat_database']

Within the container, I attempt to connect to Mongo by setting a MONGO_URI environment variable that references a service name. Full docker-compose.yml:

Full docker-compose.yml:

version: '2'

services:
  mongo_service:
    image: mongo

  web:
    # link the web container to the mongo_service container
    links:
      - mongo_service
    # explicitly declare service dependencies
    depends_on:
      - mongo_service
    # set environment variables
    environment:
      PYTHONUNBUFFERED: 'true'
    volumes:
      - docker-data/app/
    # use the image from the Dockerfile in the cwd
    build: .
    command:
      - echo "success!"
    ports:
      - '8000:8000'

Full Dockerfile:

# Specify base image
FROM andreptb/oracle-java:8-alpine

# Specify author / maintainer
MAINTAINER Douglas Duhaime <douglas.duhaime@gmail.com>

# Add the cwd to the container's app directory
ADD . "/app"

# Use /app as the container's working directory
WORKDIR "/app"

# Test that the mongo_service host is defined

RUN apk add --update --no-cache curl

RUN curl "mongo_service:27017"

This returns:

Could not resolve host: mongo_service

Does anyone know what I'm doing wrong, or what I can do to get the server to connect to Mongo? I'd be very grateful for any advice others can offer!

Docker version: Docker version 17.12.0-ce, build c97c6d6
Docker-compose version: docker-compose version 1.18.0, build 8dd22a9

duhaime
  • 25,611
  • 17
  • 169
  • 224
  • When you do `docker ps` and then `docker inspect ` on both of these services, there should be a `Network` section somewhere. Anything iffy here? When you do `docker exec -it bash`, can you ping the other container with any of the names listed in e.g. "Aliases" in the above-mentioned `Network` section? (Also I thought it was slightly odd that you built both services from `.`, but should be okay.) – sneep Mar 23 '18 at 15:46

2 Answers2

7

The depends_on section is only for controlling startup order.

A links or networks section is also required to allow the containers to talk to each order.

Update the web section of the docker-compose.yml file to add the link to the mongo_service container:

...
  web:
    depends_on:
      - mongo_service
    links:
      - mongo_service
    environment:
      PYTHONUNBUFFERED: 'true'
...

Update

The final RUN instruction will execute at build time. You need to use CMD instead for it to execute at runtime:

CMD curl "mongo_service:27017"
codemonkey
  • 3,510
  • 3
  • 23
  • 35
0

For me it was a mistake on the connection URI, so I was used to providing the connection uri like mongodb://0.0.0.0:27017/, however, what you actually need to do is provide the connection uri such that it references the database service in the docker-compose file.

services:
  web:
    restart: unless-stopped
    build: ./web
    ports:
      - 4000:4000
  api:
    restart: unless-stopped
    build: ./api
    ports:
      - 3000:3000
    environment:
      MONGDB_URI: mongodb://database/docker-compose-studies # instead of mongodb://0.0.0.0:5000/
  database:
    restart: unless-stopped
    image: mongo
    ports:
      - 5000:27017
    volumes:
      - docker-compose-studies:/data/db
volumes:
  docker-compose-studies:

the database here is referencing services.database.

aprilmintacpineda
  • 1,114
  • 13
  • 21