Mysql installation on docker container

11,277

Solution 1

UPDATE: You should check exposed port number - in your example is(was) port for memcached (11211) and not the port for mysql (3306).


Anyway, I think that you may need to modify your Dockerfile - remove unnecessary sleep in entrypoint:

ENTRYPOINT ["/usr/bin/mysqld_safe"]

Then you should start your container this way (daemon mode):

root@machine:/# docker run -d -p 3306:<host port> <image id>

Solution 2

answer has already been accepted, but i think you could leave the sleep in your entrypoint if you change the '&' to '&&'. not sure if docker does any parsing of the entrypoint or just executes it, but bash treats '&' very differently than '&&'.

ENTRYPOINT mysqld_safe && sleep 10
Share:
11,277
Hitesh
Author by

Hitesh

Updated on June 24, 2022

Comments

  • Hitesh
    Hitesh almost 2 years

    I am trying to run mysql into modified ubuntu image which includs installation of Node.js and basic mysql installation using below docker file

    # Memcached
    
    # use the ubuntu base image provided by dotCloud
    FROM ubuntu/mysqlbase
    MAINTAINER Hitesh
    
    # make sure the package repository is up to dat//e
    #RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
    #RUN apt-get update
    
    #RUN apt-get install -y mysql-client
    #ENTRYPOINT ["wc", "-l"]
    #ENTRYPOINT ["echo", "running"]
    ENTRYPOINT mysqld_safe & sleep 10
    #RUN mysql
    RUN echo "[mysqld]"                       >/etc/mysql/conf.d/docker.cnf
    RUN echo "bind-address   = 0.0.0.0"      >>/etc/mysql/conf.d/docker.cnf
    RUN echo "innodb_flush_method = O_DSYNC" >>/etc/mysql/conf.d/docker.cnf
    RUN echo "skip-name-resolve"             >>/etc/mysql/conf.d/docker.cnf
    RUN echo "init_file = /etc/mysql/init"   >>/etc/mysql/conf.d/docker.cnf
    RUN echo "GRANT ALL ON *.* TO root@'%'" >/etc/mysql/init
    
    USER root
    
    EXPOSE 3306
    

    On running this server using below command

    sudo docker run -p 3306:13306 mysql/dockerfiletest
    

    Following error was encountered

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    

    Can some one please suggest what is needed to be changed here. I want to use this container to be linked with other container which is essentially running my node.js app.

  • Jiri
    Jiri about 10 years
    Anyway I tried to build docker image with my modification without sleep and it works well. I can connect to mysqld from host without problems.
  • Hitesh
    Hitesh about 10 years
    Thanks Jiri to point that out, Sleep here is just ensuring that mysqld_safe is done which might not be necessary as you pointed out. My issue was because of messed up between memcache and mysql ports which is sorted now.
  • Mark
    Mark over 9 years
    I'm confused, wouldn't that command expose port 3306 on the localhost to the <host port> on the container? Shouldn't port 3306 on the container be forwarded to some port on the host if the point is to connect to MySQL running on the container via the localhost?
  • Jiri
    Jiri over 9 years
    @Mark Yep, you are corect the first port is host port and second port is container port.
  • j-mo
    j-mo over 5 years
    a few things: it's "race condition", and i wasn't saying the sleep would fix the issue. i was pointing out (correctly) that & is likely not what OP wanted -- for that, he should use &&
  • Melroy van den Berg
    Melroy van den Berg over 5 years
    OK agree with that