Can't delete mysql database, table or even alter table inside docker - mysql

I want to have a mysql database with some basic dataset.
I create mysql docker image using this https://index.docker.io/u/brice/mysql/ Dockerfile, but delete VOLUME ["/var/lib/mysql", "/var/log/mysql"] line, so the Dockerfile looks like:
FROM ubuntu:12.10
MAINTAINER Brandon Rice <brice84#gmail.com>
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -s /bin/true /sbin/initctl
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install 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
RUN /usr/bin/mysqld_safe & \
sleep 10s && \
mysql < create_my_db.sql && \
mysql -e "GRANT ALL ON *.* to 'root'#'%'; FLUSH PRIVILEGES"
EXPOSE 3306
CMD ["mysqld_safe"]
After that I build image such as:
docker build -t my_db_mysql .
Everything is ok while i append data, but when i want to delete db, for example:
FROM my_db_mysql
RUN /usr/bin/mysqld_safe & \
sleep 10s && \
mysql -e "DROP DATABASE my_db;"
EXPOSE 3306
CMD ["mysqld_safe"]
I obtain next error:
ERROR 6 (HY000) at line 1: Error on delete of './my_db//db.opt' (Errcode: 1)
It is appears not only when I want to build image, but even when I exec: mysql -u user -p -e "DROP DATABASE my_db;"
How to solve this?
Thanks
Update: Also I tried to run docker with different filesystem, e.g -s vfs or -s devicemapper, but nothing changed.
When I build image with VOLUME ["/var/lib/mysql", "/var/log/mysql"] - everything works properly, but i can't commit this changes.
Update: Seems I resolve this issue. Problem was in host machine with ubuntu 12.04. Issue disappeared when I update ubuntu to 13.10. Thank a lot!

Related

Installing MySQL on CentOS 7 using Dockerfile failing

