Connect Containers with Link

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful tool for deploying applications, but it can initially seem complex. This challenge will guide you through linking Docker containers to create a multi-container application. We'll begin with simple examples and gradually progress to a more complex application. Even if you're new to Docker, you should find this easy to follow. We'll explore how to make containers communicate with each other, which is a fundamental aspect of building robust applications with Docker.

This is a Challenge, which differs from a Guided Lab in that you need to try to complete the challenge task independently, rather than following the steps of a lab to learn. Challenges are usually a bit difficult. If you find it difficult, you can discuss with Labby or check the solution. Historical data shows that this is a beginner level challenge with a 83% pass rate. It has received a 81% positive review rate from learners.

Create an Image with Dockerfile

Before we can link containers, we need to have at least one containerized application. In this step, you'll create a Dockerfile for your my-app image. This Dockerfile will define the environment and the application to be run within the container.

Task

The goal of this step is to create a Dockerfile for your my-app image.

Requirements

  • Docker must be installed on your machine.
  • You should have an existing my-app application you wish to containerize.

Example Result

  1. Create the necessary files for the challenge:

    Create a new file named Dockerfile at the /home/labex/project/ path with the following content:

    • Use python:3.7-slim as the base image.
    • Set the working directory to /app.
    • Copy the current directory's contents into the container at /app.
    • Install the required packages.
    • Expose port 80 to the outside world.
    • Define an environment variable (though we don't use it in this example, keep the instruction).
    • Run app.py when the container starts.

    Create a file named app.py in your project directory /home/labex/project/ with the following content:

    import os
    
    os.system("wssh --address='0.0.0.0' --port=80")

    Create a file named requirements.txt in your project directory /home/labex/project/ with the following content:

    webssh==1.6.2
    labex:project/ $ pwd
    /home/labex/project
    labex:project/ $ ll
    total 12K
    -rw-r--r-- 1 labex labex 59 Jan 24 15:21 app.py
    -rw-r--r-- 1 labex labex 163 Jan 24 15:19 Dockerfile
    -rw-r--r-- 1 labex labex 14 Jan 24 15:21 requirements.txt
  2. Use the docker build command to build the my-app image.

    labex:project/ $ docker images | grep my-app
    my-app latest 266edf714faf 30 seconds ago 170MB
  3. Start a new container using the my-app image, and note the port mapping.

    labex:project/ $ docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    11f06f08d2d3 my-app "python app.py" 4 seconds ago Up 4 seconds 0.0.0.0:8080- hopeful_elgamal > 80/tcp
  4. Open a web browser and navigate to http://localhost:8080 to verify that your application is running correctly.

    Web app running in browser
✨ Check Solution and Practice

Linking Two Docker Containers

Now that we have a containerized application, let's move on to linking containers. In this step, we will learn how to link two Docker containers, enabling them to communicate with each other. This process is key to creating multi-service applications within Docker.

Task

Link an Apache web server container with a MySQL database container.

Requirements

  • You'll need two Docker containers: one running Apache and one running MySQL.
  • Docker must be installed on your machine.

Hint

  • Remember to open a new terminal to start this step so it doesn't conflict with your previous container.

Example Result

Access MySQL from the Apache container:

  1. Launch an Apache container named my_apache based on the httpd image, mapping host port 80 to container port 80.

    labex:project/ $ docker ps | grep my_apache
    a91a93216e84 httpd "httpd-foreground" 52 seconds ago Up 47 seconds 0.0.0.0:80- my_apache > 80/tcp
  2. Launch a MySQL container named my_mysql, setting the MYSQL_ROOT_PASSWORD environment variable to password, using the mysql image.

    labex:project/ $ docker ps | grep mysql
    0cb864cf97c6 mysql "docker-entrypoint.s…" 42 seconds ago Up 35 seconds 3306/tcp, 33060/tcp my_mysql
  3. Link the Apache container to the MySQL container.
    Note: For this step, no direct command output is expected, but the link will be visible in the container's environment variables. You can inspect container with command docker inspect my_apache

    labex:project/ $ docker ps | grep my_app
    859c201b7267 my-app "python app.py" 53 seconds ago Up 52 seconds 80/tcp my_app
  4. Use the docker exec command to access the MySQL command-line interface from the my_app container (you will need to adapt the command if you linked differently).

    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.3.0 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MySQL [(none)]>
✨ Check Solution and Practice

Linking Multiple Docker Containers and Using Environment Variables

Building on the previous step, we will now explore how to link multiple containers and leverage environment variables for configuration. This step shows how to create more complex and flexible multi-container environments.

Task

Launch three containers: one running Apache, one running MySQL, and one running a custom application. The custom application should be able to access both MySQL and Apache.

Requirements

  • You'll need three Docker containers: Apache, MySQL, and a custom application
  • Docker must be installed on your machine.

Example Result

  1. Launch an Apache and MySQL container as described in Step 2.

  2. Launch a custom application container named my_app2 that can access both MySQL and Apache.

    labex:project/ $ docker ps | grep app2
    8945b42659a6 my-app "python app.py" 15 seconds ago Up 15 seconds 80/tcp my_app2
  3. Use curl to access the custom application through the Apache container.

    <html>
      <body>
        <h1>It works!</h1>
      </body>
    </html>
✨ Check Solution and Practice

Summary

In this challenge, you learned how to link Docker containers together to create multi-container applications. We progressed from a single container to linked pairs and finally to a three-container setup. You now have an understanding of how to leverage container links and the docker exec command to allow communication between different services, enabling you to build more complex applications. Keep experimenting, and have fun!