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.

  • Docker - Mount docker image's root folder on different Disk
  • docker image running a mac osx installation
  • Dockerfile and docker-compose not updating with new instructions
  • Forward local SSH credentials to Docker container inside Vagrant VM
  • Building & running a mock server's docker container
  • Build docker image fail : Exiting on user command
  • 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


      image: mysql
      build: .
      command: bundle exec rails s -p 3000 -b ''
        - .:/testapp
        - "3000:3000"
        - db

    My database setting file:


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

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

    Access denied for user 'root'@'' (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"=>"", "REMOTE_HOST"=>"", "REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"", "SERVER_PORT"=>"3000", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.2.0/2014-12-25)", "HTTP_HOST"=>"", "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"=>"", "rack.request.query_string"=>"", "rack.request.query_hash"=>{}}
    web_1 |   error_message: Mysql2::Error: Access denied for user 'root'@'' (using password: YES)

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

  • How to have default file in image that I can override during container run?
  • Using Ansible to install Docker
  • Cluster node can't rejoin the cluster in docker swarm after its restart
  • Shell script: Remove hello world docker container without knowing ID
  • Azure App Service + Docker Autodeploy?
  • Why can “docker-compose run” create files outside the container?
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.