Install mysql in dockerfile?
The problem is that you've never started the database - you need to explicitly start services in most Docker images. But if you want to run two processes in Docker (the DB and your python program), things get a little more complex. You either have to use a process manager like supervisor, or be a bit cleverer in your start-up script.
To see what I mean, create the following script, and call it cmd.sh
:
#!/bin/bashmysqld &python main.py
Add it to the Dockerfile:
FROM ubuntu:saucy# Install required packagesRUN apt-get updateRUN DEBIAN_FRONTEND=noninteractive apt-get -y install pythonRUN DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server python-mysqldb# Add our python app code to the imageRUN mkdir -p /appADD . /appWORKDIR /app# Set the default command to executeCOPY cmd.sh /cmd.shRUN chmod +x /cmd.shCMD cmd.sh
Now build and try again. (Apologies if this doesn't work, it's off the top of my head and I haven't tested it).
Note that this is not a good solution; mysql will not be getting signals proxied to it, so probably won't shutdown properly when the container stops. You could fix this by using a process manager like supervisor, but the easiest and best solution is to use separate containers. You can find stock containers for mysql and also for python, which would save you a lot of trouble. To do this:
- Take the mysql installation stuff out of the Dockerfile
- Change
localhost
in your python code tomysql
or whatever you want to call your MySQL container. - Start a MySQL container with something like
docker run -d --name mysql mysql
- Start your container and link to the mysql container e.g:
docker run myapp --link mysql:mysql