I am trying to setup MySQL using a Dockerfile which uses CentOS 7 .. I know I should be using a seperate container for MySQL, but due to limitations I can't, so now I have to install it using the Dockerfile and i am running into some issues.
I have looked a few different places and got some ideas, and put the following together;
FROM centos:7
RUN yum -y update
RUN yum -y install \
httpd \
mariadb-server \
mariadb \
&& rm -rf /var/cache/yum/* \
&& yum clean all
ENV DB_USER testuser
ENV DB_PASSWORD testpass
ENV DB_NAME testdb
RUN /usr/bin/mysqld && sleep 5 && \
mysql -uroot -e "CREATE USER '${DB_USER}'#'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'#'%' WITH GRANT OPTION" &&\
mysql -uroot -e "CREATE DATABASE ${DB_NAME}"
EXPOSE 80
EXPOSE 3306
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
This seems to be for abother distro as /usr/bin/mysqld doesnt actually exist on Centos7 and I can find no other way to set things up manually ... I usually use the mysql_secure_installation command when setting up a VM, but as im doing this through a Dockerfile i can't as it requires input from the user.
I was wondering if anybody has manually setup MySQL using a Dockerfile on Centos and has some tips to get this going.
The error i keep coming up against when trying to use Mysql in the container is;
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

Connecting to mysql in docker fails

I'm setting up a Dockerfile where I can run my automated tests, and I'm having troubles with connecting to mysql database.
The Dockerfile depends on a prevoously built image and looks like this:
# Stage 0, assign argument as multistage image alias
ARG PHP_IMAGE
FROM ${PHP_IMAGE} as image
# Stage 1, start tests
FROM php:7.2-fpm
RUN curl -sS https://getcomposer.org/installer | php \
&& chmod +x composer.phar && mv composer.phar /usr/local/bin/composer
RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
apt-get install -yq nodejs build-essential \
git unzip \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
subversion \
&& curl -sL https://deb.nodesource.com/setup_8.x | bash - \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-enable mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install -j$(nproc) mysqli
RUN apt-get install -y mysql-server
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini
RUN npm install -g npm
COPY --from=image /var/www/html/ /var/www/html/
WORKDIR /var/www/html/
COPY scripts/develop.sh develop.sh
COPY scripts/docker-test.sh docker-test.sh
RUN ["/bin/bash", "-c", "bash develop.sh && bash docker-test.sh"]
I've added RUN mysqladmin -u root -p status to try to debug why connecting to mysql failed and I got
Enter password: mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
To run this I am running
docker build -t $TEST_DOCKER_NAME --build-arg PHP_IMAGE=$DOCKER_IMAGE_NAME_PHP -f Dockerfile.test .
The TEST_DOCKER_NAME and DOCKER_IMAGE_NAME_PHP are stored in an env file and read from there. The PHP image was built successfuly and I'm using it to copy the files from there to here so that I can run PHPUnit.
When I remove that RUN line my build fails when I'm trying to run a script that creates the database
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (99 "Cannot assign requested address")'
Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'
What do I need to do in my Dockerfile to make it work?
Answer to your specific problem
This is a common mistake people make when using docker. When you use the RUN directive in docker you are running a command through to completion, capturing the filesystem changes and then exiting.
So when you have the lines
RUN /etc/init.d/mysql start
RUN mysqladmin -u root -p status
The first one is starting mysql. But then the changes are captured, the container is exited and then a new one is started to run the mysqladmin command. Therefore the mysql process is no longer running.
To avoid this you could combine them into a single line like
RUN /etc/init.d/mysql start && mysqladmin -u root -p status
However you will need to do this every time you want to use mysql. Such as in your develop.sh.
Wider answer
It is not recommended to run multiple processes within your container and it is also not recommended to use init.d or other system startup frameworks within your container.
You seem to be treating your container like a virtual machine and are having issues because containers are not VMs.
I recommend you explore running mysql in a separate container and then using a tool like docker-compose to start and and stop your containers.

How to Install mysql-server via dockerfile

Does anybody knows how to install mysql-server via dockerfile? I have written a Dockerfile, but the build ends with an error: /bin/sh: 1: /usr/bin/mysqld: not found
USER root
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server-5.7
# Remove pre-installed database
RUN rm -rf /var/lib/mysql/*
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf
ENV DB_USER example
ENV DB_PASSWORD example
ENV DB_NAME example
ENV VOLUME_HOME "/var/lib/mysql"
EXPOSE 3306
RUN cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
RUN /usr/bin/mysqld && sleep 5 && \
mysql -uroot -e "CREATE USER '${DB_USER}'#'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'#'%' WITH GRANT OPTION" &&\
mysql -uroot -e "CREATE DATABASE ${DB_NAME}" && \
mysqladmin -uroot shutdown
For an ubuntu:16.04 base image, mysqld is found in /usr/sbin, not /usr/bin
If you can add a step RUN which mysqld before your final RUN command that will show you where the mysqld executable is found. It may vary depending on which base image/distro you're using.
You can also use RUN mysqld ... without a full path, if the file is in your $PATH
You may also need to update your RUN sed line as below, adding spaces around the quoted string:
RUN sed -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
Otherwise, you may see the following error:
The command '/bin/sh -c sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf' returned a non-zero code: 1

Can't MariaDB inside docker container

I need to run MariaDB inside existing Docker container.
Building and installation works just fine, but when Docker executes
RUN mysql < init.sql
to load DB schema I get
Can't connect to MySQL server (111 Connection refused)
However when I run the container and execute
docker exec -it silly_allen /bin/bash -c "mysql < init.sql"
it works just fine.
What might be the problem?
Thanks!
EDIT: Here's part of Dockerfile related to DB.
FROM centos:7
WORKDIR /root
...
RUN echo "[mariadb]" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "name = MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "baseurl = http://yum.mariadb.org/10.1/centos7-amd64" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB" >> /etc/yum.repos.d/MariaDB.repo
RUN echo "gpgcheck=1" >> /etc/yum.repos.d/MariaDB.repo
RUN rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
RUN yum install -y MariaDB-server MariaDB-client
RUN yum clean all
RUN echo "[mysqld]" > /etc/my.cnf
RUN echo "bind-address=0.0.0.0" >> /etc/my.cnf
RUN /etc/init.d/mysql restart
ADD init.sql /root
RUN mysql < /root/init.sql
...
According to Docker's best practices, you should be having 1 container per process that you want to run.
Also, there's an official mariadb image which allows you to mount a directory as volume, that could contain SQL dumps. These dumps are auto-imported when the container gets created, so this might prove to be handy.
I'd suggest instead of having one very large dockerfile, you break it up in separate services with docker-compose
If you do however want to keep this the way it is, I'd suggest you move the ADD init.sql ... part to the top, and concatenate the server starting up part and the dump import, because each RUN command is a separate layer with Docker. So you'd need something like what's described in the answer of this StackOverflow question:
RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
sleep 5 && \
mysql -u root -e "CREATE DATABASE mydb" && \
mysql -u root mydb < /root/init.sql
So that the server initializes and the dump gets imported in one layer
From what I can see, you are trying to run mysql < init.sql before starting the database. The error shows that this command requires the database to be running.
To solve this problem, add a startup script into you container containing:
mysqld
mysql < init.sql
And change your Dockerfile CMD to call this script.
This way is right:
# cat Dockerfile
...
ADD init.sql /tmp
ADD initdb.sh /tmp
RUN /tmp/initdb.bash
CMD ["/usr/bin/mysqld_safe --datadir=/var/lib/mysql"]
And the script:
# cat dump/initdb.bash
#!/bin/bash
set -e
set -x
mysqld_safe --datadir='/var/lib/mysql' --user=root &
until mysqladmin ping >/dev/null 2>&1; do
sleep 0.2
done
mysql -e 'create database init;' && \
mysql init < /tmp/init.sql && \
echo "Successfully imported" && exit 0

Dockerfile and background running mysql server

i have problems...
Firstly i have a Dockerfile where i define all the steps, like updating system, installing mysql, change mysql root password.
Then i set an EntryPoint so my container on start will exec mysql server.
I have 2 problems:
- When i start the container, it restarts every 10 seconds.
- When i use exec to enter the docker it says: "No docker with such id".
This is my Dockerfile:
# Set the base image
FROM ubuntu:14.04
MAINTAINER redigaffi
RUN apt-get update \
&& apt-get -y install mysql-server \
&& service mysql start \
&& mysqladmin -u root password FEGj5nmKYRha
ENTRYPOINT service mysql start \
&& bash
#VOLUME /root/mysql:/var/lib/mysql:rw Please run -v running this docker since Dockerfile has not access to host files
EXPOSE 3306
I put bash on the end on the entrypoint because without it container just closes, so it remains in background.
I have tried many commands to execute this container:
docker run -d df0bb600c10f /bin/bash # This one closes the container after 2 seconds
docker run -d --restart=always df0bb600c10f /bin/bash # This one remains, but restarts every 10 seconds and i cant access this docker using exec.
Please help, what is wrong ?
Thank you!
Try using the supervisor. This article here shows the steps.