Search code examples
rubypostgresqldockerdockerfilemysql2psql-gem

Error installing mysql2psql: ERROR: Failed to build gem native extension


Im trying to build a docker container to migrate a MySql database to a Postgresql. Plan is to then ad images for each server in a docker-compose.yml, but my initial image build fails

My dockerfile:

FROM ruby:2.4.0

RUN apt-get update && apt-get install -y \ 
  build-essential \ 
  nodejs \
  mysql-client \ 
  libmysqlclient-dev \
  libpq-dev \


RUN mkdir -p /app 
WORKDIR /app

RUN gem install mysql2psql

But I get an error:

Building native extensions.  This could take a while...
ERROR:  Error installing mysql2psql:
        ERROR: Failed to build gem native extension.

    current directory: /usr/local/bundle/gems/mysql-2.8.1/ext/mysql_api
/usr/local/bin/ruby -r ./siteconf20180130-1553-1id0936.rb extconf.rb
checking for mysql_ssl_set()... yes
checking for rb_str_set_len()... yes
checking for rb_thread_start_timer()... no
checking for mysql.h... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/local/bin/$(RUBY_BASE_NAME)
        --with-mysql-config
        --without-mysql-config
extconf.rb:67:in `<main>': uninitialized constant Config (NameError)
Did you mean?  RbConfig
               CONFIG

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /usr/local/bundle/extensions/x86_64-linux/2.4.0/mysql-2.8.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /usr/local/bundle/gems/mysql-2.8.1 for inspection.
Results logged to /usr/local/bundle/extensions/x86_64-linux/2.4.0/mysql-2.8.1/gem_make.out

What am I missing?


Solution

  • First, you have an error in your Dockerfile: you should not put \ on the end of the RUN command.

    The problem you are having installing this gem relates to it just being too old. This gem requires gem mysql -v '2.8.1', which is only compatible with Ruby 2.1 or earlier.

    So, to get this working, you could change your ruby version, by using FROM ruby:2.1 or using another gem for the task, for example: https://github.com/maxlapshin/mysql2postgres.

    Working solution:

    FROM ruby:2.1
    
    RUN apt-get update && apt-get install -y \
      build-essential \
      nodejs \
      mysql-client \
      libmysqlclient-dev \
      libpq-dev
    
    RUN mkdir -p /app
    WORKDIR /app
    
    RUN gem install mysql2psql