docker run mysql just during one command - mysql

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

Related

[Fail]: Starting MySQL in my Ubuntu docker

FROM ubuntu
RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y mysql-server && \
apt-get install -y mysql-client
Then, I started a container, and ran:
/etc/init.d/mysql restart
Unfortunately, it didn't work:
root#5e37c0985d07:/opt# /etc/init.d/mysql restart * Stopping MySQL
database server mysqld
[ OK ]
* Starting MySQL database server mysqld
No directory, logging in with HOME=/
[fail]
Please note No directory, logging in with HOME in the error message.
Try usermod -d /var/lib/mysql mysql and then /etc/init.d/mysql restart
Try First to create an empty directory "mysql" at the host machine and then run the container with this directory path bind to the container directory path.
Ex:
docker run -i -t -p "3306:3306" -v ${PWD}/mysql:/var/lib/mysql "docker-image"

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.

Docker : Start mysql and apache from entrypoint or CMD

Building a docker image for development, I want to start automatically mysql and apache when I run the image.
If I log into the container and run "service apache2 start" and "service mysql start" it works. But if I put in entrypoint or CMD it fails.
I was able to start apache by putting ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]but I was not able to start mysql programmatically.
I tried many many things. Most of the time if fails silently in that the container is not running, other time I got : docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"/etc/init.d/mysql start\": stat /etc/init.d/mysql start: no such file or directory"
This is what I have so far :
FROM debian:wheezy
RUN apt-get update && \
apt-get install -y libmcrypt-dev \
subversion ssl-cert nano wget unzip && \
echo "deb http://packages.dotdeb.org wheezy-php56 all" >> /etc/apt/sources.list.d/dotdeb.list && \
echo "deb-src http://packages.dotdeb.org wheezy-php56 all" >> /etc/apt/sources.list.d/dotdeb.list && \
wget http://www.dotdeb.org/dotdeb.gpg -O- | apt-key add - && \
echo mysql-server-5.5 mysql-server/root_password password yourpass | debconf-set-selections && \
echo mysql-server-5.5 mysql-server/root_password_again password yourpass | debconf-set-selections && \
apt-get update && \
apt-get install -y \
apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-bin apache2.2-common libapache2-mod-php5 \
openssl php-pear php5 php5-cli php5-common php5-curl php5-gd php5-mcrypt php5-mysql php5-memcache php5-readline \
subversion ssl-cert nano wget unzip \
mysql-server-5.5 mysql-client mysql-client-5.5 mysql-common && \
/etc/init.d/mysql start && \
mysql -u root -pyourpass -e "create database mydb;" && \
rm -rf /var/lib/apt/lists/* && \
rm /etc/apache2/sites-enabled/000-default && \
mkdir -p /var/www/html && \
chown www-data:www-data -R /var/www/html/
COPY conf/etc/ /etc/
COPY mydump.sql /var/www/html/mydump.sql
RUN /etc/init.d/mysql start && \
mysql -u root -pyourpass -h localhost mydb < /var/www/html/mydump.sql && \
rm /var/www/html/mydump.sql
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2", "/var/lib/mysql"]
EXPOSE 80 443 3306
Your way of starting either Apache or Mysql looks wrong to me
If I look at the most popular Apache on hub.docker.com the Dockerfile shows how to start Apache. The last line of the Dockerfile is
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
For the reference Mysql, the last line of the Dockerfile is
CMD ["mysqld"]
So you can look at supervisor or any other similar tool like S6 or daemontools in order to start both Apache and Mysql in the Docker way.
A model often seen is to include a script (bash, shell, etc) in your Docker image, and then use that script as the entrypoint for your application. See that described in https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#entrypoint
So, put the things you're starting in a docker-entrypoint.sh script, COPY the script in, and reference it from the ENTRYPOINT.

How to configure PHP 7 - Apache with MySQL PDO driver in Debian Docker image?

I'm using official docker images for PHP 7 (7.0.3-Apache) and MySql (5.7.10).
Using docker-compose, created containers from images and linked both.
Copied php.ini from https://github.com/php/php-src/blob/php-7.0.3/php.ini-production, replaced dll extensions with so and placed that file in /usr/local/etc/php and enabled _pdo_mysql_ extension in it.
extension=php_pdo_mysql.so
phpinfo shows php.ini loaded but not pdo_mysql extension because it's not installed.
I googled and tried different extension names with apt-get install:
php-mysql, php7-mysql, php7.0-mysql, php7.0.3-mysql.
None of them works. Error says: E: Unable to locate package.
With php5-mysql, it's get installed but after restarting apache with command: docker kill --signal="USR1" <container-name>, extension doesn't show loaded in php.ini.
(Don't think it's much related to docker but I'm new to docker and trying with that now, so mentioning it here.)
Can anyone help to configure pdo_mysql extension with php7-Apache?
You need the Dotdeb repository in /etc/apt/sources.list on your docker image:
FROM php:7-apache
# Install pdo_mysql
RUN apt-get update \
&& echo 'deb http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list \
&& echo 'deb-src http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list \
&& apt-get install -y wget \
&& wget https://www.dotdeb.org/dotdeb.gpg \
&& apt-key add dotdeb.gpg \
&& apt-get update \
&& apt-get install -y php7.0-mysql \
&& docker-php-ext-install pdo_mysql