2

Given the following Dockerfile:

From python:2.7-wheezy
RUN apt-get update && apt-get upgrade --assume-yes
RUN apt-get install mysql-server --assume-yes
ENTRYPOINT service mysql start

When I run the docker, it exits immediately after starting mysql server:

Jamess-iMac:docker-python-test jameslin$ docker run -i -t 9618f71f65e4 /bin/bash
[ ok ] Starting MySQL database server: mysqld ..
[info] Checking for tables which need an upgrade, are corrupt or were 
not closed cleanly..
Jamess-iMac:docker-python-test jameslin$ 

How do I make it automatically start up mysql but stays in interactive mode?

James Lin
  • 25,028
  • 36
  • 133
  • 233

2 Answers2

4

ubuntu 12.04 into docker “service mysql start”

If you read above thread you can see that Docker doesn't have any runlevel so mysql doesn't know when it should start.

You can run two container and create a nework between them. One for mysql and another for pythonapp. Here is how you can create a network between two container

docker network create <network_name>

Start the container attaching the container to new network using --net=<network_name>

docker run -d --net=anetwork --name=mysql -e MYSQL_USER=ROOT -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql

docker run --net=anetwork --name=pythonapp -it python:2.7-wheezy bash

enter image description here

I think you are confused between ENTRYPOINT and CMD. The key point to understand is that an ENTRYPOINT will always be run when the image is started, even if a command is supplied to the docker run invocation. If you try to supply a command, it will add that as an argument to the ENTRYPOINT, replacing the default defined in the CMD instruction. You can only override the ENTRYPOINT if you explicitly pass in an --entrypoint flag to the docker run command.

This means that running the image with a /bin/bash command will not give you a shell; rather it will supply /bin/bash as an argument to the service mysql start.

Network between container ubuntu 12.04 into docker “service mysql start”

Difference Between CMD and ENTRYPOINT in Dockerfile

Community
  • 1
  • 1
A0__oN
  • 8,740
  • 6
  • 40
  • 61
3

Your ENTRYPOINT / CMD directive needs to be a long running command. service mysql start is not a continuous command as it runs, in Ubuntu, the service itself and then the command exits.

To make this simple - if you're just trying to run a mysql container you can run docker run mysql. If you absolutely need to run a one-off mysql container you can piggy-back off of the way that the default mysql container starts here: MySql Dockerfile - CMD ["mysqld"] - which should be similar to what you see as the actual start command in /etc/init.d/mysql

Dan Hoerst
  • 6,222
  • 2
  • 38
  • 51
  • 1
    but why is it ignoring it's an interactive mode? – James Lin Oct 05 '16 at 01:51
  • 1
    Because you are running off of an entrypoint command that runs and stops. – Dan Hoerst Oct 05 '16 at 01:53
  • so are you saying entrypoint doesn't work well with interactive mode? – James Lin Oct 05 '16 at 01:54
  • 2
    I am not. Check out this answer: http://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile. In a poor attempt to summarize, a `CMD` or `ENTRYPOINT` command will run that command and exit. So if your entrypoint is `ENTRYPOINT ls -l /usr/bin` and you run an interactive container you will get an output of your `/usr/bin` folder and a hard exit. For the same reason, you are getting a hard exit from `service mysql start` - this command runs and exits while Mysql runs in the background. Docker `CMD` and `ENTRYPOINT` commands need to be persistent. – Dan Hoerst Oct 05 '16 at 01:58
  • `mysqld`, on the other hand, runs the service in the foreground and persists, so it never exists until failure, thus keeping your container alive. – Dan Hoerst Oct 05 '16 at 01:59