Issue to node-sass and Docker

后端 未结 2 490
粉色の甜心
粉色の甜心 2021-02-13 00:27

I\'m attempting to dockerise my node application. My current application is a nodejs express server with postgresql. ExpressJS uses node-sass-middleware to handle the sass asset

2条回答
  •  时光取名叫无心
    2021-02-13 01:24

    I have encountered this problem a few times when running my apps within Docker. I believe the problem arises when mounting my local app directory with Docker as this includes the node_modules folder which have been built in a different environment.

    My local setup is Mac OSX while Docker is running in linux.

    The solution for me was to add a .dockerignore file in the root of my app and add node_modules as per this suggestion https://stackoverflow.com/a/55657576/3258059

    I then needed to trigger yarn install to run again in my docker container and also ran the node-sass rebuild command with the docker container: docker-compose run web npm rebuild node-sass where web is the name of my docker container.

    I suspect my Dockerfile may have a bit of bloat but I will add in case it helps someone:

    FROM ruby:2.3.7
    ENV BUNDLER_VERSION=1.17.3
    RUN gem install bundler -v "$BUNDLER_VERSION" --no-document
    RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -  
    RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -   
    RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list   
    RUN apt-get update -qq && apt-get install -y nodejs yarn build-essential libpq-dev postgresql-client
    RUN mkdir /myapp
    WORKDIR /myapp
    COPY Gemfile /myapp/Gemfile
    COPY Gemfile.lock /myapp/Gemfile.lock
    ENV RAILS_ENV docker    
    ENV WEBPACKER_DEV_SERVER_PUBLIC localhost:3035  
    ENV WEBPACKER_DEV_SERVER_HOST localhost 
    COPY package.json *yarn* ./
    RUN bundle install
    RUN yarn install
    COPY . /myapp
    
    # Add a script to be executed every time the container starts.
    COPY entrypoint.sh /usr/bin/
    RUN chmod +x /usr/bin/entrypoint.sh
    ENTRYPOINT ["entrypoint.sh"]
    EXPOSE 3000
    
    # Start the main process.
    CMD ["rails", "server", "-b", "0.0.0.0"]
    

    and my docker-compose.yml

    version: '3'
    services:
      db:
        image: postgres
        ports: 
          - "5433:5432"
        volumes:
          - ./tmp/db:/var/lib/postgresql/data
      web:
        build: .
        environment: 
          - RAILS_ENV=development
          - NODE_ENV=development
          - WEBPACKER_DEV_SERVER_PUBLIC=localhost:3035
          - WEBPACKER_DEV_SERVER_HOST=localhost
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes:
          - .:/myapp
        ports:
          - "3000:3000"
        depends_on:
          - db
          - redis
          #- elasticsearch
      redis:
        image: redis:alpine
    

提交回复
热议问题