Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose installing requirements.txt

In my docker image I am cloning the git master branch to retrieve code. I am using docker-compose for the development environment and running my containers with volumes. I ran across an issue when installing new project requirements from my python requirements.txt file. In the development environment, it will never install new requirements on dev environment because when re-building the image, the latest code is pulled from github.

Below is an example of my dockerfile:

FROM base

# Clone application
RUN git clone repo-url
# Install application requirements
RUN pip3 install -r app/requirements.txt

# ....

Here is my compose file:

myapp:
    image: development
    env_file: .env
    ports:
        - "8000:80"
    volumes:
        - .:/home/app

    command: python3 manage.py runserver 0.0.0.0:8000

Is there any way to install newly added requirements after build on development?

like image 242
Robert Christopher Avatar asked Dec 29 '16 14:12

Robert Christopher


1 Answers

The way I solved this is by running two services:

  1. server: run the server depends on requirements
  2. requirements: installs requirements prior to running server

And this is how the docker-compose.yml file would look like:

version: '3'

services:
  django:
    image: python:3.7-alpine
    volumes:
     - pip37:/usr/local/lib/python3.7/site-packages
     - .:/project
    ports: 
      - 8000:8000
    working_dir: /project
    command: python manage.py runserver
    depends_on:
      - requirements

  requirements:
    image: python:3.7-alpine
    volumes:
      - pip37:/usr/local/lib/python3.7/site-packages
      - .:/project
    working_dir: /project
    command: pip install -r requirements.txt

volumes:
  pip37:
    external: true

PS: I created a named volume for the pip modules so I can preserve them across different projects. You can create one yourself by running:

docker volume create mypipivolume
like image 108
Alexander Luna Avatar answered Sep 17 '22 08:09

Alexander Luna