MariaDB never starts within docker image - mysql

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

Related

How to set up root password while installing mysql in docker?

I am trying to install mysql in docker and setting default password of root as root using following Dockerfile.
Dockerfile:
FROM ubuntu:20.04
COPY mysql_setup.sh /software/mysql_setup.sh
RUN sh /software/mysql_setup.sh
CMD ["/bin/sh"]
Where /software/mysql_setup.sh
# Download and Install the Latest Updates for the OS
apt-get update && apt-get upgrade -y
# Set the Server Timezone to CST
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Enable Ubuntu Firewall and allow SSH & MySQL Ports
ufw enable
ufw allow 22
ufw allow 3306
# Install essential packages
apt-get -y install zsh htop
# Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
echo "mysql-server mysql-server-8.0.26/root_password password root" | sudo debconf-set-selections
echo "mysql-server mysql-server-8.0.26/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server
# Run the MySQL Secure Installation wizard
mysql_secure_installation
sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
mysql -uroot -p -e 'USE mysql; UPDATE `user` SET `Host`="%" WHERE `User`="root" AND `Host`="localhost"; DELETE FROM `user` WHERE `Host` != "%" AND `User`="root"; FLUSH PRIVILEGES;'
service mysql restart
But above code does not seem to be working as when i enter into docker and use and use root as password it gives an error.
root#839f2946bfdf:/# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
How can i fix this error?
You cannot run a mysql install inside a container in a normal way. Thing like ufw cannot be done. The entire RUN step is at build time only, so service mysql restart won't be running when the container is started, only CMD is.
Recommend just using the Docker Library mysql container. There's a standard MYSQL_ROOT_PASSWORD variable for setting the root password.

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.

Error : The command '/bin/sh returned a non-zero code: 1

When I am trying to build one of my projects by running a script written by previous team in my ubuntu 16.04
sudo ./build
I am getting error :
Step 8/24 : RUN service mysql start
---> Running in 3djjk653642d
* Starting MySQL database server mysqld
...fail!
The command '/bin/sh -c service mysql start' returned a non-zero code: 1
My Dockerfile looks like:
COPY schema.sql /tmp/schema.sql
### User with ALL accesses (winter/toor)
RUN service mysql start
RUN mysql < /tmp/schema.sql
RUN mysql -e "CREATE USER 'winter'#'%' IDENTIFIED BY 'toor'"
RUN service mysql start && mysql -e "GRANT ALL ON its.* TO 'winter'#'%'"
Please ,any help ?
RUN statements in a Dockerfile are used to run a command which will have some effect on the filesystem, that is then saved in another layer.
It's not normal to start a service like this, as the state of the memory (where the service is running) is not stored in the image, it can only be running in a running container.
The normal way to do stuff like this would be to write a bash script, (called start.sh, or something similar), copy it into the image and then run from an ENTRYPOINT / CMD line at the end of the Dockerfile. This will be run when the container is created in a docker run ... command
start.sh:
service mysql start
mysql < /tmp/schema.sql
mysql -e "CREATE USER 'winter'#'%' IDENTIFIED BY 'toor'"
service mysql start && mysql -e "GRANT ALL ON its.* TO 'winter'#'%'"
Dockerfile:
COPY schema.sql /tmp/schema.sql
COPY start.sh /
ENTRYPOINT ["/start.sh"]
Have a read here for some information on the difference between ENTRYPOINT & CMD and when each should be used.
Better still - use the official MySQL image from Docker hub. Through the use of environment variables, you could probably achieve all you require.
For me the error was:
yum -y install nginx' returned a non-zero code: 1
This docker file helped me:
FROM centos:7
MAINTAINER linuxtechlab
LABEL Remarks="This is a dockerfile example for Centos system"
RUN yum -y update
RUN yum -y install httpd
RUN yum clean all
RUN yum -y install nginx
EXPOSE 80
#ENV HOME /root
#WORKDIR /root
#ENTRYPOINT ["ping"]
#CMD ["google.com"]

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