Mysql installation for alpine linux in docker - mysql

I have still problem with mysql installation on alpine in docker.
I need also openjdk:8u201-jdk-alpine3.9.
My dockerfile:
FROM openjdk:8u201-jdk-alpine3.9
RUN apk update && \
apk add mysql mysql-client && \
rm -f /var/cache/apk/* && \
addgroup mysql mysql && \
mkdir run/mysqld && \
touch /var/run/mysqld/mysqld.sock && \
touch /var/run/mysqld/mysqld.pid && \
chown -R mysql:mysql /var/run/mysqld/mysqld.sock && \
chown -R mysql:mysql /var/run/mysqld/mysqld.pid && \
chmod -R 644 /var/run/mysqld/mysqld.sock && \
apk add openrc --no-cache
But, in container:
- there is no mysql service (only mariadb, but it is probably standard situation for alpine)
- there is no any my.cnf file in /etc/mysql directory
When I tried mysql command, I got error:
Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (111)
When I tried mysql -h 127.0.0.1 -P 3306 -u root command, I got error:
Can't connect to MySQL server on '127.0.0.1' (115)
That same for localhost.

First of all - why do you want to install mysql-server on that specific image?
I would suggest using 2 separate containers - one for the mysql database and one for the application you are developing using that openjdk image.
You can simply use this gist https://gist.github.com/karlisabele/d0bebe3d27fc44a57d1db9a9abdff45a
to create a setup where your Java application can connect to mysql database using database (the service name) as hostname for mysql server.
See https://hub.docker.com/_/mysql for additional configurations on mysql image (you should define MYSQL user, password, db name etc...)
UPDATE
I updated the gist and added the environment variables necessary for the mysql to work... Replace the qwerty values with your own and you should be able to access the database from your Java application via database:3306 using the username and password provided in the environment variables
The volumes definition at the end of the file tells docker daemon that we want to create a persistent volume somewhere in the host file system and use mysql as an alias. So that when we define the database service volumes we can simply use the mysql:/var/lib/mysql and it will actually mount an obscure folder that docker created on your filesystem. This will allow the mysql database to have persistent state everytime you start the service (because it will mount the data from your host machine) and you don't have to worry about the actual location of the volume
You can see all volumes by running docker volumes ls

vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
#skip-networking
restart the mysql container

Related

Deploy mysql during build from Dockerfile

I am building an application which has parent and child dependency and to build of my application which is the final stage of build i need to connect to mysql for it during build stage itself.
In this i am getting the error:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
I have mentioned my docker file code i am using and for mysql i have pulled image from dockerhub following instructions from below link:
https://dev.mysql.com/doc/mysql-installation-excerpt/5.5/en/docker-mysql-getting-started.html
And i was planning to run this as a separate container using bridge to communicate with my above container using below command:
docker run -d -name app-container-name --link mysql-container-name app-image-name
FROM maven:3.5.4-jdk-8 as maven
COPY ZP ZP
COPY CommonApp CommonApp
RUN cd ZP && mvn clean install
RUN cd CommonApp && mvn clean install package -U && mvn install:install-file -Dfile=/CommonApp/target/commonapp-0.0.1-SNAPSHOT.jar -DgroupId=com.z -DartifactId=commonapp -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar;
FROM mysql:5.7
# ROOT PASSWORD
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_USER=root
ENV MYSQL_PASSWORD=root
ENV MYSQL_DATA_DIR=/var/lib/mysql \
MYSQL_RUN_DIR=/run/mysqld \
MYSQL_LOG_DIR=/var/log/mysql
RUN /etc/init.d/mysql start && \
mysql -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root';FLUSH PRIVILEGES;"
#PORT
EXPOSE 3306
FROM maven:3.5.4-jdk-8
COPY ZCApp ZCApp
RUN cd ZCApp && mvn clean package -U
How should i approach this problem. How can i build mysql along with the application itself using dockerfile.?
Had the same issue when built maven project. What makes this different from similar requests is that here you don't link two running containers but instead you link docker daemon, preforming build process, to running container.
For Docker to get access to database during build you have to expose ports of database. Using --link will have no effect because it links containers (and you dont have second container at the moment) and btw is considered as obsolete technique.
You have to explicitly start database container before build process and somehow expose its ports for docker daemon to access them.
Option 1 - using host networking.
First start database:
docker run -d --network=host mysql
Then build:
docker built -t foo .
Docker will see database on localhost during build process because database uses host's network and doesn't need any port exposion.
Option 2 - Expose ports
First start database:
docker run -d -p 3306:3306 mysql
Then build:
docker built -t foo .
Docker will again see database on localhost during build process because port is exposed.
What you have to double check is your connection string in mvn. It has to use localhost and default tcp port 3306

How to pre-configure and prefill official MySQL docker container?

I want to build a new MySQL image based on the official MySQL docker container image. I want to reduce the number of parameters I need to add when running the image. (eg. -e MYSQL_USER, -e MYSQL_DATABASE and even -e MYSQL_ROOT_PASSWORD='rootsecret')
that already includes my settings for the global variables and my Create Database SQL file in the docker-entrypoint-initdb.d folder.
How do I add all my settings and create a new image to simply docker run mysql:config1 docker run mysql:config2 and so on?
You could build your own mysql docker image using a docker file, configure username, password and everything else you might need, build that image, upload it to the docker hub and then when you launch a new docker container you just use the previously built container.
An example of a Docker file to build an ubuntu image with a mysql server inside would be something like bellow (save it to a file called Dockerfile):
FROM ubuntu:latest
RUN apt-get update \
&& apt-get install -y apt-utils \
&& { \
echo debconf debconf/frontend select Noninteractive; \
echo mysql-community-server mysql-community-server/data-dir \
select ''; \
echo mysql-community-server mysql-community-server/root-pass \
password 'Desired-Password'; \
echo mysql-community-server mysql-community-server/re-root-pass \
password 'Desired-Password'; \
echo mysql-community-server mysql-community-server/remove-test-db \
select true; \
} | debconf-set-selections \
&& apt-get install -y mysql-server mysql-client
Then build your mysql docker container like this (you have to be in the folder where the Docker file was/is saved):
docker build my-ubuntu-mysql-docker
Then you have to push it to the docker hub and then you can use it to start a new docker container like this:
docker run -d -p 2222:22 -p 3306:3306 --name my-ubuntu-mysql-docker ...
Where 2222 is local ssh port mapped to ssh port 22 of the docker container and 3306 is local mysql port mapped to the mysql port of the docker container.
I hope this helps!
The following has to be written into the Dockerfile:
FROM mysql:latest
LABEL Name=mylabel Version=0.0.1
COPY path/to/sh/sql/sql.gz/files /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD='rootpassword'
As stated in the documentation on the official docker website:
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions .sh, .sql and .sql.gz that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order.
What you you would want to do is to modify Mysql's image entry point
and please note that you do not need to pass all the parameters, most of them are optional

How to create mysql volume which exists even if i remove the docker container

I created docker container with mysql using -v option:
-v /storage/mysql1/mysql-datadir:/var/lib/mysql
When i removed container i lost database saved in /var/lib/mysql .
How can i create mysql container with -v option (to see everything from the host) and not lost the data?
Have you tried restarting the mysql service in your host?
This is something like restoring MYSQL db from physical file.
When you perform db operations inside container, it saves real-data and indexes in /var/lib/mysql path or the path that you specify while installing.
But, for Host machine, MYSQL service still does not know about the change.
Restart the MYSQL service by service mysql restart.
Additionally check the owner and permission in the MYSQL data folder like below:
sudo chown -R mysql:mysql /var/lib/mysql
sudo chmod -R 660 /var/lib/mysql/
sudo chown -R mysql.mysql /var/log/mysql
While running container, mount /var/log/mysql as well along with data folder.

How to start mysql server in docker container

I am creating docker container and base image is ubuntu:14.04. I have to start mysql server in that container and then I have to create databases and I have to give the permission to the users. Docker is new for me. I tried a lot but still whenever I go to that image and check for mysql server is running or not. Every time what I got is mysql server is stopped and my dbs are also not created.
This is my dockerfile
FROM ubuntu:14.04
MAINTAINER <name> <emailid>
RUN sudo apt-get update
RUN sudo apt-get install -y apache2 mysql-server libapache2-mod-auth-mysql php5-mysql php5 git
RUN sudo apt-get install -y vim
CMD sudo /usr/sbin/mysqld -u mysql
I tried a lot but i am not able to run mysql server in docker image.
Did you manually install it in your container?
Why do you not simply use:
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_DATABASE=mySchema mysql:5
That would start a container named mysql running a mysql daemon, setting the default root password to secret, creating a new schema called mySchema and expose the MySQL port 3306 to clients so that they could connect.
Then you could connect to that with any MySQL client using the root user with the specified password secret and create your tables within the created schema mySchema.
I had similar issue for ubuntu :14.04 while setting up druid cluster in the docker container, using CMD to start mysql fixed it for me.
CMD mysql start \
Druid related stuff
&& mysql stop
docker exec -it container_name/id bash
service mysql status to check on the service status
service mysql start to start the mysql service

Mysql installation on docker container

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.
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>
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