Skip to content

Part 2 Multi Container Apps

Chris Swan edited this page May 13, 2015 · 6 revisions

Launch a 3 tier app with links

Create the directory for persistent data

sudo mkdir -p /data/mysql

Start the database

sudo docker run -d -p 3306:3306 --name todomvc_db \
-v /data/mysql:/var/lib/mysql cpswan/todomvc.mysql

This container will bind mount a volume into the persistent data directory created on the host.

Start the app server

sudo docker run -d -p 4567:4567 --name todomvc_app \
 --link todomvc_db:db cpswan/todomvc.sinatra

The app server container links back to the database container.

Start the web server

sudo docker run -d -p 443:443 --name todomvc_ssl \
 --link todomvc_app:app cpswan/todomvc.ssl

The web server container links back to the app server container.

The ToDo list app will now be working at https://public_ip, though the HTTPS protocol will need to be added to the AWS security group to allow access.

Look at how the links work

Get a shell in the app container

sudo docker exec -it $(sudo docker ps | grep sinatra | cut -c1-12) bash

The subshell sudo docker ps | grep sinatra | cut -c1-12 is being used to extract the ID of the app server container. It's taking the list of running containers, filtering for sinatra in the base image name, then cutting out the first 12 characters (which holds the container ID).

Take a look at the app using ENV variable

head /opt/sinatra-ToDoMVC-docker/app.rb
exit

The key line is dburl = 'mysql://root:pa55Word@' + ENV['DB_PORT_3306_TCP_ADDR'] + '/todomvc' and the most important piece of that is ENV['DB_PORT_3306_TCP_ADDR'] where the IP of the database container is parsed from an environment variable populated by the linking process.

exit is needed to return from the container shell to the host

Source is at

https://github.com/cpswan/sinatra-ToDoMVC/blob/docker/app.rb

Look at how the links work pt.2

Get a shell in the ssl container

sudo docker exec -it $(sudo docker ps | grep ssl | cut -c1-12) bash

ENV variable has been hard coded into config

tail /etc/nginx/nginx.conf

The app server IP is statically linked into the Nginx config (because environment variables can't be used there).

The launch script uses a template to fetch ENV vars

cat /etc/nginx/upstream.template
exit

At startup $APP_PORT_4567_TCP_ADDR:$APP_PORT_4567_TCP_PORT gets turned into the static IP (and port) within the config file.

Source is at

https://github.com/cpswan/dockerToDoMVC/blob/master/NginxSSL/start_nginx.sh

Another quick look at iptables

Look at NAT rules

sudo iptables -t nat -L -n

Port mappings for the 3 tier app can now be seen.

Look at DOCKER chain

sudo iptables -L

Rules to allow the web server to connect to the app server, and the app server to connect to the database have been put in place.

Docker Compose

Install Docker Compose

sudo apt-get install -y python-pip
sudo pip install -U docker-compose

Download and view example file

wget http://is.gd/onugdc -O docker-compose.yml
cat docker-compose.yml

Full URL for is.gd/onugdc is https://raw.githubusercontent.com/cpswan/container-networking-tutorial/master/examples/docker-compose.yml

Bring up the demo app again

Restart Docker to clear out containers

sudo service docker restart

This needs to be done as the hand launched 3 tier app will be using ports 443, 3306 and 4567.

Invoke Docker compose (in background)

sudo docker-compose up &

The same database state as before will be visible when browsing to the app on https://public_ip

List Docker processes

sudo docker ps

Clone this wiki locally