24

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.

Xiaorong Liao
  • 1,201
  • 14
  • 14
Mark Taylor
  • 361
  • 1
  • 2
  • 6
  • 2
    Did you follow the instructions described on the official [MySQL](https://hub.docker.com/_/mysql/) docker hub page? – Henrik Sachse Aug 10 '15 at 12:03
  • 1
    I am not getting. I installed mysql in the container, then I did CMD sudo service mysql restart, then why its not starting sql server. I followed "http://stackoverflow.com/questions/25135897/how-to-automatically-start-a-service-when-running-a-docker-container" also, still same. – Mark Taylor Aug 10 '15 at 12:11
  • Did you read the Docker User Guide? What ever is started with CMD must be a process that is running continuously, e.g.: "while true; do date; sleep 10; done", or "/usr/sbin/apache2ctl -D FOREGROUND", or "/usr/sbin/mysqld -u mysql". Simply running "service mysqld restart" will just restart the service and then return, so the docker container stops. – mgor Aug 10 '15 at 17:39
  • mgor, I did this, still mysql is not running. – Mark Taylor Aug 10 '15 at 19:01
  • 1
    Share your Dockerfile. – mgor Aug 11 '15 at 07:07

3 Answers3

24

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.

Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
  • I tried this also, It got stuck here 150810 11:43:38 [Note] - '0.0.0.0' resolves to '0.0.0.0'; 150810 11:43:38 [Note] Server socket created on IP: '0.0.0.0'. 150810 11:43:38 [Note] Event Scheduler: Loaded 0 events 150810 11:43:38 [Note] mysqld: ready for connections. Version: '5.5.45' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL) – Mark Taylor Aug 10 '15 at 12:25
  • Did you try to connect to it? It says *ready for connections* – Henrik Sachse Aug 10 '15 at 12:35
  • Why do I create a new container. I have to create an image in which this is required and then after I have to deploy that image. Is this not possible to start sql and create db and other stuffs at the time of creating that image. – Mark Taylor Aug 10 '15 at 12:36
  • Because in the *container instance* you will have the running MySQL daemon process. The image only contains *the files* on disk (like a tar archive). – Henrik Sachse Aug 10 '15 at 12:39
  • Can you please give me your mailId, So that I will chat with you regarding my problem. – Mark Taylor Aug 10 '15 at 12:42
  • 1
    Even though if I did this, then that mysql daemon is running on my local machine, How the deployed image will communicate? – Mark Taylor Aug 10 '15 at 12:52
  • Does the Database always needed to be created? – Benyamin Limanto Jun 15 '19 at 09:17
3

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
Sindhu
  • 2,422
  • 2
  • 19
  • 17
2

docker exec -it container_name/id bash
service mysql status to check on the service status
service mysql start to start the mysql service

Emms Magdy
  • 21
  • 1