Solution

The solution to exercise 3

We'll cover the following...

Congratulations!! You’ve made it this far. You can pat your back, because as you have done a wonderful job. Solving this exercise might have squeezed all your developer plus Docker knowledge, but you did not give up and that’s a good thing.

Even if you were not able to solve this exercise, but you’ve tried your best, kudos to you because this exercise was a little tricky one. So, let’s go through the solution and see what you might have missed.

version: '3'
services:
  web:
    # Path to dockerfile.
    # '.' represents the current directory in which
    # docker-compose.yml is present.
    build: .

    # Mapping of container port to host
    
    ports:
      - "5000:5000"
    # Mount volume 
    volumes:
      - "/usercode/:/exercise_3"

    # Link database container to app container 
    # for rechability.
    links:
      - "database:exercisedb"

    command: ["flask", "run"]

    environment:
      - "FLASK_RUN_HOST=0.0.0.0"

    depends_on:
      - database
    
  database:

    # image to fetch from docker hub
    build:
      context: ./db
      dockerfile: Dockerfile-db
    #image: mysql/mysql-server:5.7

    # Environment variables for startup script
    # container will use these variables
    # to start the container with these define variables. 
    environment:
      - "MYSQL_ROOT_PASSWORD=root"
      - "MYSQL_USER=testuser"
      - "MYSQL_PASSWORD=admin123"
      - "MYSQL_DATABASE=backend"
      
    # Mount init.sql file to automatically run 
    # and create tables for us.
    # everything in docker-entrypoint-initdb.d folder
    # is executed as soon as container is up nd running.
    volumes:
      - "/usercode/db/init.sql:/docker-entrypoint-initdb.d/init.sql"
    

Here, we will only see the new changes in ...