NSWI153: Advanced Web Applications

Containers : Docker Compose

Preliminaries

Please read this section at least one day prior to the seminar. This section outlines what you are expected to know, be able to do, or prepare in advance. Following these instructions will help you get the most out of the seminar and ensure smooth participation.

Preliminaries : Knowledge, Skills, and Competence


Before the start of the practical, you should be able to:

  • Explain relation between a Dockerfile, a Container and an Image.
  • Know how to deploy your PHP application.

Preliminaries : Using your own computer


You need to bring your own computer to the practical. If you can not you will need to work with your collogue.

Before the start of the practical, make sure that:

  • Make sure you have Docker installed.
  • Make sure you can successfully execute following command
    
              docker run hello-world
            
  • Have your Docker image for your frontend application from last practical ready as "nswi153/frontend".
  • Run following commands to download images we use during practicals.
    
              # PHP with Apache
              docker pull php:8.4.20-apache
              # Nginx
              docker pull nginx:1.29.8
              # Databases
              docker pull postgres:18.3
            

Objectives

  • Docker compose
  • Backend container
  • Database container

Docker compose

What if we need more then one container?

Docker compose - CLI

Docker compose - File

Services are defined in docker-compose.yaml file. Values for environment variables are automatically loaded from .env file.


      version: '3.8'
      services:
        frontend:
          image: nswi153/frontend
          ports:
            - 8090:80
          environment:
            BACKEND_BASE_URL: 'https://webik.ms.mff.cuni.cz/nswi153/2024-2025/service/09/'
    
  • restart
  • depends_on
  • healthcheck
  • volumes
  • variable defaults
  • variables and .env file

Exercise: Container for backend

Time to create a container for backend. It is just another container, right?

Assignment: Container for backend

GitLab: ./practical-04/


Following slides should guide you towards a reasonable Dockerfile for backend and name it Name the image nswi153/backend. Beware of potential pitfalls! You are allowed to modify the application, but try to keep the changes minimal.

Use php:8.4.20-apache as a base image. Database host and driver can be set using DATABASE_HOST and DATABASE_DRIVER respectively.

Continue to the next slide once you are done >>>

Preparing the environment

This is a list of steps that you need to carry out to create functional Docker image for the assignment.

  • Enable rewrite and allow it is use in .htaccess.
    
              sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
              a2enmod rewrite
            
  • Update package index so you can install packages. Next install requirements for Composer and pgsql.
    
              apt-get update
              apt-get -y --no-install-recommends install unzip libpq-dev
            
  • Install PHP extensions using provided script.
    
              docker-php-ext-install pgsql
            
  • Install Composer and use it to install all dependencies.

Congratulations, if you have followed the instructions, you have just reached the end of this exercise.

Exercise: Docker compose

Single (*) command deployment.

Assignment: Container for backend

GitLab: ./docker-compose.yaml


Create docker-compose.yaml file hosting:

  • nswi153/frontend build from ./practical-06/
  • nswi153/backend build from ./practical-03/
  • database from ./practical-10/
  • adminer

Continue to the next slide once you are done >>>

Assignment: Database

GitLab: ./practical-10


Create Dockerfile with postgres:16.2. Automatically populate the database nswi153 with articles and authors.

Once ready utilize the image in the docker-compose file as the database. You may need to modify other files in the practical-10 directory to make it work.

For image postgres:18.3 you can copy SQL scripts to "/docker-entrypoint-initdb.d". Those scripts get automatically executed after a database is created. Declare a named volume for postgres data "postgres_data:/var/lib/postgresql/data".

Congratulations, if you have followed the instructions, you have just reached the end of this exercise.

Questions, ideas, or any other feedback?

Please feel free to use the anonymous feedback form.