美文网首页
Docker下的Rails的Dockerfile文件制作2019

Docker下的Rails的Dockerfile文件制作2019

作者: 乐哈网 | 来源:发表于2019-05-27 19:25 被阅读0次

    主要内容:

    1. Rails的Dockerfile创建:快速创建,避免漫长等待、只装有需要的包。
    2. docker-compose.yml的配置:包括开发、生产环境
    3. 问题记录:Yarn过期问题等

    关于Dockerfile

    使用的是ruby:2.5的基础镜像,相对来说干净
    分下面几个部分:
    1 基础docker镜像
    2 更换源地址(国内用户必须更换,就算科学上网也不慢)
    3 安装基础packages。
    4 创建和设置工作目录,制作Gemfile,copy Gemfile,便于通过bundle安装rails。
    5 创建enterpoint.sh ,这个里面内容重要,影响后续command里面的命令执行。

    FROM ruby:2.5
    LABEL maintainer="maweikeji"#武汉码为科技
    
    # throw errors if Gemfile has been modified since Gemfile.lock
    #RUN bundle config --global frozen 1
    RUN bundle config mirror.https://rubygems.org https://gems.ruby-china.com
    
    RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
    RUN echo "deb http://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
    
    RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
    RUN  sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
    RUN  sed -i s@/security.debian.org/@/mirrors.163.com/@g /etc/apt/sources.list
    RUN  apt-get clean
    
    RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
    RUN apt-get update && apt-get install -y  nodejs  yarn mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
    
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app
    
    COPY Gemfile /usr/src/app/Gemfile
    COPY Gemfile.lock /usr/src/app/Gemfile.lock
    RUN bundle install
    #COPY api/. /usr/src/app (copy代码进去,或 在docker-compose.yml里面配置volumes)
    
    # 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"]
    
    

    entrypoint.sh文件

    #enteypoint.sh
    #!/bin/bash
    set -e
    # Remove a potentially pre-existing server.pid for Rails.
    rm -f ./tmp/pids/server.pid
    # Then exec the container's main process (what's set as CMD in the Dockerfile).
    exec "$@"
    
    

    docker-compose.yml

      app-owl_api:
        build:
          context: .
          dockerfile: ./api/Dockerfile-rails
        command: bin/dev.sh  # 生产环境下可以改成 bin/pro.sh
        volumes:
          - ./api:/usr/src/app
        ports:
          - "3001:3000"
        depends_on:
          - db
          - redis
    
    

    几个麻烦点的问题

    问题1:yarn的问题

    Screenshot from 2019-05-27 18-48-44.png

    yarn检查相关的js包里面的js语法出错!

    app-owl_api_1    | yarn check v1.16.0
    app-owl_api_1    | warning Integrity check: System parameters don't match
    app-owl_api_1    | error Integrity check failed
    app-owl_api_1    | error Found 1 errors.
    app-owl_api_1    | info Visit https://yarnpkg.com/en/docs/cli/check for documentation about this command.
    
    Installation Problem: Unexpected token { in cli.js
    

    怎么解决?如果能找到那个包的版本有问题,那么就重装。重装会自动查找依赖,就是yarn的问题交给yarn去解决

    yarn remove  xxx
    yarn add xxx
    #重新装各包
    

    问题2:遇到一个Not agitrepository的问题

    # [Receiving “fatal: Not a git repository” when attempting to remote add a Git repo](https://stackoverflow.com/questions/4630704/receiving-fatal-not-a-git-repository-when-attempting-to-remote-add-a-git-repo)
    
    

    怎么处理:

    git init .
    

    问题3

    Bundled gems are installed into `/usr/local/bundle`
    Post-install message from i18n:
    
    HEADS UP! i18n 1.1 changed fallbacks to exclude default locale.
    But that may break your application.
    
    Please check your Rails app for 'config.i18n.fallbacks = true'.
    If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be
    'config.i18n.fallbacks = [I18n.default_locale]'.
    If not, fallbacks will be broken in your app by I18n 1.1.x.
    
    For more info see:
    https://github.com/svenfuchs/i18n/releases/tag/v1.1.0
    

    参考,
    https://www.firehydrant.io/blog/developing-a-ruby-on-rails-app-with-docker-compose/
    https://docs.docker.com/compose/rails/
    ...

    相关文章

      网友评论

          本文标题:Docker下的Rails的Dockerfile文件制作2019

          本文链接:https://www.haomeiwen.com/subject/guuctctx.html