Search code examples
dockerboot2dockermysql

Docker: How does this (not) work?


I am trying to create a docker image which runs an sql server. For this purpose I did the following steps on a Windows machine.

  1. Run a mysql container to copy an init file into the virtual machine (not sure whether this step is actually necessary):

    docker run -i centurylink/mysql /bin/bash -c 'cat > table.sql' < /c/Users/bastian/Documents/docker/table.sql
    
  2. Create an image from the resulting container:

    docker commit ID mysql/with-table
    
  3. Run the image in order to initialize the database:

    docker run -d -it -p 3306:3306 -v /c/Users/bastian/Documents/docker/mysql:/mysql -e MYSQL_DATABASE='google' -e MYSQL_USER='foo' -e MYSQL_PASSWORD='bar' MYSQL_ROOT_PASSWORD='xxx' mysql/with-table
    

Now I have the follwing questions:

  1. When I execute docker inspect ID3 the output contains the command:

    "Cmd": [
         "/bin/bash",
         "-c",
         "cat \u003e table.sql"
     ]
    

    Does this mean that the copy command is executed and not the mysql server? How do I see which command will actually be executed?

  2. If I try to connect to the mysql server on the host machine:

    $ boot2docker ip 2> /dev/null
    192.168.59.103
    
    $ mysql -h 192.168.59.103 -p 3306
    Enter password: ***                        // I enter xxx here
    

    I get the following error message:

    ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.59.103' (10061)
    

    Any idea what the reason might be?

EDIT: So, I just removed the first steps and instead ran the centurylink/mysql image directly. If I do this and change the mysql line to $ mysql -h 192.168.59.103 -p -u root (or -u foo) the connection works. Thus, the mysql line is erroneous and the mysql server is actually not started correctly when steps 1 and 2 are performed. Do you know the reason for this?


Solution

  • When you run a container with docker run some_image some_command, you've given it a command to run. In your case (step 1) this command was /bin/bash -c 'cat > table.sql'. Then in step 2 you committed that command to a new image. So your image runs that command instead of mysql.

    Maybe look at docker exec to do the futzing around to initialize. Or mount the data as a 'Docker Volume'.