Docker Compose

From bibbleWiki
Jump to navigation Jump to search

Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Example of MYSQL Container

This is an example of a mysql. The ports are changed to stop collision with desktop.

version: '3.8'
networks:
  default:
services:
   db:
     image: mysql:5.7
     container_name: conference_security
     ports:
       - 3307:3306
     volumes:
       - "./.data/db:/var/lib/mysql"
     environment:
       MYSQL_ROOT_PASSWORD: pass
       MYSQL_DATABASE: TEST_DB

Example of django Container

Introduction

Django is a python based web framework solution. To create this container we will

  • Define the python requirement document
  • Define a docker file
  • Define a docker compose stack file

Define the python requirement document

Here are the specific requirements for the Django.

Django==3.1.5
djangorestframework==3.12.2
mysqlclient==2.0.3
django-mysql==3.10.0
django-cors-headers==3.6.0
pika==1.1.0

Define a docker file

This defines the App code for the django DockerFile

FROM python:3.9
ENV  PYTHONNUMBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

Define a docker compose stack file

We define the deployment include, services, database and networks

version: '3.8'
services: 
  django:
    container_name: djangoapp
    build: 
      context: .
      dockerfile: Dockerfile
    command: 'python3 manage.py runserver 0.0.0.0:8000'
    ports: 
      - 8000:8000
    volumes: 
      - .:/app
    depends_on:
      - db
    networks:
      django_demo:

  queue:
    environment:
      - PYTHONUNBUFFERED=1
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: djangoqueue
    command: 'python3 consumer.py'
    depends_on:
      - db
    networks:
      django_demo:

  db: 
    image: mysql:5.7.32
    container_name: djangodb
    restart: always
    environment: 
      MYSQL_DATABASE: admin
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes: 
      - .dbdata:/var/lib/mysql
    ports: 
      - 33066:3306
    networks:
      django_demo:

networks:
  django_demo:
    external: true

Flask Container

Introduction

Flask is a python based web framework solution. To create this container we will

  • Define the python requirement document
  • Define a docker file
  • Define a docker compose stack file

Define the python requirement document

Here are the specific requirements for the Flask.

Flask==1.1.2
Flask-SQLAlchemy==2.4.4
SQLAlchemy==1.3.20
Flask-Migrate==2.5.3
Flask-Script==2.0.6
Flask-Cors==3.0.9
requests==2.25.0
mysqlclient==2.0.3
pika==1.1.0

Define a docker file

This defines the App code for the Flask DockerFile

FROM python:3.9
ENV  PYTHONNUMBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

Define a docker compose stack file

We define the deployment include, services, database and networks

version: '3.8'
services: 
  flask:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: flaskapp
    command: python main.py
    ports: 
      - 8001:5000
    volumes: 
      - .:/app
    depends_on:
      - db
    networks:
      django_demo:

  queue:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: flaskqueue
    command: 'python consumer.py'
    depends_on:
      - db
    networks:
      django_demo:

  db: 
    image: mysql:5.7.32
    container_name: flaskdb
    restart: always
    environment: 
      MYSQL_DATABASE: main
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes: 
      - .dbdata:/var/lib/nysql
    ports: 
      - 33067:3306
    networks:
      django_demo:

networks:
  django_demo:
    external: true