How to install mysql-server in a Dockerfile? - mysql

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.

Related

Create a Dockerfile with ubuntu image and run mysql in it

I am trying to set up a Dockerfile with an ubuntu image that runs a mysql server (I know I should use the mysql image instead, but I need to do it this way for an assignment). I am having a lot of trouble designing the Dockerfile. My Dockerfile looks like this right now:
FROM ubuntu:latest
EXPOSE 3306
VOLUME [ "/var/lib/mysql" ]
RUN apt update && \
apt install -y mysql-server && \
apt-get install -y gosu
ENV MYSQL_ROOT_PASSWORD=secret
COPY ./wordpress.sql /docker-entrypoint-initdb.d/
COPY ./docker-entrypoint.sh /usr/local/bin/
COPY ./entrypoint.sh /
RUN chmod +x entrypoint.sh && \
chmod +x /docker-entrypoint-initdb.d/wordpress.sql && \
chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD [ "mysqld" ]
(I am running docker build -t sql . to build the image, and docker run --name sql_cont -v sql_vol:/var/lib/mysql sql_img to run the container)
I have a folder where I have the docker-entrypoint.sh file and the entrypoint.sh file, because when I install mysql-server, these files are not created by default as If I was just using the mysql docker image.
I just need a Dockerfile from an ubuntu image that runs mysql and just stays waiting ready for connections, so, If anybody know how to refactor this Dockerfile or just give me the answer I'd much appreciate it.
If I got What is the difference between CMD and ENTRYPOINT in a Dockerfile? right, the argument in CMD will be passed to the ENTRYPOINT. Don’t know about your ENTRYPOINT script, but I expect it to just ignore parameters.
I would suggest to:
not replace the default ENTRYPOINT (defaults to /bin/sh -c)
start the docker-entrypoint.sh from CMD
Append mysqld to docker-ENTRYPOINT.sh

[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"

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.

MariaDB never starts within docker image

I'm creating a docker image with ubuntu trusty and MariaDB 5.5 but I can never get MariaDB to start unless I actually connect to the running VM.
Dockerfile:
FROM ubuntu:trusty
# Upgrade packages
RUN apt-get update && apt-get upgrade -y
# So we can add a repo to apt
RUN apt-get install -y software-properties-common
# Add MariaDB repo to aptitude
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
RUN add-apt-repository 'deb http://mirror.jmu.edu/pub/mariadb/repo/5.5/ubuntu trusty main'
RUN apt-get update
# Install MariaDB
RUN DEBIAN_FRONTEND=noninteractive debconf-set-selections << 'mariadb-server-5.5 mysql-server/root_password password PASS'
RUN DEBIAN_FRONTEND=noninteractive debconf-set-selections << 'mariadb-server-5.5 mysql-server/root_password_again password PASS'
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mariadb-server
# Start MariaDB
RUN service mysql start
# Configure MariaDB User permissions
RUN echo "CREATE USER 'ubuntu'#'localhost'" | mysql -u root
The command I'm running to create:
docker build -t ebth-com-trusty --file `pwd`/Dockerfile `pwd` --no-cache
The create command will always fail due to:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
But if I comment out the CREATE USER command, and then connect to the VM, I can connect to MariaDB just fine after running a manual service mysql start.
It is as if the RUN service mysql start just doesn't work, and I'm not sure how to debug this any further.
Every command in a Dockerfile is run in it's own container.
You can think of the process like:
new container is spun up using the previous image
the command is executed
the container is spun down
an image of that container is taken for the next command to run on
This means that the command RUN service mysql start will spin up a new container, start MariaDB, then shut down the container including shutting down MariaDB.
Instead try using CMD and ENTRYPOINT. You can think of them as setting the default executable, command and parameters executed once the container is spun up. However, the difference between the two is a bit more neuanced. Check out the docs: CMD and ENTRYPOINT
It would look something like this:
# Create default user
RUN service mysql start && \
echo "CREATE USER 'ubuntu'#'localhost'" | mysql -u root
# Start MariaDB
ENTRYPOINT ["/bin/bash"]
CMD ["service", "mysql", "start"]

Why SSH connection to docker container is not working?

So i have this Dockerfile:
FROM debian:squeeze
MAINTAINER Name < email : >
# Update the repository sources list
RUN apt-get update
# Install apache, PHP, and supplimentary programs. curl and lynx-cur are for debugging the container.
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 build-essential php5 mysql-server openssh-server libapache2-mod-php5 php5-mysql php5-gd php-pear php-apc php5-curl curl lynx-cur
# Enable apache mods.
RUN a2enmod php5
RUN a2enmod rewrite
# Manually set up the apache environment variables
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
EXPOSE 80
# Copy site into place.
ADD www /var/www/site
# Update the default apache site with the config we created.
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
# start mysqld and apache
EXPOSE 3306
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
CMD bash -c ' (mysqld &); /usr/sbin/apache2ctl -D FOREGROUND;/usr/sbin/sshd -D'
it builds up, no problem,MySQL and Apache start and work fine but the ssh won't work and i don't know why. openssh-server is installed.
i tried starting it up like this:
#startup.sh file
#/bin/bash
sshd
+
ADD ./startup.sh /opt/startup.sh
ENTRYPOINT ["/opt/startup.sh"]
and many other,i'm stuck.
What am i doing wrong?
you are starting apache in the foreground, hence the apachectl process will never give back the hand to the shell that started it and thus the /usr/sbin/sshd -D will never be called (unless you kill apache).
The following instruction will start both mysql and apache in the background and then sshd in the foreground:
CMD bash -c ' (mysqld &); /usr/sbin/apache2ctl start;/usr/sbin/sshd -D'
While such a CMD statement is ok for tests I would advise using a different approach for running multiple processes in a single docker container:
supervisor
phusion/baseimage
Replace below lines of code in the docker file,
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
Using these codes
RUN apt-get install -y openssh-server
RUN echo 'root:password' |chpasswd
RUN mkdir -p /var/run/sshd
this works for me.
Note: Use ssh only for debugging purpose, it is not a good practice at all.