DEV Community

Cover image for Rapid Docker on AWS: How to test locally?
Andreas Wittig
Andreas Wittig

Posted on

Rapid Docker on AWS: How to test locally?

As promised one of the benefits of Docker is that you can test your application locally. To do so, you need to spin up three containers:

  • NGINX
  • PHP-FPM
  • MySQL (which will be replaced by Amazon Aurora when deploying on AWS)

Theoretically, you can create the needed containers manually with docker run but doing so is a little cumbersome. You will use Docker Compose, a tool for running multi-container applications, instead.

All you need to do is to create a Docker Compose file docker-compose.yml.

Customization You probably need to add your own environment variables for the PHP container (see inline comments).

version: '3'
services:
  nginx:
    build:
      context: '..'
      dockerfile: 'docker/nginx/Dockerfile' # build your NGINX image
    depends_on:
    - php
    network_mode: 'service:php' # use network interface of php container to simulate awsvpc network mode
  php:
    build:
      context: '..'
      dockerfile: 'docker/php-fpm/Dockerfile' # build PHP image
    ports:
    - '8080:80' # forwards port of nginx container
    depends_on:
    - mysql
    environment: # add your own variables used by envsubst here
      DATABASE_HOST: mysql
      DATABASE_NAME: app
      DATABASE_USER: app
      DATABASE_PASSWORD: secret
  mysql:
    image: 'mysql:5.6' # matches the Amazon Aurora MySQL version
    command:'--default-authentication-plugin=mysql_native_password'
    ports:
    - '3306:3306' # forwards port 3306 to 3306 on your machine
    environment:
      MYSQL_ROOT_PASSWORD: secret # password for root user
      MYSQL_DATABASE: app # create database with name app
      MYSQL_USER: app # user app is granted full access to db app
      MYSQL_PASSWORD: secret # the password for user app

From within your temporary working environment execute the following command to spin up the containers on your machine.

docker-compose -f docker/docker-compose.yml up

Magically, Docker Compose will spin up three containers. Point your browser to http://localhost:8080/index.php to check that your web application is up and running. The log files of all containers will show up in your terminal which simplifies debugging a lot.

If you need to make a change to your setup, please cancel the running docker-compose process CTRL + C and restart with the following command afterwards to make sure the images get re-built.

docker-compose -f docker/docker-compose.yml up --build

Use your favorite MySQL client and connect to localhost:3306 with username root and password secret if you need to create a schema or restore a database dump.

After you have verified that your application is working correctly, cancel the running docker-compose process by pressing CTRL + C, and tear down the containers.

docker-compose -f docker/docker-compose.yml down

It's time to deploy your web application on AWS. You will learn how to to so in the next part of this series.

Do you have any questions? Please leave them in the comments. This is the 3rd post of a series. Follow me to make sure you are not missing the following posts.

Rapid Docker on AWS
This post is an excerpt from our new book Rapid Docker on AWS. The book includes code samples for PHP, Ruby (Rails), Python (Django), Java (Spring Boot), and Node.js (Express).

Top comments (0)