Development

Docker Bootcamp – Linking Multiple Containers

Pexels Joey Kyber 119562

Welcome back to Docker Bootcamp.  In this post, we’ll set up multiple containers and link them, so they function together.  For example, you could have one container for a database server and another for a web server hosting a content management system.  Remember, containers are specialized and can only run one piece of software.  Linking containers lets us create a complex software environment.

Commands

  • run – Create and start a new container

Command Details

You can find a full list of commands and all available flags at https://docs.docker.com/engine/reference/commandline/docker/.

run

docker run [options] image [command] [args…]
Outputs: ContainerID

–env-file O Read in a file of environment variables
–link O Add a link to another container by name

 

Examples

Setup

  • View the image details page for MySql database on docker hub
  • View the image details page for WordPress content management system on docker hub
  • Create an environment variables file for MySQL and WordPress
    • env-mysql.txt
      • MYSQL_ROOT_PASSWORD=<yourrootpassword>
      • MYSQL_DATABASE=WordPress
      • MYSQL_USER=wpress
      • MYSQL_PASSWORD=<yourdbuserpassword>
    • env-wordpress.txt
      • WORDPRESS_DB_HOST=<mysqlcontainername>
      • WORDPRESS_DB_USER=wpress
      • WORDPRESS_DB_PASSWORD=<yourdbuserpassword>
      • WORDPRESS_DB_NAME=WordPress

Link containers using the –link flag

  • Create, start and run a detached database container
    • docker run -d –env-file env-mysql.txt –name mysqldb mysql
  • Create, start and run a detached WordPress container
    • docker run -d –env-file env-wordpress.txt -p 8080:80 –name wordpresscms wordpress
      • Browse to localhost:8080
        • Notice the message “Error establishing a database connection.”
  • Stop the container
    • docker stop wordpresscms
  • Create, start and run a detached WordPress container linked to the database container
    • docker run -d –env-file env-wordpress.txt –link mysqldb -p 8081:80 –name wordpresscms2 WordPress
      • Browse to localhost:8081
        • Notice the WordPress installer appears
  • Complete the installation wizard
  • View the site
    • Browse to localhost:8081
  • Validate the database content
    • docker exec mysqldb mysqldump WordPress -u root -p<yourdbuserpassword> > wordpressdb.sql
      • Notice there is no space between the “-p” flag and the password
      • Here we are using the > to redirect output to a file named wordpressdb.sql
      • Open the file to see the database is populated with tables and data
        • You should see your user in the wp_users table
  • Inspect the MySQL container
    • docker inspect mysqldb
        • In the portbindings and ports sections, notice the ports on the container are not exposed to the host
          • HostConfig: { PortBindings: {} }
          • NetworkSettings: { Ports {3306/tcp: null} }
  • Inspect the wordpresscms2 container
    • docker inspect wordpresscms2
      • In the links section, notice the mysql container is listed
        • HostConfig: { Links: { mysqldb:/wordpresscms2/mysqldb } }
  • Stop the containers
    • docker stop mysqldb wordpresscms2
  • Start the WordPress container while the database container is stopped.
    • docker start wordpresscms2
        • Notice the message “Error response from daemon: Cannot link to a non-running container: /mysqldb AS /wordpresscms2/mysqldb”
  • Stop and remove the containers
    • docker rm -f mysqldb wordpresscms wordpresscms2

When using the –link flag, docker uses its bridge network and other internal networking features to allow the containers to communicate.  As such, any dependent containers must be started and available first.  This is easy in a small example but can easily become complicated in a more complex scenario.  Note that using the –link flag is deprecated and could be removed from a future version of docker (https://docs.docker.com/network/links/).

Link containers using network settings

  • Create, start and run a detached database container
    • docker run -d –env-file env-mysql.txt –name mysqldb mysql
  • Find the IP address of the container
    • docker inspect mysqldb
      • The IP is listed under Networks: { bridge: { IPAddress } }
  • Modify the env-wordpress.txt file
    • Update the database host to point to the ipaddress:port of the mysql container
      • WORDPRESS_DB_HOST=<containerip>:<containerport>
  • Create, start and run a detached WordPress container
    • docker run -d –env-file env-wordpress.txt -p 8082:80 –name wordpresscms wordpress
      • Browse to localhost:8082
        • Notice the WordPress installer appears
  • Stop and remove the containers
    • docker rm -f mysqldb wordpresscms

Remember Part 4 of this series on networking? By default, all containers use the bridged network type and can communicate with each other (if you know the container’s IP).  In future posts, I’ll explore how to make linking containers more dynamic.

About the Author

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories