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
- https://hub.docker.com/_/mysql
- This page lists how to use the image as well as the available environment variables
- View the image details page for WordPress content management system on docker hub
- https://hub.docker.com/_/wordpress
- This page lists how to use the image as well as the available environment variables
- 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
- env-mysql.txt
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.”
- Browse to localhost:8080
- docker run -d –env-file env-wordpress.txt -p 8080:80 –name wordpresscms wordpress
- 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
- Browse to localhost:8081
- docker run -d –env-file env-wordpress.txt –link mysqldb -p 8081:80 –name wordpresscms2 WordPress
- 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
- docker exec mysqldb mysqldump WordPress -u root -p<yourdbuserpassword> > wordpressdb.sql
- 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} }
- In the portbindings and ports sections, notice the ports on the container are not exposed to the host
-
- docker inspect mysqldb
- Inspect the wordpresscms2 container
- docker inspect wordpresscms2
- In the links section, notice the mysql container is listed
- HostConfig: { Links: { mysqldb:/wordpresscms2/mysqldb } }
- In the links section, notice the mysql container is listed
- docker inspect wordpresscms2
- 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”
-
- docker start wordpresscms2
- 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 } }
- docker inspect mysqldb
- 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>
- Update the database host to point to the ipaddress:port of the mysql container
- 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
- Browse to localhost:8082
- docker run -d –env-file env-wordpress.txt -p 8082:80 –name wordpresscms wordpress
- 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.