Docker volume and Compose

Docker volume and Compose

#Devops #Docker #DockerVolume #DockerCompose

hello folks! how are you doing.

Today’s topic is docker volume :- Docker volume is term introduce when the problem start occuring that when docker containers are get killed by mistake then all the data got missing and there was no way to retrive it back. SO thats why Docker volume is introduced.

Now , we will see how we can use volumes to retrive the data of container which was got killed

First we will make folder for the volumes and then we will make another for the saving the device source where volume of the container is stored now we will see the importnat commands related to making and connecting volume to the container are as followed.

  1. Command to create volume

    docker volume create -- name <name_of_the_volume> --opt type=none --opt device=<device_path> --opt bind

  2. Command to connect volume to the container

    docker docker runt -d -p 8000:8000 --mount source =<volume name, target =/app

    <name_of_the_container>

Docker Compose :- Docker compose is the kind of the tool which you have to downlode and and then you have to make a YAML(Yet Another Markup Language).

Now you have to write the yaml file which will contain the Services like Frontend ,Backend, cache(Redis) etc

now this file will look like this

Let me explain youone service

Frontend is one of the service

build : is for the buildeg the container for the frontend

context: is for script ./frontend si bash scirpt

dockerfile: it will contain dockerfile of your project

Volumes: have the name of volume and device path so it will create and connect volume with container.

Ports: Porsts are for the ports where you app/container will run

networks : it is the network of you app

depend_on: will show you that it is depend up on backend

Environment : it for the having the URL and User password and user name and all.

version: '3.8'

services:
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: frontend
    volumes:
      - frontend_vol:/app/node_modules
      - ./frontend:/app
    ports:
      - "3000:3000"
    networks:
      - app-network
    depends_on:
      - backend
    environment:
      - NODE_ENV=development
      - REACT_APP_API_URL=http://backend:8000

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: backend
    volumes:
      - backend_vol:/app/node_modules
      - ./backend:/app
    ports:
      - "8000:8000"
    networks:
      - app-network
    depends_on:
      - database
    environment:
      - NODE_ENV=development
      - DB_HOST=database
      - DB_PORT=5432
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - DB_NAME=${DB_NAME}

  database:
    image: postgres:15-alpine
    container_name: database
    volumes:
      - postgres_vol:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - app-network
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}

volumes:
  frontend_vol:
  backend_vol:
  postgres_vol:

networks:
  app-network:
    driver: bridge