Advanced
Web Applications


Docker with Compose

Get ready

You need to bring your own computer to the practical.

Make sure you have Docker installed you can execute following command:


      docker run hello-world
    

In addition, please run following command before attending the practical.


      # Apache with PHP
      docker pull php:8.3.6-apache
    

You need to have the nswi153/frontend Docker image from previous practical.

Objectives

  • Docker compose
    Docker compose file

Docker compose

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:
      client:
        image: nswi153/frontend
        ports:
          - 8090:80
        environment:
          SERVER_URL: 'https://webik.ms.mff.cuni.cz/nswi153/2024-2025/service/09/api/'
  
  • restart
  • depends_on
  • healthcheck
  • volumes
  • variable defaults
  • variables and .env file

Assignment: backend

./practical-10/

Copy practical-03/backend and practical-09/frontend from previous practicals to practical-10.

Create a Dockerfile for a container hosting the web application backend. Name the image nswi153/backend. Update the source code of the application so that:

  • Database host can be set using DATABASE_HOST.
  • Database driver can be set using DATABASE_DRIVER.

Use php:8.3.6-apache as a base image.

You are allowed to modify the copied files.

...

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.

Assignment: docker compose

./practical-10/

Create docker-compose.yaml file hosting:

  • nswi153/frontend build from ./frontend
  • nswi153/backend build from ./backend
  • database
  • adminer

Assignment: docker compose

./practical-10/postgres

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:16.2 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".



You must implement and push the assignments to GitLab before 8.5.2025 23:59 UTC.

Extra content ...

Content beyond this slide is not mandatory part of the seminar. Yet it may help you with better use and understanding of Docker.

Keeping deployment up-to-date

We can employ tools like Watchtower to automatically update the containers.

Questions, ideas, or any other feedback?

Please feel free to use the anonymous feedback form.