0

I'm trying to build a sql docker container from ubuntu base. I build the image and run it. And am able to confirm that it's running, however it dies right away. How would I keep it alive?

Docker File

FROM ubuntu

RUN apt-get update && \
    apt-get install -y mysql-server
COPY run.sh /sbin/run.sh
COPY createDBTable.sql /
RUN chmod 755 /sbin/run.sh
CMD ["./sbin/run.sh"]

run.sh

#!/bin/sh
service mysql start
cat createDBTable.sql | mysql -u root
echo "show databases" | mysql -u root

Output of docker run

 * Starting MySQL database server mysqld
   ...done.
Database
information_schema
<new table>
mysql
performance_schema
sys
LeftN
  • 37
  • 1
  • 5
  • 1
    why not just use the MySQL image? -- Also, docker containers only run so long as the command in the CMD section doesn't return an exit code. your bash script would need to check for that (similar story with using something like `apachectl -D FOREGROUND` for an apache container. – Jhecht Sep 11 '18 at 19:13
  • Possible duplicate of [Why docker container exits immediately](https://stackoverflow.com/questions/28212380/why-docker-container-exits-immediately) – David Maze Sep 11 '18 at 20:02

2 Answers2

1

I created a docker container using the dockerfile mentioned above. Instead of running it in background using -d flag I used -it to start it in interactive mode. After some time the container stopped with the following error-

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

You can try out various approaches to get rid of the above error using Google.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • not getting that. Its running, my shell script is even creating a database and adding tables – LeftN Sep 11 '18 at 22:18
  • I also got the database created and I was able to create tables. But after some time the container stopped and I can see the mentioned error in logs. – Yug Singh Sep 12 '18 at 04:55
0

If there is something wrong with the container, you cannot keep it alive. What I would recommend will be to run the following:

docker logs mssql_container_name


replace "mssql_container_name" with the name of the container. If you didn't get the name, type: docker ps -a and get the name there. If you want you can post the answer to the docker logs here and maybe we can help you. But this is how you find out what happened to a container.

Jensen
  • 199
  • 2
  • 11
  • docker@docker:~/mysql$ docker logs unruffled_goodall * Starting MySQL database server mysqld No directory, logging in with HOME=/ [ OK ] Database information_schema mysql performance_schema sys – LeftN Sep 11 '18 at 21:11
  • So this possible related to a MySQL issue within the container. If MySQL fails to load the service, or in this case, if the script pipes any sort of error, the container will fail. I would recommend using the [mysql](https://hub.docker.com/_/mysql/) image. Hope this helps – Jensen Sep 12 '18 at 00:06