Access source container from recipient container - mysql

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.

Related

How to install mysql-server in a Dockerfile?

I have a complex Dockerfile which install much more than just mysql-server so I cannot start from an existing mysql container.
When removing all the extra-stuff I get this Dockerfile
FROM ubuntu:latest
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_ROOT_USER=root
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y mysql-server
RUN usermod -d /var/lib/mysql/ mysql
RUN service mysql start
Unfortunately, mysql does not want to start:
---> 57a66bd64c2c
Step 8/9 : RUN usermod -d /var/lib/mysql/ mysql
---> Running in 596df248c2e4
---> ee78442bcc56
Step 9/9 : RUN service mysql start
---> Running in 0d9e5803cf33
* Starting MySQL database server mysqld
...fail!
The command '/bin/sh -c service mysql start' returned a non-zero code: 1
What is my mistake?
Looks like you've removed the most important parts of your docker file. Here is the Official MySQL repo Docker file.
FROM oraclelinux:7-slim
ENV PACKAGE_URL https://repo.mysql.com/yum/mysql-8.0-community/docker/x86_64/mysql-community-server-minimal-8.0.2-0.1.dmr.el7.x86_64.rpm
# Install server
RUN rpmkeys --import http://repo.mysql.com/RPM-GPG-KEY-mysql \
&& yum install -y $PACKAGE_URL \
&& yum install -y libpwquality \
&& rm -rf /var/cache/yum/*
RUN mkdir /docker-entrypoint-initdb.d
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 3306 33060
CMD ["mysqld"]
You need to include a proper source with correct version to pull the image from.
and expose right ports, separate out volumes for MySQL to run. your container maybe failing due to any of this. I'd say remove the MySQL part out of your dockerfile and run the rest of the container.
Use the official mySQL image and install it in separate container. and then you can connect the Database with other apps.

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

Custom Docker MySQL build won't run

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.

Could not install mysql-server inside docker container

I want to configure a container with MySQL, thereafter I will add a java application to the container. But, I could not install MySQL-server inside the container.
here is my dockerfile:
FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install mysql-server --fix-missing --fix-broken
EXPOSE 3306
CMD ["/usr/bin/mysqld_safe"]
I got the following error:
Errors were encountered while processing:
/var/cache/apt/archives/mysql-server-5.5_5.5.44-0ubuntu0.14.04.1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
2015/09/01 13:17:36 The command [/bin/sh -c apt-get -y install mysql-server --fix-missing --fix-broken] returned a non-zero code: 100
What's probably happening is the apt-get install is asking for input, and failing. What happens if you just run those commands interactively in the base container? e.g. launch a shell with docker run -ti --rm ubuntu:latest /bin/bash
One thing you can try is to use the DEBIAN_FRONTEND=noninteractive environment variable, e.g.
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -yq install mysql-server
I would suggest you use the official mysql docker image. When running that you have to provide the root password as an environment variable during the docker run command. That would be better.