< BACK

Creating a LEMP with Docker and the Data-Only Container Pattern

 

Create a data container

[code]$ docker run -v /var/data --name APPDATA busybox true[/code]

This is tricky but here's what's happening:

  1. We're using the Data-Only Container Pattern to create a "hidden" directory where to save the data
  2. We create a new volume "/var/data" inside the container with name "APPDATA" which is using  the "busybox" image and we run the "true" command so it finishes automatically.
  3. The name busybox IS actually an docker image which exists in the docker.io repository, it contains utilities.
  4. We run the true command becuase it's not needed that this image is running. It just needs to exists, so running true will create it but it will not be running.
  5. It's not neccesary to run the image with -d unless you want to prevent this container from being removed (it will be removed anyway if you do docker rm -f"
  6. The name APPDATA allows us to identify it easily as a data container.
  7. The /var/www directory will be created INSIDE the container and it WILL NOT be linked to the /var/www on your host, they are different directories, period, do not confuse them.
  8. The /var/data will be available
  9. If you do docker inspect APPDATA you'll get something like:

    [code]..."Volumes": {"/var/www": "/var/lib/docker/vfs/dir/ed5ec43a7237f2ff51159e900a336f61470b261f38b83d5f04def259f492a231"}[/code]

    You can see that /var/www inside the container is actually mapped to "/var/lib/docker/vfs/dir/ed5ec43a7237f2ff51159e900a336f61470b261f38b83d5f04def259f492a231" in your host machine, if you go to that directory (as root) you're accessing the same folder that your container is accessing, but it's not recommended to do it.
  10. We only created one volume with the path /var/www but we can create as many as we want, BUT since creating volumes is inexpensive, you probably want to create one volume for each container to keep things organized.

 

If you want to know more about this, read: http://docs.docker.io/use/working_with_volumes/

Create the MariaDB container

Before we do this, we need to create another data volume container to save the MariaDB data.

[code]$ docker run -v /var/lib/mysql --name DBDATA busybox true[/code]

I'm using the dockerfile/mariadb and if you inspect the Dockerfile of that image, you'll see that it will save the data in /data so it needs a volume in /data.

Now we can run the MariaDB container:

[code]$ docker run -it --rm -p 3306:3306 --volumes-from DBDATA dockerfile/mariadb[/code]

The --rm is something we haven't used before, it just means that the container will be removed if it exists, which is something we want, if MariaDB fails, we need to kill it and start another one, Docker will take care of the removal of this container. Launching a new container is something will do in another tutorial.

Database

[code]docker pull orchardup/mysql[/code]

[code]docker run -d -e "MYSQL_ROOT_PASSWORD=123" -e "MYSQL_DATABASE=drupal" --volumes-from DBDATA --name mysql orchardup/mysql[/code]


Create the php5-nginx container

[code]$ docker run -it --volumes-from APPDATA -v /var/www/siaeducacion:/var/www -p 8000:80 --name lemp luis/php5-nginx[/code]

Here we use whatever volume we created in the APPDATA container and also map /var/www/siaeducacion in the host to /var/www inside the container.

[code]HOME=/
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=0035b1fb8e54
TERM=xterm
MYSQL_PORT=tcp://172.17.0.4:3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.4:3306
MYSQL_PORT_3306_TCP_ADDR=172.17.0.4
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_NAME=/loving_fermi/mysql
MYSQL_ENV_MYSQL_ROOT_PASSWORD=123
MYSQL_ENV_MYSQL_DATABASE=drupal
DEBIAN_FRONTEND=noninteractive[code]

 


Share this: