How to connect mysql in a rails app with docker?

I created a rails application: testapp.

Then added Dockerfile and docker-compose.yml files under it.

  • Deimos Expecting 'MESOS_SLAVE_PID' in environment variables
  • monitoring memory usage in docker
  • advantages VM's have over lightweight containers (docker) [closed]
  • Control a container from another one
  • Running development server with create-react-app inside of a docker container
  • How can I wait for a docker container to be up and running?
  • Dockerfile

    FROM ruby:2.2.0
    RUN apt-get update -qq && apt-get install -y build-essential mysql-client libmysqlclient-dev nodejs
    RUN mkdir /testapp
    WORKDIR /testapp
    ADD Gemfile /testapp/Gemfile
    ADD Gemfile.lock /testapp/Gemfile.lock
    RUN bundle install
    ADD . /testapp
    

    docker-compose.yml

    db:
      image: mysql
      environment:
        MYSQL_ROOT_PASSWORD: 1234
    web:
      build: .
      command: bundle exec rails s -p 3000 -b '0.0.0.0'
      volumes:
        - .:/testapp
      ports:
        - "3000:3000"
      links:
        - db
    

    My database setting file:

    config/database.yml

    default: &default
      adapter: mysql2
      encoding: utf8
      pool: 5
      username: root
      password: 1234
      host: db
      port: 3306
    
    development:
      <<: *default
      database: testapp_development
    
    test:
      <<: *default
      database: testapp_test
    
    production:
      <<: *default
      database: testapp_production
    

    When I start container from a docker machine: docker-compose up, and access http://192.168.99.104:3000/ from browser, it gave me the error:

    Mysql2::Error
    Access denied for user 'root'@'172.17.0.7' (using password: YES)
    

    The error in console:

    web_1 | /usr/local/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
    web_1 |   error_class: Mysql2::Error
    web_1 |   environment_name: development
    web_1 |   cgi_data: {"GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/", "QUERY_STRING"=>"", "REMOTE_ADDR"=>"192.168.99.1", "REMOTE_HOST"=>"192.168.99.1", "REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"http://192.168.99.104:3000/", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"192.168.99.104", "SERVER_PORT"=>"3000", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25)", "HTTP_HOST"=>"192.168.99.104:3000", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0", "HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "HTTP_ACCEPT_LANGUAGE"=>"ja,en-US;q=0.7,en;q=0.3", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_CONNECTION"=>"keep-alive", "HTTP_CACHE_CONTROL"=>"max-age=0", "rack.version"=>["1", "3"], "rack.multithread"=>"false", "rack.multiprocess"=>"false", "rack.run_once"=>"false", "rack.url_scheme"=>"http", "rack.hijack?"=>"true", "rack.hijack"=>"#<Proc:0x007fd6b4016c28@/usr/local/bundle/gems/rack-1.6.4/lib/rack/handler/webrick.rb:76 (lambda)>", "rack.hijack_io"=>nil, "HTTP_VERSION"=>"HTTP/1.1", "REQUEST_PATH"=>"/", "ORIGINAL_FULLPATH"=>"/", "ORIGINAL_SCRIPT_NAME"=>"", "action_dispatch.parameter_filter"=>["password"], "action_dispatch.redirect_filter"=>[], "action_dispatch.show_exceptions"=>"true", "action_dispatch.show_detailed_exceptions"=>"true", "action_dispatch.cookies_serializer"=>"json", "action_dispatch.cookies_digest"=>nil, "ROUTES_47215567452160_SCRIPT_NAME"=>"", "action_dispatch.request_id"=>"4048a5f6-974e-484a-9c16-fc4e1ca1c1dc", "action_dispatch.remote_ip"=>"192.168.99.1", "rack.request.query_string"=>"", "rack.request.query_hash"=>{}}
    web_1 |   error_message: Mysql2::Error: Access denied for user 'root'@'172.17.0.7' (using password: YES)
    

    How to set the config/database.yml file? Anything else wrong? For example, the mysql image.

  • Use Docker registry with SSL certifictae without IP SANs
  • Use GitLab CI to deploy app with ftp
  • Docker service start failed
  • How to generate a host unique ID?
  • Jenkins, Docker Build Step create container issue
  • Docker Alpine image + grpc: Warning: insecure environment read function 'getenv' used
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.