Sunday 15 May 2016

Running Rails on Dockerized MySQL or Postgres instances

At the moment I need to have an installation of MySQL and Postgresql on my machine to help migrate an application from Postgres to MySQL. There were a few of pitfalls along the way. Here are some of the things I learned in the setup process:

  1. When you create the docker image you need to specify the port that will be accessible on the host system. To do this you need to specify the -p option to the run command. That means that it must directly follow run. For example 'docker run -p ...'
  2. You also need to specify which IP address the container should listen on and what host port should be mapped to the internal port of the container: '... 127.0.0.1:3306:3306 ...'
  3. This as a whole comes out as the following:
docker run -p 127.0.0.1:3306:3306 --name database-name-mysql -d mysql:5.5.43
The database.yml file needs the host to be set to an IP, not 'localhost'.

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  pool: 55
  host: 127.0.0.1 # NOTE: This can't be 'localhost' when you are running in Docker
  port: 3306

Introductory post

You are welcome to use any ideas you find on this blog, at your own risk. It's a compilation of useful ideas I've found for developing applications.

Many of the tips here are from development in Rails, but I may include JavaScript suggestions too.