Month: October 2016

Home / Month: October 2016

Docker Lab – Docker, Apache, MySQL, PHP (DAMP)

October 18, 2016 | Labs | No Comments

This lab might be used for my upcoming workshop.

The goal of this lab is to setup “LAMP” environment on top of docker ecosystem. Technically speaking we will use simple containers on single docker host. Although the title is about DAMP (Docker, Apache, MySQL, PHP) in practice we will spawn two containers, which are:

  1. MySQL Container – where data reside.
  2. WordPress Container – where actual wordpress application run

I do hope you have know basic docker before proceeding.

Step

Planning

Our lab is simple wordpress with MySQL backend. Both MySQL and WordPress will use latest version. Nothing special here.

We don’t want to use default password therefore we will override them.

Creating Lab Network

The reason we create new network is for isolation. This is optional, we can use default network provided by docker. But the goal for new lab network is to give every container in lab ability to communicate with each other without affecting other lab (and containers there).

docker network create --driver bridge lab1-damp

This network lab should reside on same Docker host. If we are using swarm, consider using overlay network.

Generate Password

We need two strong password. One for MySQL root password. Another one for WordPress user. This two password will be passed to containers through environment variable. You can generate any string as password and export them in the end.

export ROOT_PASSWORD=<my super secure mysql root password>
export WORDPRESS_PASSWORD=<my super secure wordpress password>

Make sure you replace the password with your own.

Spawn MySQL Container

We will override four configuration. They are exposed as “environment variable” by docker which we can change.

docker run -d --name mysql \
--env MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD} \
--env MYSQL_USER=wordpress \
--env MYSQL_PASSWORD=${WORDPRESS_PASSWORD}\
--env MYSQL_DATABASE=wordpress \
--net lab1-damp \
mysql

The configurations that we override is self explaining.

We are detaching the container so the container would run in “background” when we can do something else.

Verify that the container is running

docker ps

Spawn WordPress Container

WordPress is PHP application, that way WP is running on top of web server and PHP interpreter. Technically speaking our container would have Apache HTTPD and PHP installed so we only concern about our WordPress.

docker run -d -p 80:80 --name wordpress \
--env WORDPRESS_DB_HOST=mysql:3306 \
--env WORDPRESS_DB_USER=wordpress \
--env WORDPRESS_DB_PASSWORD=${WORDPRESS_PASSWORD} \
--env WORDPRESS_DB_DATABASE=wordpress \
--net lab1-damp \
wordpress

We expose this container’s port 80 and map it to our port 80. As we know WordPress need MySQL instance so we provide pointer to our container with WORDPRESS_DB_HOST environment.

To verify the container is running, again:

docker ps

we can also try to check it.

docker exec -ti wordpress bash

We are now connected to wordpress instance by tty. Specifically, we are running new process which is bash on container. To exit, give command exit.

Configure

Use browser and go to http://localhost and you would see famous WordPress installation page, something like this. Just do your configuration.

screen-shot-2016-10-19-at-01-01-53

Challenge

  1. Inspect the wordpress container. Can you open the page with its IP address instead of localhost?
  2. Stop mysql, will wordpress run normally? Try starting mysql to confirm it.
  3. Remove mysql and create new mysql container without MYSQL_PASSWORD set. Restart wordpress. Will it successfully run? Try omit other.
  4. Exec bash on wordpress. Can you find the www directory? Try creating a file to confirm it.
  5. Exec bash on mysql. Can you find the data directory?

Social media & sharing icons powered by UltimatelySocial