Docker-Compose

  • DC makes it easier to configure and run applications made up of multiple containers
  • DC is a very handy tool to quickly get docker environment up and running.
  • It uses yaml files to store the configuration of all the containers
  • We can create some containers like: web app, postgres and redis
    • and can establish an internal connection

Installation

  • sudo apt-get install docker-compose
  • Check
    • docker-compose –version
    • docker-compose -v

Commands

# This will not build the image if it's already exists
docker-compose up

# Force to build the image
docker-compose build

docker-compose down
docker-compose config

# stops all the running containers without removing them.
docker compose stop
docker container stop <container id>

# removes all the containers.
docker compose rm

Mongodb

  • Install mongo
npm init -y
npm install mongoose

# package.json --> Update
"scripts": {
    "start": "node index.js"
  }
  • Check connection
    • npm start
# index.js
const mongoose = require("mongoose");

const DATABASE = "mongodb://localhost:27017/test";

mongoose
    .connect(DATABASE)
    .then(() => {
        console.log("DB connected");
    })
    .catch(()=>{
        console.log("ERROR in DB connection");
    });
  • Ignore file
# .dockerignore --> create and add
node_modules
  • Add Dockerfile
# Dockerfile
FROM node:alpine

WORKDIR /var/app
COPY ./ ./

RUN npm install

# npm start
CMD ["npm", "start"]
  • docker-compose file
# docker-compose.yml
version: "3"
services:
    mymongo:
        image: "mongo"
    mynode:
        build: .
        ports:
        - "8000:8000"
# mymongo --> container name
# mongo --> image name
  • Run
# Update index.js
const DATABASE = "mongodb://mymongo:27017/test";

docker-compose build
docker-compose up

# OR
docker-compose up --build

DjRest-Adv

# Dockerfile

FROM python:3.7-alpine
MAINTAINER Hitman

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache postgresql-client jpeg-dev
RUN apk add --update --no-cache --virtual .tmp-build-deps \
      gcc libc-dev linux-headers postgresql-dev musl-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN apk del .tmp-build-deps

RUN mkdir /app
WORKDIR /app
COPY ./app /app

RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web

USER user
# docker-compose.yml

version: "3"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py wait_for_db &&
             python manage.py migrate &&
             python manage.py runserver 0.0.0.0:8000"
    environment:
      - DB_HOST=db
      - DB_NAME=app
      - DB_USER=postgres
      - DB_PASS=supersecretpassword
    depends_on:
      - db

  db:
    image: postgres:10-alpine
    environment:
      - POSTGRES_DB=app
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=supersecretpassword
# django app inside app folder

# Run
docker-compose up