Bharath
Bharath

Reputation: 113

How to run commands from docker-compose.yml to start couchdb

I have a webapp written in django which i need to connect with couchdb both are running on docker container. django will store the data in couchdb table named "test". So i need to create "test" table in couch db. But i'm unable to figure out how to achieve this as there is no good material available for reference. I have tried the below codes but it is throwing the following error because couchdb is not up and running. Is there any way to create a table in couchdb using docker-compose.yml or any other alternatives.

I use docker-compose up to run the containers

docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: python ./webapp/server.py
    volumes:
      - .:/code
      - ./webapp/static:/static

    networks:
      - couchserver_network
    depends_on:
      - couchserver

  couchserver:
    image: couchdb
    ports:
       - "5984:5984"
    command: curl -u rob:123456 -X PUT localhost:5984/_users && curl -u rob:123456 -X PUT localhost:5984/test
    environment:
      - COUCHDB_USER=rob
      - COUCHDB_PASSWORD=123456
    volumes:
        - ./dbdata:/opt/couchdb/data
    networks:
      - couchserver_network


networks:
  couchserver_network:
    driver: bridge

Error thrown :

 0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 5984: Connection refused

If i remove the line "command: curl -u rob:123456 -X PUT localhost:5984/_users && curl -u rob:123456 -X PUT localhost:5984/test" below error is thrown :

couchserver_1  | {database_does_not_exist,[{mem3_shards,load_shards_from_db,"_users",[{file,"src/mem3_shards.erl"},{line,399}]},.... 

This is my Dockerfile :

FROM python:3.6-slim-buster
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

Any sort of help is really appreciated

One more query : what does the below lines do. I'm novice into docker and this is my first hands on with it

 volumes:
     - ./dbdata:/opt/couchdb/data

Upvotes: 4

Views: 23853

Answers (1)

Neo Anderson
Neo Anderson

Reputation: 6350

The command being used in the compose file overrides the CMD from the Dockerfile that starts the database.

First thing coming in mind is to override the CMD in order to start the DB AND execute the curls, but this won't work due to how the DB is being initialized.

A better approach is to have a helper container that executes the curl commands against the DB.

Here is a minimal example that will create the desired databases:

version: '3'
services:
  couchserver:
    image: couchdb
    ports:
      - "5984:5984"
    environment:
      - COUCHDB_USER=rob
      - COUCHDB_PASSWORD=123456
    volumes:
        - ./dbdata:/opt/couchdb/data
  initializer:
    image: curlimages/curl
    deploy:
      restart_policy:
        condition: on-failure
    depends_on:
      - couchserver
    command: ["sh","-c","sleep 15 && curl -u rob:123456 -X PUT couchserver:5984/_users &&  curl -u rob:123456 -X PUT couchserver:5984/test"]

Upvotes: 6

Related Questions