Search code examples
dockerdocker-composedockerfiledocker-machinegraphdb

Docker compose run shell command after startup


for a hello world example I want to start 2 containers:

  1. Flask server (which later accesses the Graphdb server over http)
  2. Graphdb server

For my Flask server I wrote the following Dockerfile:

FROM python:3.7.1

LABEL Author="Test"
LABEL version="1.0.0"

ENV PYTHONDONTWRITEBYTECODE 1
ENV FLASK_APP "app.py"
ENV FLASK_ENV "development"
ENV FLASK_DEBUG True

RUN mkdir /app
WORKDIR /app

RUN pip install flask

ADD . /app

EXPOSE 5000

CMD flask run --host=0.0.0.0

And my docker-compose.yml file looks like:

version: '3'
services:
  db:
    image: ontotext/graphdb:9.1.1-se
    ports:
    - "7200:7200"
  server:
    build: .
    ports:
    - "5000:5000"
    depends_on:
    - db

Now I want to execute a shell command

loadrdf -c [path to .ttl file]

targeting the graphdb instance to load my RDF data from a file, located at: ../rdf/rdf.ttl (relative path from the Dockerfile and docker-compose.yml).

My question is how to access the rdf.ttl file from the graphdb container and how / where to perform this shell command?


Solution

  • You need to mount a volume in your docker-compose.yml, containing your RDF files:

      server:
        ...
        volumes:
        - ../rdf:/rdf
    

    THen with your service running you can run this command on the host:

    docker-compose exec server loadrdf /rdf/rdf.ttl
    

    This will run your command inside the container and find your file at the mount point.