1

I need to use some graph-tool calculations in my Django project. So I started with docker pull tiagopeixoto/graph-tool and then added it to my Docker-compose file:

version: '3'

services:

  db:
    image: postgres

  graph-tool:
    image: dcagatay/graph-tool

  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
      - graph-tool

When I up my docker-compose I got a line:

project_graph-tool_1_87e2d144b651 exited with code 0

And finally when my Django projects starts I can not import modules from graph-tool, like:

from graph_tool.all import *

If I try work directly in this docker image using:

docker run -it -u user -w /home/user tiagopeixoto/graph-tool ipython

everything goes fine. What am I doing wrong and how can I fix it and finally attach graph-tool to Django? Thanks!

D. Make
  • 579
  • 2
  • 6
  • 23

1 Answers1

2

Rather than using a seperate docker image for graphtool, i think its better to use it within the same Dockerfile which you are using for Django. For example, update your current Dockerfile:

FROM ubuntu:16.04  # using ubuntu image

ENV PYTHONUNBUFFERED 1
ENV C_FORCE_ROOT true

# python3-graph-tool specific requirements for installation in Ubuntu from documentation
RUN echo "deb http://downloads.skewed.de/apt/xenial xenial universe" >> /etc/apt/sources.list && \
echo "deb-src http://downloads.skewed.de/apt/xenial xenial universe" >> /etc/apt/sources.list

RUN apt-key adv --keyserver pgp.skewed.de --recv-key 612DEFB798507F25

# Install dependencies
RUN apt-get update \
    && apt-get install -y python3-pip python3-dev \ 
    && apt-get install --yes --no-install-recommends --allow-unauthenticated python3-graph-tool \
    && cd /usr/local/bin \
    && ln -s /usr/bin/python3 python \
    && pip3 install --upgrade pip

# Project specific setups 
# These steps might be different in your project
RUN mkdir /code
WORKDIR /code
ADD . /code
RUN pip3 install -r requirements.pip

Now update your docker-compose file as well:

version: '3'
services:
  db:
    image: postgres
  web:
    build: .
    container_name: djcon  # <-- preferred over generated name
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Thats it. Now if you go to your web service's shell by docker exec -ti djcon bash(or any generated name instead of djcon), and access the django shell like this python manage.py shell. Then type from graph_tool.all import * and it will not throw any import error.

ruddra
  • 50,746
  • 7
  • 78
  • 101