Setting Up an Environment with Docker Compose

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

In this Live Environment, you will be creating a development environment using PHP and MySQL for a project you are working on. Because the environment is a bit sophisticated, you’ve decided that it would be easier to build it out using Docker-Compose. Create a Docker-Compose file that sets up a db and webapp container. Use volumes so that your database and PHP files will be persistent.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Setting Up an Environment with Docker Compose

Introduction

In this hands-on lab, we will create a development environment using PHP and MySQL for a project we are working on.

Solution

Open a terminal session, and log in to the server via SSH using the credentials provided on the lab page:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Then, become the root user:

sudo su -

Create a Docker Compose File

  1. Create the docker-compose.yml file:

    vi docker-compose.yml
  2. Enter the following:

    version: '3.3'
    services:
      db:
        image: mysql:5.7
        volumes:
          - db_data:/var/lib/mysql
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: R00tP4ssw0rd
          MYSQL_DATABASE: app_name
          MYSQL_USER: db_user
          MYSQL_PASSWORD: 4pp_n4meP4sswrd0!
      webapp:
        image: php:7.0-apache
        volumes:
          - app_data:/var/www/html
          - app_logs:/var/logs/apache2
        restart: always
        links:
          - db
    
    volumes:
        db_data:
        app_data:
        app_logs:

    Save and exit the file.

Run the Containers

  1. Bring the containers up and make sure they are detached:

    docker-compose up -d

Conclusion

Congratulations on completing this hands-on lab!