Skip to content

Instantly share code, notes, and snippets.

@benschw
Last active October 1, 2018 18:30
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save benschw/7391723 to your computer and use it in GitHub Desktop.
Save benschw/7391723 to your computer and use it in GitHub Desktop.
MySQL Docker Container
#!/bin/sh
docker build -t mysql .
FROM ubuntu
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install mysql-client mysql-server
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
ADD ./startup.sh /opt/startup.sh
EXPOSE 3306
CMD ["/bin/bash", "/opt/startup.sh"]
#!/bin/sh
TAG="mysql"
CONTAINER_ID=$(docker ps | grep $TAG | awk '{print $1}')
IP=$(docker inspect $CONTAINER_ID | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[0]["NetworkSettings"]["IPAddress"]')
mysql -u admin -p -h $IP
#!/bin/sh
docker run -d -p 3306:3306 -v /data/mysql:/var/lib/mysql mysql
#/bin/bash
if [ ! -f /var/lib/mysql/ibdata1 ]; then
mysql_install_db
/usr/bin/mysqld_safe &
sleep 10s
echo "GRANT ALL ON *.* TO admin@'%' IDENTIFIED BY 'changeme' WITH GRANT OPTION; FLUSH PRIVILEGES" | mysql
killall mysqld
sleep 10s
fi
/usr/bin/mysqld_safe
@awidegreen
Copy link

Does this actually work? I'm having issues with mysql_install_db which drops the privileges to the mysql user and is therefore not able to create certain files and directories in /var/lib/mysql/ (when running something similar to your startup.sh)

@goswamig
Copy link

goswamig commented Jul 2, 2014

I am getting access denied for user admit though using the password 'changeme' ?

@bholagabbar
Copy link

@benschw assuming I wanted to change the bind address to 0.0.0.0 in the mysql docker file itself, could I possibly add RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf to https://github.com/mysql/mysql-docker/blob/mysql-server/5.5/Dockerfile#L11 ? Would that work?

@davidwowa
Copy link

I have error on container build:

build command
docker build -t mysql:c_mysql .
output
Sending build context to Docker daemon 4.096kB Step 1/10 : FROM ubuntu ---> 0458a4468cbc Step 2/10 : RUN dpkg-divert --local --rename --add /sbin/initctl ---> Using cache ---> 1f00c319dccb Step 3/10 : RUN ln -s /bin/true /sbin/initctl ---> Running in 322de55db062 ln: failed to create symbolic link '/sbin/initctl': File exists The command '/bin/sh -c ln -s /bin/true /sbin/initctl' returned a non-zero code: 1

Is this part in Dockerfile with error?
RUN dpkg-divert --local --rename --add /sbin/initctl RUN ln -s /bin/true /sbin/initctl

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment