Custom Docker MySQL build won't run - mysql

I am trying to compose my custom Dockerfile for setting up Mysql 5.7.
As part of this I would like to set s3 backup as well.
But when I try to run/create the docker instance it fails
Here is the Dockerfile:
# Start with a base mysql:5.6 image
FROM mysql:5.7
MAINTAINER Ikenna N. Okpala <me#ikennaokpala.com>
USER root
# RUN locale-gen
ENV DEBIAN_FRONTEND noninteractive
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.en
ENV LC_ALL en_US.UTF-8
ENV PS_NGX_EXTRA_FLAGS --with-cc=/usr/bin/gcc --with-ld-opt=-static-libstdc++
# Add all base dependencies
RUN apt-get update -y
RUN apt-get install -y build-essential checkinstall
RUN apt-get install -y vim curl wget unzip
RUN apt-get install -y libfuse-dev libcurl4-openssl-dev mime-support automake libtool python-docutils libreadline-dev
RUN apt-get install -y pkg-config libssl-dev
RUN apt-get install -y git-core
RUN apt-get install -y man cron
RUN apt-get install -y libgmp-dev
RUN apt-get install -y zlib1g-dev
RUN apt-get install -y libxslt-dev
RUN apt-get install -y libxml2-dev
RUN apt-get install -y libpcre3 libpcre3-dev
RUN apt-get install -y freetds-dev
# RUN apt-get install -y openjdk-7-jdk
RUN apt-get install -y software-properties-common
RUN mkdir -p /mnt/s3b
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
RUN cd ~/
RUN /bin/bash -l -c "wget https://github.com/s3fs-fuse/s3fs-fuse/archive/master.zip"
RUN unzip master.zip
RUN cd s3fs-fuse-master/ && ./autogen.sh && ./configure --prefix=/usr --with-openssl && make && make install
ADD templates/setup.sh /root/setup.sh
RUN chmod +x /root/setup.sh
ADD templates/backup-cron /etc/cron.d/backup-cron
RUN chmod 0644 /etc/cron.d/backup-cron
RUN cron
# RUN chmod +x /root/backup-cron
EXPOSE 3306
CMD ["/bin/bash", "-l", "-c", "/root/setup.sh"]
Here is the setup.sh file
#!/bin/bash
export MYSQL_HOST_IP=`awk 'NR==1 {print $1}' /etc/hosts`
set -e
set -x
# NOW=$(date +"%Y-%m-%d-%H%M")
# DUMP_FILE="/dumps/dump.sql"
echo $AWS_S3 >> ~/.passwd-s3fs && cp ~/.passwd-s3fs /etc/passwd-s3fs
chmod 600 ~/.passwd-s3fs
chmod 640 /etc/passwd-s3fs
mysql -h$MYSQL_HOST_IP -uroot -p$MYSQL_ROOT_PASSWORD -e "DROP DATABASE IF EXISTS $MYSQL_DATABASE; CREATE USER '$MYSQL_USER'#'localhost' IDENTIFIED BY '$MYSQL_PASSWORD'; CREATE DATABASE $MYSQL_DATABASE; GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'#'localhost'; FLUSH PRIVILEGES;"
Here is the docker run command:
docker run --name=mysql-s3 --env MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} --env MYSQL_USER=${MYSQL_USER} --env MYSQL_PASSWORD=${MYSQL_PASSWORD} --env MYSQL_DATABASE=${MYSQL_DATABASE} --env AWS_S3=${AWS_S3} --detach --publish 3306:3306 --volume=/vagrant/scripts/dumps/:/dumps/ --cap-add mknod --cap-add sys_admin --device=/dev/fuse --privileged mysql-s3

This approach seems too complicated using fuse and modifying the base mysql container. I would suggest that you just stick with the base MYSQL and write a script that you run in a separate container that does a MYSQL dump to a text file and then copies that text file to S3 with the AWS CLI.

Related

Dockerize Dash app that connects to sql server

I have an app that I use to connect to a sql server database. The app run smoothly on my local machine. I created a docker container for the app. In the past the app was working properly but now after re-building the container the app fails to connect to the sql server when deployed using the container. locally it still connects.
I thought it has to do with some library versions but I made sure the requirments.txt has the same versions I have installed locally.
any idea what else it could be?
This is my dockerfile
FROM python:3.9.7
RUN apt update && apt-get update
# RUN apt install odbcinst1debian2 libodbc1 odbcinst unixodbc unixodbc-dev -y
RUN wget --no-check-certificate http://ftp.us.debian.org/debian/pool/main/u/unixodbc/libodbc1_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://ftp.us.debian.org/debian/pool/main/u/unixodbc/libodbc2_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://ftp.us.debian.org/debian/pool/main/u/unixodbc/libodbccr2_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://http.us.debian.org/debian/pool/main/u/unixodbc/odbcinst_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://http.us.debian.org/debian/pool/main/u/unixodbc/odbcinst1debian2_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://ftp.us.debian.org/debian/pool/main/u/unixodbc/unixodbc_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://http.us.debian.org/debian/pool/main/u/unixodbc/unixodbc-dev_2.3.9-5_amd64.deb
RUN wget --no-check-certificate http://http.us.debian.org/debian/pool/main/u/unixodbc/unixodbc-common_2.3.9-5_all.deb
RUN wget --no-check-certificate http://http.us.debian.org/debian/pool/main/u/unixodbc/libodbcinst2_2.3.9-5_amd64.deb
RUN dpkg -i libodbc2_2.3.9-5_amd64.deb
RUN dpkg -i libodbccr2_2.3.9-5_amd64.deb
RUN dpkg -i libodbc1_2.3.9-5_amd64.deb
RUN dpkg -i unixodbc-common_2.3.9-5_all.deb
RUN dpkg -i libodbcinst2_2.3.9-5_amd64.deb
RUN dpkg -i odbcinst_2.3.9-5_amd64.deb
RUN dpkg -i odbcinst1debian2_2.3.9-5_amd64.deb
RUN dpkg -i unixodbc_2.3.9-5_amd64.deb
RUN dpkg -i unixodbc-dev_2.3.9-5_amd64.deb
RUN pip install --proxy=http://PITC-Zscaler-Americas-Houston.proxy.bakerhughes.com:80 --upgrade pip
COPY requirements.txt requirements.txt
RUN pip3 install --trusted-host bhihoutfsnuget --index-url https://bhihoutfsnuget/pypi/PyPi/simple -r requirements.txt
RUN apt-get update && apt-get install -y gnupg
# RUN apt install gnupg
RUN mkdir /app
RUN wget --no-check-certificate https://dev.mysql.com/get/Downloads/Connector-ODBC/8.0/mysql-connector-odbc_8.0.26-1debian10_amd64.deb
RUN wget --no-check-certificate https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-community-client-plugins_8.0.26-1debian10_amd64.deb
RUN dpkg -i mysql-community-client-plugins_8.0.26-1debian10_amd64.deb
RUN dpkg -i mysql-connector-odbc_8.0.26-1debian10_amd64.deb
RUN rm -rf mysql-connector-odbc_8.0.26-1debian10_amd64.deb
RUN rm -rf mysql-community-client-plugins_8.0.26-1debian10_amd64.deb
RUN rm -rf libodbc2_2.3.9-5_amd64.deb
RUN rm -rf libodbccr2_2.3.9-5_amd64.deb
RUN rm -rf libodbc1_2.3.9-5_amd64.deb
RUN rm -rf unixodbc-common_2.3.9-5_all.deb
RUN rm -rf libodbcinst2_2.3.9-5_amd64.deb
RUN rm -rf odbcinst_2.3.9-5_amd64.deb
RUN rm -rf odbcinst1debian2_2.3.9-5_amd64.deb
RUN rm -rf unixodbc_2.3.9-5_amd64.deb
RUN rm -rf unixodbc-dev_2.3.9-5_amd64.deb
COPY ./v2 /app
WORKDIR /app
CMD ["python","main.py"]

How to install mysql-server in debian:buster using script without being asked any configuration questions?

I am trying to install mysql-server in debian:buster using Dockerfile and shell script, when I run this command
apt install -y mysql-server and during the installation I need to enter the password and retype it as shown in the picture:
I did a little search and I found how to do it using here-string in this page Install MySQL on Ubuntu without a password prompt
so because I am using this command apt install -y mysql-server I did it like this:
debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password'
apt-get -y install mysql-server
but when I run them I still get the same thing and I need to enter the password.
Here is my Dockerfile:
From debian:buster
COPY ./run.sh /
CMD bash /run.sh
and this is run.sh:
apt update
apt install -y nginx gnupg wget lsb-release
wget https://dev.mysql.com/get/mysql-apt-config_0.8.13-1_all.deb
printf "1\n1\n4\n" | dpkg -i mysql-apt-config_0.8.13-1_all.deb //version selected is mysql-5.7
apt update
service nginx start
bash //just to stay in the bash of container and don't quit from it.
by the way I am trying to install mysql-server from inside the bash of the container then I'll write the commands in my script.
and there is something weird as you can see in the picture above it's written :
Configuring mysql-community-server
----------------------------------
even that I am installing with this command apt-get -y install mysql-server not this
apt-get -y install mysql-community-server
So do you know what am I missing here?
Use
shell> sudo debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password mypassword"
shell> sudo debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password mypassword"
shell> sudo DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server
Documentation

docker run mysql just during one command

I am trying to install phpmyadmin into docker containers.
I am doing something like that :
Installing the MySQL-server and the phpmyadmin dependencies.
In one command:
Start MySQL-server.
Install phpmyadmin.
Stop MySQL-server.
This Dockerfile works perfectly on my computer but it is failing on hub.docker.com
Is anyone who how to execute MySQL command during the docker building process ?
RUN apt-get install -y -qq --no-install-recommends \
mysql-server-5.7 \
apache2 \
php7.0 \
php7.0-zip \
php7.0-bz2 \
php7.0-gd \
libapache2-mod-php7.0
# Fix MySQL home error
RUN usermod -d /var/lib/mysql mysql
RUN service mysql start && \
apt-get install -y -qq --no-install-recommends phpmyadmin && \
service mysql stop

Docker - How can I initiate mysql database with a .sql file running on a container?

It is my first time working with docker. I want to create a remote container with test environment for my java application. I also need mysql database. Everything should run on one container (it's a requirement, not my idea).
I need to have a db initiated with .sql file (let's say file.sql located in the same directory as Dockerfile)
Thank you very much for help :)
Here is part of my Dockerfile:
FROM ubuntu:16.04
#Install JRE
RUN apt-get update && apt-get install default-jre -y
#Install JDK
RUN apt-get update && apt-get install default-jdk -y
#Install Maven
RUN apt-get update && apt-get install maven -y
#Install wget
RUN apt-get update && apt-get install wget -y
#\&& rm -rf /var/lib/apt/lists/*
#Install unzip
RUN apt-get update && apt-get install unzip -y
#Install glassfish
RUN mkdir -p /opt && cd opt/
RUN wget http://download.java.net/glassfish/4.1.1/release/glassfish-4.1.1.zip
RUN unzip glassfish-4.1.1.zip
#Install mysql
ENV MYSQL_USER=mysql\ MYSQL_DATA_DIR=/var/lib/mysql \ MYSQL_RUN_DIR=/run/mysqld \ MYSQL_LOG_DIR=/var/log/mysql
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server \&& rm -rf ${MYSQL_DATA_DIR} \&& rm -rf /var/lib/apt/lists/*
First use a ADD to copy your .sql file in the image. Then add a RUN command to restore your DB using mysql commands.
Thank you all for answers!
I found a sollution if anyone will encouter the same problem it's here:
RUN echo "mysql-server mysql-server/root_password password password" | debconf-set-selections
RUN echo "mysql-server mysql-server/root_password_again password password" | debconf-set-selections
RUN apt-get update && apt-get -y install mysql-server && service mysql start
if mysql is still not starting check /etc/docker/daemon.json, make sure it looks like this:
{
"storage-driver":"overlay2"
}
if you don't have this file execute:
mkdir -p /etc/docker
echo '{"storage-driver":"overlay2"}' > /etc/docker/daemon.json
systemctl restart docker.service

Access source container from recipient container

I have a mysql container created using docker file
from ubuntu:14.04
run apt-get update && apt-get install -y apt-transport-https
run apt-get install mysql-server -y
run apt-get install mysql-client -y
RUN apt-get update
RUN apt-get install software-properties-common -y
RUN add-apt-repository ppa:webupd8team/java -y
RUN apt-get update
RUN echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
RUN apt-get install oracle-java7-installer -y && \ oracle-java7-set-default
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
env set MYSQL_ROOT_PASSWORD=root
EXPOSE 3306
and an java container from java:8 image. The java container is linked to mysql container using --link option.
docker run -it --link mysqlc:csql java:8
Now i need to execute mysql commands from java container but mysql commands not working in java container. What is the problem?
I need to start mysql, create databases from java container? How to do this?
Do i need to use another image other than java to access mysql container?
An approach is building your own Java image that contains mysql-client as long as Java:
Dockerfile:
FROM Java:8
RUN apt-get update && apt-get install mysql-client
Build:
docker build . -t my-java-mysql
Run:
docker run -it --link mysqlc:csql my-java-mysql:8
Then you have mysql client command available in your Java container.