使用docker卷时,本地更改不反映浏览器中的更改

问题描述

我有一个Rails应用程序,我使用docker卷使代码持久化。每当我在本地进行任何更改时,代码都会反映在docker容器中,但这些更改会反映在浏览器中。我不确定是否遗漏了任何东西。

现在,每次代码更改时,我都在重新启动容器,这不是正确的方法。 以下是我的docker-compose文件

data = {'dates': ['01.05.2020','01.05.2020','02.05.2020','03.05.2020','03.05.2020'],'amount' : [500,1000,2000,3000,4000,2300],'state' : ['ABC','ERT','ABC','XYZ','ZEQ','ERT']}

这是我的dockerfile

version: '3'
services:
  webapp:
    build: .
    ports:
      - '3001:3001'
    volumes:
      - '.:/data/checklist'
    depends_on:
      - db
      - redis
    environment:
      DB_USERNAME: "root"
      DB_PASSWORD: "Mission2019"
      DB_DATABASE: "list"
      DB_PORT: 3306
      DB_HOST: db
      RAILS_ENV: production 
      RAILS_MAX_THREADS: 5
  db:
    image: MysqL:5.7
    restart: always
    environment:
      MysqL_DATABASE: "list"
      MysqL_ROOT_PASSWORD: "Mission2019"
      MysqL_USERNAME: "root"
  redis:
    image: redis:4.0-alpine
    command: redis-server
    ports:
      - '6379:6379'
  sidekiq:
    depends_on:
      - "db"
      - "redis"
    build: .
    command: bundle exec sidekiq -C config/sidekiq.yml
FROM ubuntu:16.04


ENV RUBY_MAJOR="2.6" \
    RUBY_VERSION="2.6.3" \
    RUBYGEMS_VERSION="3.0.8" \
    BUNDLER_VERSION="1.17.3" \
    RAILS_VERSION="5.2.1" \
    RAILS_ENV="development" \
    GEM_HOME="/usr/local/bundle"
ENV BUNDLE_PATH="$GEM_HOME" \
    BUNDLE_BIN="$GEM_HOME/bin" \
    BUNDLE_SILENCE_ROOT_WARNING=1 \
    BUNDLE_APP_CONfig="$GEM_HOME"

ENV PATH="$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH"

USER root
RUN apt-get update && \
      apt-get -y install sudo
RUN echo "%sudo ALL=(ALL) nopASSWD: ALL" >> /etc/sudoers && \
    addgroup --gid 1024 stars && \
    useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
    && echo 'install: --no-document' >> /usr/local/etc/gemrc \
    && echo 'update: --no-document' >> /usr/local/etc/gemrc

USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
    curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libMysqLclient-dev tzdata MysqL-client
    
RUN sudo rm -rf /var/lib/apt/lists/* \
    && sudo curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
    && sudo mkdir -p /usr/src/ruby \
    && sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    && sudo rm ruby.tar.gz

USER root
RUN cd /usr/src/ruby \
    && { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } > file.c.new && mv file.c.new file.c \
    && autoconf \
    && ./configure --disable-install-doc

USER user
RUN cd /usr/src/ruby \
    && sudo make -j"$(nproc)" \
    && sudo make install \
    && sudo gem update --system $RUBYGEMS_VERSION \
    && sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version "$BUNDLER_VERSION"

RUN sudo mkdir -p "$GEM_HOME" "$BUNDLE_BIN" \
    && sudo chmod 777 "$GEM_HOME" "$BUNDLE_BIN" \
    && sudo gem install rails --version "$RAILS_VERSION"
RUN mkdir -p ~/.ssh && \
    chmod 0700 ~/.ssh && \
    ssh-keyscan github.com > ~/.ssh/kNown_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo "$ssh_pub_key" > ~/.ssh/id_rsa.pub && \
    echo "$ssh_prv_key" > ~/.ssh/id_rsa && \
    chmod 600 ~/.ssh/id_rsa.pub && \
    chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
workdir /data
RUN sudo mkdir /data/checklist
workdir /data/checklist
ADD Gemfile Gemfile.lock ./

RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_ENV production
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
ENTRYPOINT ["sh","./config/docker/startup.sh"]

production.rb

#! /bin/sh

# Wait for DB services

# Prepare DB (Migrate - If not? Create db & Migrate)
kill -9 `cat /data/checklist/tmp/pids/server.pid`
sh ./config/docker/prepare-db.sh

# Pre-comple app assets
sh ./config/docker/asset-pre-compile.sh

# Start Application
rails s -p 3001 -b 0.0.0.0

解决方法

通常,Rails不会在生产中对代码进行热重装(出于很好的原因,包括安全性,稳定性和速度),因此我会考虑切换到开发Rails环境。

接下来要警惕的是,docker可能不会以Rails会“注意到”文件已更改的方式(甚至在development中)接收更改。为此,请添加一行:

config.file_watcher = ActiveSupport::FileUpdateChecker

到您的production.rb文件(或在切换到development.rb的情况下)。