How to use sqlite3 with docker compose

≯℡__Kan透↙ 提交于 2020-08-05 02:04:34

问题


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 react-scripts@1.1.4
RUN npm install gulp -g

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

Many thanks.


回答1:


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



来源:https://stackoverflow.com/questions/51769034/how-to-use-sqlite3-with-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!