Dockerfile and background running mysql server - mysql

i have problems...
Firstly i have a Dockerfile where i define all the steps, like updating system, installing mysql, change mysql root password.
Then i set an EntryPoint so my container on start will exec mysql server.
I have 2 problems:
- When i start the container, it restarts every 10 seconds.
- When i use exec to enter the docker it says: "No docker with such id".
This is my Dockerfile:
# Set the base image
FROM ubuntu:14.04
MAINTAINER redigaffi
RUN apt-get update \
&& apt-get -y install mysql-server \
&& service mysql start \
&& mysqladmin -u root password FEGj5nmKYRha
ENTRYPOINT service mysql start \
&& bash
#VOLUME /root/mysql:/var/lib/mysql:rw Please run -v running this docker since Dockerfile has not access to host files
EXPOSE 3306
I put bash on the end on the entrypoint because without it container just closes, so it remains in background.
I have tried many commands to execute this container:
docker run -d df0bb600c10f /bin/bash # This one closes the container after 2 seconds
docker run -d --restart=always df0bb600c10f /bin/bash # This one remains, but restarts every 10 seconds and i cant access this docker using exec.
Please help, what is wrong ?
Thank you!

Try using the supervisor. This article here shows the steps.

Related

why dockerfile can't start db with " CMD /etc/init.d/mysql start "?

I downloaded a docker image with mariadb and phpmyadmin,
then wrote two dockerfiles below..
# dockerfile A
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin
CMD /etc/init.d/mysql start && /etc/init.d/apache2 start
# dockerfile B
FROM alfvenjohn/raspbian-buster-mariadb-phpmyadmin
CMD service mysql start && /usr/sbin/apachectl -D FOREGROUND
dockerfile B worked well,
but dockerfile A failed.
I can build image from dockerfileA,
then spin-up container from it docker run -it -p 80:80 <img id> bash
the container up successfully,
but while I inside the container, I found the services of mariadb and apache2 not working.
After I execute /etc/init.d/mysql start && /etc/init.d/apache2 start,
mariadb and apache2 works!
Trying to get error messages by docker logs <container id>, but got nothing.
What my question is
"If I run the docker image without dockerfile just by commands,
like what I did in dockerfile A. The container works well. "
$ docker run -it -p 80:80 alfvenjohn/raspbian-buster-mariadb-phpmyadmin bash
$ /etc/init.d/mysql start && /etc/init.d/apache2 start
Why? Didn't dockerfile A do the same thing, as I spin up my container with commands ?
You need to remove the bash at this end of the command. This replace the command inside your dockerfile.
docker run -d -p 80:80 <img id>
You can use this command to connect inside the container afterward:
docker exec -it <container_id> bash
A Docker image runs a single command only. When that command exits, the container exits too.
This combination usually means a container runs a single server-type process (so run MySQL and Apache in separate containers), and it means the process must run in the foreground (so the lifetime of the container is the lifetime of the server process).
In your setup where you launch an interactive shell instead of the image CMD, you say you can start the servers by running
/etc/init.d/mysql start && /etc/init.d/apache2 start
This is true, and then you get a command prompt back. That means the command completed. If you run this as an image's CMD then "you get a command prompt back" or "the command completed" means "the container will exit".
Generally you want to launch separate database and Web server containers; if you have other application containers you can add those to the mix too. Docker Compose is a common tool for this. You may want to look through Docker's sample applications for some examples, or other SO questions.

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 pre-configure and prefill official MySQL docker container?

I want to build a new MySQL image based on the official MySQL docker container image. I want to reduce the number of parameters I need to add when running the image. (eg. -e MYSQL_USER, -e MYSQL_DATABASE and even -e MYSQL_ROOT_PASSWORD='rootsecret')
that already includes my settings for the global variables and my Create Database SQL file in the docker-entrypoint-initdb.d folder.
How do I add all my settings and create a new image to simply docker run mysql:config1 docker run mysql:config2 and so on?
You could build your own mysql docker image using a docker file, configure username, password and everything else you might need, build that image, upload it to the docker hub and then when you launch a new docker container you just use the previously built container.
An example of a Docker file to build an ubuntu image with a mysql server inside would be something like bellow (save it to a file called Dockerfile):
FROM ubuntu:latest
RUN apt-get update \
&& apt-get install -y apt-utils \
&& { \
echo debconf debconf/frontend select Noninteractive; \
echo mysql-community-server mysql-community-server/data-dir \
select ''; \
echo mysql-community-server mysql-community-server/root-pass \
password 'Desired-Password'; \
echo mysql-community-server mysql-community-server/re-root-pass \
password 'Desired-Password'; \
echo mysql-community-server mysql-community-server/remove-test-db \
select true; \
} | debconf-set-selections \
&& apt-get install -y mysql-server mysql-client
Then build your mysql docker container like this (you have to be in the folder where the Docker file was/is saved):
docker build my-ubuntu-mysql-docker
Then you have to push it to the docker hub and then you can use it to start a new docker container like this:
docker run -d -p 2222:22 -p 3306:3306 --name my-ubuntu-mysql-docker ...
Where 2222 is local ssh port mapped to ssh port 22 of the docker container and 3306 is local mysql port mapped to the mysql port of the docker container.
I hope this helps!
The following has to be written into the Dockerfile:
FROM mysql:latest
LABEL Name=mylabel Version=0.0.1
COPY path/to/sh/sql/sql.gz/files /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD='rootpassword'
As stated in the documentation on the official docker website:
When a container is started for the first time, a new database with
the specified name will be created and initialized with the provided
configuration variables. Furthermore, it will execute files with
extensions .sh, .sql and .sql.gz that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order.
What you you would want to do is to modify Mysql's image entry point
and please note that you do not need to pass all the parameters, most of them are optional

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