3

I am trying to automate the installation and running of set of linked docker containers using fig. The configuration is composed of a container running RStudio linked to a container running MySQL, such that I can query the MySQL database from RStudio.

On first run, I would like to create the MySQL container from the base MySQL image, and populate it with a user and database. From the command line, something like this:

#Get the latest database file
wget -P /tmp http://ergast.com/downloads/f1db.sql.gz && gunzip -f /tmp/f1db.sql.gz

#Create the database container with user, password and database
docker run --name ergastdb -e MYSQL_USER=ergast -e MYSQL_ROOT_PASSWORD=mrd -e MYSQL_DATABASE=f1db -d mysql

#Populate the database
docker run -it --link=ergastdb:mysql -v /tmp:/tmp/import --rm mysql sh -c 'exec mysql -h$MYSQL_PORT_3306_TCP_ADDR -P$MYSQL_PORT_3306_TCP_PORT -uergast -pmrd f1db < /tmp/import/f1db.sql'

#Fire up RStudio and link to the MySQL db
docker run --name f1djd -p 8788:8787 --link ergastdb:db -d rocker/hadleyverse

If I could get hold of a database image with the data preloaded, I guess that something like the following fig.yml script could link the elements?

gdrive:
    command: echo created
    image: busybox
    volumes:
        - "~/Google Drive/shareddata:/gdrive"

dbdata:
    image: mysql_preloaded
    environment:
        MYSQL_USER=ergast
        MYSQL_ROOT_PASSWORD=mrd
        MYSQL_DATABASE=f1db 

rstudio:
    image: rocker/hadleyverse
    links:
        - dbdata:db
    ports:
        - "8788:8787"
    volumes_from:
        - gdrive

My question is, can I use a one-shot fig step to create the dbdata container, then perhaps mount a persistent volume, link to it and initialise the database, presumably as part of an initial fig up. If I then start and stop containers, I don't want to run the db initialisation step again, just link to the data volume container that contains the data I previously installed.

I also notice that the MySQL docker image looks like it will support arbitrary datadir definitions (Update entrypoints to read DATADIR from the MySQL configuration directly instead of assuming /var/lib/docker). As I understand it, the current definition of the MySQL image prevents mounting (and hence persisting) the database contents within the database container. I guess this might make it possible to create a mysql_preloaded image, but I don't think the latest version of the MySQL docker script has been pushed to dockerhub just yet and I can't quite think my way to how fig might then be able to make use of this alternative pathway?

psychemedia
  • 5,690
  • 7
  • 52
  • 84

2 Answers2

0

Some options:

  1. Edit the fig.yml to run a custom command that is different than the default image command/entrypoint.

    From http://www.fig.sh/yml.html (example): command: bundle exec thin -p 3000

  2. Start the container locally, modify it and then commit it as a new image.

  3. Modify the MySQL image docker-entrypoint.sh file to do your custom initialization.

    https://github.com/docker-library/mysql/blob/567028d4e177238c58760bcd69a8766a8f026e2a/5.7/docker-entrypoint.sh

Community
  • 1
  • 1
ryan1234
  • 7,237
  • 6
  • 25
  • 36
  • 2
    but why customize the db container with docker-entrypoint.sh ? Isnt psychemedia's attempt to keep the db container non-unique and initialized the db at run or in docker compose/fig better practice? or is 'command:' incompatible with 'exec mysql...'? – Tom Jun 01 '15 at 19:31
0

Couldn't you just roll your own version of the MySQL docker image? The official one from MySQL "upstream" is available at https://github.com/mysql/mysql-docker/blob/mysql-server/5.7/Dockerfile What if you simply make your own copy of that, remove the VOLUME line (line 11) and then you can

docker build -t my_mysql .
docker run -d --name=empty_db my_mysql ...
# add data to the database running in the container
docker commit empty_db primed_db
docker rm -v empty_db

docker run -d --name=instance1 primed_db
docker run -d --name=instance2 primed_db

which should leave you with two running "identical" but fully isolated instances.

Fredrik Wendt
  • 988
  • 1
  • 10
  • 17