Docker and MySQL can't connect

I can’t for the life of me figure this out. I feel like I’m really close, but im just not quite there yet. Here’s my Dockerfile:

FROM ubuntu:14.04
MAINTAINER Oscar Godson

RUN apt-get update
RUN apt-get install -y nodejs-legacy
RUN apt-get install -y npm
RUN apt-get install -y mysql-server
RUN apt-get install -y mysql-client

COPY . /src

WORKDIR /src

RUN service mysql start;\
    mysql -u root < /src/bin/demo-data.sql;\
    npm install

EXPOSE 4000

CMD ["node", "index.js"]

If i go into interactive mode and go into bash in that RUN service... line at the end I can see that mysql is running, the demo data was imported and everything works including starting my Node app with node index.js. If I then try to do another RUN or the CMD you see below it no longer can access MySQL. It’s like it stops running.

  • Gulp not detecting file changes in subfolder
  • Is it possible to have a Supervisord pretask?
  • How to use Docker with Vue.js?
  • Docker: Npm install very slow in Ubuntu host
  • Possible to run Google Standard Managed VM Locally with Docker for Development?
  • Flannel and docker don't start
  • The error I get Error: connect ECONNREFUSED which happens when it’s down or the data isn’t there. From me playing with it it looks like MySQL isn’t running if I try to access it after the RUN where I start it.

    P.S. The MySQL DB here is purely for development. On production we use AWS RDS, so thats why its using root without any password and importing data that way.

  • How to know if my program is completely started inside my docker with compose
  • Docker toolbox cannot allocate memory
  • Docker devicemapper storage space
  • Docker : How to run a service and a terminal in one command?
  • Docker: fail linking with mysql DB
  • swift 3.1.1.-release on linux copyItem(atPath:toPath:) is not yet implemented
  • One Solution collect form web for “Docker and MySQL can't connect”

    The problem was that RUN shouldn’t be used for long running processes, however, you can only use one CMD option. For some reason it didn’t click with me until a friend pointed it out, but I could just make a “installer.sh” script and call that with CMD. Now it’s not losing the MySQL context. My update Dockerfile looks something like this:

    FROM ubuntu:14.04
    MAINTAINER Oscar Godson
    
    RUN apt-get update
    RUN apt-get install -y nodejs-legacy
    RUN apt-get install -y npm
    RUN apt-get install -y mysql-server
    RUN apt-get install -y mysql-client
    
    COPY . /src
    WORKDIR /src
    
    RUN cd /src;npm install
    
    EXPOSE 4000
    
    CMD ./bin/installer.sh
    

    Installer than has

    service mysql start
    mysql -u root < ./bin/demo-data.sql
    node index.js
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.