Perform automatic backup using Docker

Asked

Viewed 45 times

1

My application is composed of Frontend (Vue.js) + Backend (Node.js with postgresql bank), before playing in production I dockerizei using Docker and Docker-Compose.

The file below is Docker-Compose.yml:

version: "3"

services:
  db:
    image: 'postgres'
    environment:
      POSTGRES_PASSWORD: <POSTGRES_PASSWORD>
      POSTGRES_USER: <POSTGRES_USER>
      POSTGRES_DB: <POSTGRES_DB>
    ports:
      - "5432:5432"
    container_name: db

  backend:
    image: backend
    command: sh -c "npm install -g knex && knex migrate:latest && npm start"
    depends_on: 
      - db
    ports:  
      - "3000:3000"
    container_name: backend

  frontend:
    image: frontend
    ports:
      - '8080:8080'
    container_name: frontend

Basically I have 3 containers that communicate. In the system, the backup of the database happens in 2 ways:

  1. Automatically every 7 days;
  2. Admin user has access to a screen in the frontend that allows to perform a backup manually.

This second method works as follows, in the backend has a route /backup executing a function with a cmd command on the machine, using the postgresql pg_dump tool. Here is this function (it is in the backend):

const {
   exec
} = require('child_process');    

function backup() {
    execute(`pg_dump -U ${username} -d ${database} -f ${fileName} -F t`,).then(async () => {
    }).catch(err => {
        console.log(err);
    })
}
 
backup();

So far so good, everything works perfectly while the application is not docked. But when I dockerizo and try to make a backup the following error happens:

/bin/sh: pg_dump: not found

This happens because in the backend container, it is not the bd container, so it does not have the Postgresql driver or database.

Is there any way to access the db container by backend container and run the command pg_dump?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.