-
Notifications
You must be signed in to change notification settings - Fork 7
Part 2 Multi Container Apps
sudo mkdir -p /data/mysql
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.
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.
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.
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).
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
https://github.com/cpswan/sinatra-ToDoMVC/blob/docker/app.rb
sudo docker exec -it $(sudo docker ps | grep ssl | cut -c1-12) bash
tail /etc/nginx/nginx.conf
The app server IP is statically linked into the Nginx config (because environment variables can't be used there).
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.
https://github.com/cpswan/dockerToDoMVC/blob/master/NginxSSL/start_nginx.sh
sudo iptables -t nat -L -n
Port mappings for the 3 tier app can now be seen.
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.
sudo apt-get install -y python-pip
sudo pip install -U docker-compose
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
sudo service docker restart
This needs to be done as the hand launched 3 tier app will be using ports 443, 3306 and 4567.
sudo docker-compose up &
The same database state as before will be visible when browsing to the app on https://public_ip
sudo docker ps