Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sqlite3 with docker compose

Between the following tutorials;

  • Dockerizing create-react-app
  • Developing microservices - Node, react & docker

I have been able to convert my nodejs app to dockerized micro-services which is up and running and connecting to services. However, my app uses Sqlite/Sequelize and this was working perfectly prior to dockerizing.

With the new setup, I get error;

/usr/src/app/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:31
throw new Error('Please install sqlite3 package manually');
Error: Please install sqlite3 package manually at new ConnectionManager 
(/usr/src/app/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:31:15)

My question is;

  1. Is it possible to use Sqlite3 with Docker
  2. If so, anyone able to share sample docker-compose.yml and Dockerfile combo that works for this please.

My docker-compose.yml

version: '3.5'

services:
  user-service:
    container_name: user-service
    build: ./services/user/
    volumes:
      - './services/user:/usr/src/app'
      - './services/user/package.json:/usr/src/package.json'
    ports:
      - '9000:9000' # expose ports - HOST:CONTAINER

  web-service:
    container_name: web-service
    build:
      context: ./services/web
      dockerfile: Dockerfile
    volumes:
      - './services/web:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - '3000:3000' # expose ports - HOST:CONTAINER
    environment:
      - NODE_ENV=development
    depends_on:
      - user-service

My user/ Dockerfile

FROM node:latest

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
ADD package.json /usr/src/package.json
RUN npm install

# start app
CMD ["npm", "start"]

My web/ Dockerfile

FROM node:latest

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install [email protected]
RUN npm install gulp -g

# start app
CMD ["npm", "start"]

Many thanks.

like image 335
ClaraU Avatar asked Aug 09 '18 14:08

ClaraU


1 Answers

Got it. The issue was that my local node_modules were being copied to the host container. Hence in the sqlite3 lib/binding, node-v57-darwin-x64 was there instead of what is expected - node-v57-linux-x64. Hence the mess.

I updated the Dockerfiles and docker-compose.yml as follows:

My docker-compose.yml

services:
  user-service:
    container_name: user-service
    build: 
      context: ./services/user/
      dockerfile: Dockerfile
    volumes:
      - './services/user:/usr/src/app'
      - '/usr/src/node_modules'
    ports:
      - '9000:9000' # expose ports - HOST:CONTAINER

  web-service:
    container_name: web-service
    build:
      context: ./services/web/
      dockerfile: Dockerfile
    volumes:
      - './services/web:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - '3000:3000' # expose ports - HOST:CONTAINER
    environment:
      - NODE_ENV=development
    depends_on:
      - user-service

My user/ Dockerfile

FROM node:latest

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/node_modules/.bin` to $PATH
ENV PATH /usr/src/node_modules/.bin:$PATH

# install and cache app dependencies
ADD package.json /usr/src/package.json
RUN npm install

# start app
CMD ["npm", "start"]

Helpful posts Getting npm packages to be installed with docker-compose

like image 55
ClaraU Avatar answered Sep 30 '22 18:09

ClaraU