How to start mysql server in docker container - mysql

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

Related

Unable to connect to host mysql in docker container [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 3 months ago.
I want to connect to host mysql in container, and I have tried some methods but none of them work.
Container: python & mysql client
Host: mysql service
Host mysql setting:
sudo apt install -y mysql-server
apt install -y default-libmysqlclient-dev
# And modify bind address: /etc/mysql/mysql.conf.d/mysqld.cnf
# bind-address = 0.0.0.0
Docker container run command's setting:
RUN apt-get update &&\
apt install -y default-mysql-client &&\
apt-get install -y gcc &&\
apt-get install -y default-libmysqlclient-dev &&\
/usr/local/bin/python -m pip install --upgrade pip &&\
pip install --no-cache-dir SQLAlchemy
My python file in container:
from sqlalchemy import create_engine
from sqlalchemy import text
SQLALCHEMY_DATABASE_URI = "mysql://root:pwd#localhost:3306/mysql?charset=utf8mb4"
engine = create_engine(SQLALCHEMY_DATABASE_URI, encoding='utf8', pool_pre_ping=True) # for mysql
with engine.connect() as connection:
result = connection.execute(text("show tables;"))
print(result.all())
Envrionment:
Ubuntu: 20.04
Docker version: 20.10
Mysql version: 8.0
My purpose is to connect to mysql throught sqlalchemy in container, but not even through mysql-client.
Therefore, I try to use mysql-client to connect, but still failed as same reason.
I have tried several method:
allow bind-address
run container with: --add-host host.docker.internal:host-gateway
without --add-host, try IP address
Most of the solution just say add --add-host host.docker.internal:host-gateway and it can work,
But not work for me.
Does anyone solve this problem?
Thanks!
Update:
I have tried a method that works.
run container --network=host
(in container)# mysql -h 127.0.0.1 -u root ...
But I don't want to use --network=host, how can I connect to host mysql with "host.docker.internal"?
Update:
Problem solved.
I run container with --add-host host.docker.internal:host-gateway
connect to db with: mysql -h host.docker.internal
And in mysql, update mysql.user root's host to '%'.
(allow non-localhost connect)
A couple things:
Using mysql without -h or --host by default tries to connect to localhost via a socket file.
Using mysql -h host.docker.internal -u root -p is correct. However, as the error message states, you'll need to provide permission for the root user to connect via non-localhost. If using the mysql container, run with -e MYSQL_ROOT_HOST=% or similar set.
I don't know which command you're using to start the mysql container. Make sure you're publishing the ports with -p 3306:3306 to all for cross-container communication.

How to start mysql and set up the mysql root password for a docker container?

I am trying to host a web server via docker.The web server needs mysql database.But I am failing to change the root password of mysql and also start the mysql and apache2 service.
So can someone help me to set the root password and start start the services.
Here is my try to setup the docker container.
FROM ubuntu:16.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install apache2
RUN apt-get -q -y install mysql-server
CMD service mysql start

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

Dockerfile and background running mysql server

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.

docker to connect with mysql database of host system and dump the sql file into host system and then host a web application

I am new to Docker but i have read quite about it. Now my requirement is:
I will give my client a shell script which he would run on a base ubuntu os on a completely new system. The docker image should use the database of host system. The shell script will do all the prerequisites of installing docker, mysql, etc. and will run a docker image. As the image is not available locally, it will pull from docker repository.
Now my problem is that i dont want to give my client the sql dump file just like that. The dump is included in the image and once the images is run i want the image to connect to the host database and dump the data and then host the webapp.
My docker file is :
FROM ubuntu:14.04
MAINTAINER test_manoj
RUN pip install requirements.txt
ADD . /home/myapp/
RUN sudo apt-get install -y supervisor
WORKDIR /home/myapp/
EXPOSE 8000
EXPOSE 80
cmd ["supervisord", "-c", "/home/myapp/supervisord.conf"]
There are some more apt-get install-s but i didnt find it useful to mention here. So basically i am installing nginx, uwsgi, supervisor.
I have exposed port 8000 for socket uwsgi connections and port 80 for nginx.
My docker run command is :
docker run --detach --net=host -v /var/run/mysqld.sock:/var/run/mysqld/mysqld.sock manoj/mydocker
I am using -v to connect the host mysql to container's mysql.
I have already found a work around for my problem that is running
docker run --rm --detach --net=host -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock manoj/mydocker mysq -uroot -proot db_name < dump.sql
before the main run command. I know this works but is there any other way to do this? And is there any other way i can use host's mysql without providing -v tag?