-
Notifications
You must be signed in to change notification settings - Fork 19
Docker
For the installation, it's better to review the official documentation for docker and docker-compose:
Some Linux flavours have a package for their package managers, but this kind of installation could cause some issues because they aren't updated in most of the cases.
Docker is a tool that can containerize the microservices that power an app into individual Linux Containers, aka separated filesystems and kernel namespaces. It provides repeatable builds, interprocess isolation guarantees, and system resource protection.
Docker uses Dockerfile
files to define a container. Each service has one defining its environment and setup in docker/<service>/Dockerfile
.
Oddslingers is a project that was initially developed separating the code, data and configuration in the /opt directory. It is important to understand this work philosophy to realize why this design.
In the project directory you will find other folders like:
- bin: for management command executables.
- data: to store the variable data like postgres, redis and logs data.
- etc: it contains the configuration files for the different services.
- core: it is the main django application folder.
For more information read Folder Locations page.
Note: On Linux usually is a good practice to create the additional folders for volumes mapping. In this case, you should create the data
folder before running these commands.
Django service uses a custom image based on a python3.7. The django image building process basically replicates the two first steps of the Manual setup and it tags the image as oddslingers:django_base
. This image installs the different Django and python requirements for running oddslingers.
Note: Dramatic and yacron services are supported on this image too.
Docker is a very flexible tool and it allows isolating the different containers using virtual networks. This ensures Oddslingers stack won't be accessed by other containers. For this tutorial we are creating a network with name: "oddslingers_network".
# Create a network called oddslingers_network
docker network create -d bridge oddslingers_network
By definition containers are ephemeral elements. This means that when a container is turned off the data in it will be lost. For that reason we are using Docker volumes to store the data. There are many ways for creating volumes. We are going to create a volume for each mount point or storage point mapping them with a "real" folder or location in our filesystem. For example:
# Create a volume called oddslingerspoker_data_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/data \
--opt o=bind \
oddslingerspoker_data_vol
This command creates a volume called "oddslingerspoker_data_vol" mapped to /opt/oddslingers/data folder. These volume is used in different service containers like: django, postgres, dramatiq and yacron.
The next picture shows the containers and their volume dependencies.
Execute these commands one by one to create the other volumes:
# Create a volume called oddslingerspoker_core_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/core \
--opt o=bind \
oddslingerspoker_core_vol
# Create a volume called oddslingerspoker_certs_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/data/certs \
--opt o=bind \
oddslingerspoker_certs_vol
# Create a volume called oddslingerspoker_nginx_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/etc/nginx \
--opt o=bind \
oddslingerspoker_nginx_vol
# Create a volume called oddslingerspoker_postgres_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/data/postgres \
--opt o=bind \
oddslingerspoker_postgres_vol
# Create a volume called oddslingerspoker_redis_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/data/redis \
--opt o=bind \
oddslingerspoker_redis_vol
# Create a volume called oddslingerspoker_yacron_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/etc/yacron \
--opt o=bind \
oddslingerspoker_yacron_vol
# Create a volume called oddslingerspoker_js_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/core/js \
--opt o=bind \
oddslingerspoker_js_vol
# Create a volume called oddslingerspoker_static_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/core/static \
--opt o=bind \
oddslingerspoker_static_vol
# Create a volume called oddslingerspoker_nodemodules_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/core/js/node_modules \
--opt o=bind \
oddslingerspoker_nodemodules_vol
# Create a volume called oddslingerspoker_dumps_vol
docker volume create --driver local \
--opt type=none \
--opt device=/opt/oddslingers.poker/core/dumps \
--opt o=bind \
oddslingerspoker_dumps_vol
For this step we need two build two custom images. The first one is called "django_base" and it installs the main python requirements for running django applications.
# Build django image use by django, dramatic, yacron services
docker build . -t "oddslingers:django_base"
The second image is used by the webpack service container to bundle javascript modules and other frontend components.
# Build webpack image use by webpack service
docker build . -f Dockerfile.webpack -t "oddslingers:webpack"
If you read Supervisord or Layers of the Stack pages you can learn about the three main groups of services required by Oddslingers.
- Always running: nginx, postgres and redis.
- Task workers: yacron, dramatiq.
- Django workers: django.
The next picture shows the containers and their dependencies.
The first group we will need to run is: nginx, postgres and redis:
# Create postgres service container
docker run -tid \
-v oddslingerspoker_postgres_vol:/var/lib/postgresql/data \
--network oddslingers_network \
--env-file .env --env-file .secrets.env \
--expose 5432 \
--name postgres \
postgres:12-alpine
# Create redis service container
docker run -tid \
-v oddslingerspoker_redis_vol:/data \
--network oddslingers_network \
--entrypoint redis-server \
-p 6479 \
--expose 6379 \
--name redis \
redis:5-alpine --appendonly yes
Note: Remember before run nginx container to execute ssl script in bin folder to generate the ssl certificates.
# Create nginx service container
docker run -tid \
-v oddslingerspoker_nginx_vol:/etc/nginx \
-v oddslingerspoker_certs_vol:/opt/oddslingers.poker/data/certs \
-v oddslingerspoker_core_vol:/opt/oddslingers.poker/core \
--network oddslingers_network \
-p 80:80 -p 443:443 \
--link django \
--name nginx \
nginx:alpine
When these services are running you can begin to use oddslingers by running a django container with the following command:
# Create django service container
docker run -tid \
-v oddslingerspoker_data_vol:/opt/oddslingers.poker/data \
-v oddslingerspoker_core_vol:/opt/oddslingers.poker/core \
--network oddslingers_network \
-w /opt/oddslingers.poker/core/ \
--env ODDSLINGERS_ENV=DEV --env POSTGRES_HOST=postgres --env REDIS_HOST=redis \
--env-file .env --env-file .secrets.env \
--expose 8000 \
--name django oddslingers:django_base ./manage.py runserver 0.0.0.0:8000
In this step, you can run migrations and create the super user:
docker exec django ./manage.py migrate
docker exec -ti django ./manage.py createsuperuser
Next, it is possible run the other containers for dramatiq, yacron and webpack:
# Create dramatiq service container
docker run -tid \
-v oddslingerspoker_data_vol:/opt/oddslingers.poker/data \
-v oddslingerspoker_core_vol:/opt/oddslingers.poker/core \
--network oddslingers_network \
-w /opt/oddslingers.poker/core/ \
--entrypoint nice \
--env ODDSLINGERS_ENV=DEV --env POSTGRES_HOST=postgres --env REDIS_HOST=redis \
--env-file .env --env-file .secrets.env \
--cap-add SYS_NICE \
--name dramatic oddslingers:django_base -15 ./manage.py rundramatiq --processes 1 --threads 1
# Create yacron service container
docker run -tid \
-v oddslingerspoker_data_vol:/opt/oddslingers.poker/data \
-v oddslingerspoker_core_vol:/opt/oddslingers.poker/core \
-v oddslingerspoker_yacron_vol:/opt/oddslingers.poker/etc/yacron \
--network oddslingers_network \
-w /opt/oddslingers.poker/core/ \
--entrypoint yacron \
--env ODDSLINGERS_ENV=DEV --env POSTGRES_HOST=postgres --env REDIS_HOST=redis \
--env-file .env --env-file .secrets.env \
--name yacron oddslingers:django_base -c /opt/oddslingers.poker/etc/yacron/oddslingers-dev.yaml
# Create webpack service container
docker run -tid \
-v oddslingerspoker_js_vol:/opt/oddslingers.poker/core/js \
-v oddslingerspoker_static_vol:/opt/oddslingers.poker/core/static \
-v oddslingerspoker_nodemodules_vol:/src/node_modules \
-v oddslingerspoker_dumps_vol:/opt/oddslingers.poker/core/dumps \
--env ODDSLINGERS_ENV=DEV\
--env-file .env --env-file .secrets.env \
--name webpack oddslingers:webpack sh -c "(webpack --mode development --watch --info-verbosity verbose & node-sass-chokidar --watch scss/ -o ../static/css 2>&1 &)| cat"
Go to oddslingers.l
and enjoy