How to do it...

Execute the following steps one by one to prepare pip requirements for your virtual environment Django project:

  1. Let's go to your Django project that you have under version control and create a requirements directory with these text files, if you haven't already done so:
    • base.txt for shared modules
    • dev.txt for the development environment
    • test.txt for the testing environment
    • staging.txt for the staging environment
    • prod.txt for production
  1. Edit base.txt and add the Python modules that are shared in all environments, line by line. For example, we might migrate our original requirements.txt as base.txt, which would give us this in our virtual environment project:
# base.txt
Django~=2.1.0
djangorestframework
-e git://github.com/omab/python-social-auth.git@6b1e301c79#egg=python-social-auth
  1. If the requirements of a specific environment are the same as in base.txt, add the line including base.txt in the requirements file of that environment, as in the following example:
# prod.txt
-r base.txt
  1. If there are specific requirements for an environment, add them after the base.txt inclusion, as shown in the following:
# dev.txt
-r base.txt
django-debug-toolbar
selenium
  1. You can run the following command in a virtual environment in order to install all of the required dependencies for the development environment (or analogous command for other environments), as follows:
(myproject_env)$ pip3 install -r requirements/dev.txt

With a Docker setup, we follow steps 1-4 in almost precisely the same manner, except the requirements directory would live underneath the config directory. From there, a few additional steps are needed to install the correct requirements by environment:

  1. The Dockerfile file will need to be updated to select the appropriate requirements file based on a build argument, which here defaults to prod:
# Dockerfile
FROM python:3
RUN apt-get update
&& apt-get install -y --no-install-recommends
less mysql-client libmysqlclient-dev
WORKDIR /usr/src/app
ARG BUILD_ENV=prod
ADD config/requirements ./requirements

RUN pip3 install --upgrade pip;
pip3 install -r requirements/$BUILD_ENV.txt
RUN django-admin startproject myproject .;
mv ./myproject ./origproject
  1. The docker-compose.yml file needs to pass through this argument using the current environment variable, as in the following:
# docker-compose.yml
version: '3'
services:
db:
image: "mysql:5.7"
app:
build:
context: .
args:
BUILD_ENV: $BUILD_ENV
command: "python3 manage.py runserver 0.0.0.0:8000"
volumes:
- "./project:/usr/src/app/myproject"
- "./media:/usr/src/app/media"
- "./static:/usr/src/app/static"
- "./templates:/usr/src/app/templates"
- "./apps/external:/usr/src/app/external"
- "./apps/myapp1:/usr/src/app/myapp1"
- "./apps/myapp2:/usr/src/app/myapp2"
ports:
- "8000:8000"
depends_on:
- db
  1. Scripts under bin for each environment are then updated to set the appropriate value for the BUILD_ENV variable. For example, we would update the dev script as follows:
#!/usr/bin/env bash
# bin/dev
# ...

BUILD_ENV="dev" adds

#...
docker-compose $*
  1. We simply use the environment-specific script when building the container, and the argument passes through automatically, causing the correct requirements file to be added to the container:
myproject_docker/$ MYSQL_USER=myproject_user 
> MYSQL_PASSWORD=pass1234
> ./bin/dev build
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.193.172