Skip to content

Latest commit

 

History

History
120 lines (100 loc) · 3.45 KB

docker-compose-todolist-solution.md

File metadata and controls

120 lines (100 loc) · 3.45 KB

The Dockerfile in your PHP todolist repository should look something like this:

FROM bitnami/php-fpm:8.4.3

ENV PHP_CONF_DIR=/opt/bitnami/php/etc

COPY index.php /app/

RUN echo >> "/opt/bitnami/php/etc/php-fpm.d/www.conf" && \
    echo 'env["TODOLIST_DB_USER"] = $TODOLIST_DB_USER' >> "/opt/bitnami/php/etc/php-fpm.d/www.conf" && \
    echo 'env["TODOLIST_DB_PASS"] = $TODOLIST_DB_PASS' >> "/opt/bitnami/php/etc/php-fpm.d/www.conf" && \
    echo 'env["TODOLIST_DB_NAME"] = $TODOLIST_DB_NAME' >> "/opt/bitnami/php/etc/php-fpm.d/www.conf" && \
    echo 'env["TODOLIST_DB_HOST"] = $TODOLIST_DB_HOST' >> "/opt/bitnami/php/etc/php-fpm.d/www.conf" && \
    echo 'env["TODOLIST_DB_PORT"] = $TODOLIST_DB_PORT' >> "/opt/bitnami/php/etc/php-fpm.d/www.conf"

Here's a sample .dockerignore file to go with it:

# https://docs.docker.com/engine/reference/builder/#dockerignore-file
/compose.yml
/.gitignore
/images
/LICENSE.txt
/README.md
/update.sh

Here's an example of what the compose.yml file in your PHP todolist repository can look like:

name: todolist

services:
  # Reverse proxy
  rp:
    image: nginx:1.27.3-alpine
    depends_on:
      - app
    networks:
      - front-tier
    ports:
      - "${TODOLIST_PORT:-12000}:80"
    restart: always
    volumes:
      # Overwrite the default configuration in the container with the site
      # configuration in this directory.
      - ./site.conf:/etc/nginx/conf.d/default.conf:ro

  # PHP todolist application
  app:
    image: todolist/app
    # Build the image based on the Dockerfile in this directory.
    build: .
    depends_on:
      - db
    environment:
      # Connect to the database service.
      TODOLIST_DB_HOST: db
      # Read the password from the .env file or the environment.
      TODOLIST_DB_PASS:
    networks:
      - front-tier
      - back-tier
    ports:
      - "12001:9000"
    restart: always

  # Database
  db:
    image: mysql:9.2.0
    environment:
      MYSQL_DATABASE: todolist
      MYSQL_USER: todolist
      # Read the passwords from the .env file or the environment.
      MYSQL_PASSWORD:
      MYSQL_ROOT_PASSWORD:
    networks:
      - back-tier
    restart: always
    volumes:
      # Persist server data in a named Docker volume.
      - "db_data:/var/lib/mysql"
      # Run the todolist database setup script on server initialization.
      - ./todolist.sql:/docker-entrypoint-initdb.d/todolist.sql:ro

# Separate backend and frontend networks for increased security.
networks:
  back-tier:
  front-tier:

volumes:
  db_data:

📚 This Compose file example includes the configurability and network isolation improvements suggested in the Make it more configurable and Make it more secure sections.

Here's a sample .env file you could use with this compose.yml file:

MYSQL_PASSWORD=spoiled-barrel-enlighten-stoke
MYSQL_ROOT_PASSWORD=deniable-earthlike-boil-say-avoid
TODOLIST_DB_PASS=spoiled-barrel-enlighten-stoke