1

I am trying to automate the development of a project I am currently working on by creating a container on Docker that is composed of mysql, php and nginx to run a Symfony project. Here is my docker-compose.yml


services:
  database:
    container_name: database
    image: mysql:8.0
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: rapidhcm
      MYSQL_USER: admin
      MYSQL_PASSWORD: password
    ports:
      - '4306:3306'
    volumes:
      - ./docker/mysql:/var/lib/mysql

  php:
    container_name: php
    build:
      context: .
    ports:
      - '9000:9000'
    volumes:
      - ./rapid-backend:/var/www/public
    depends_on:
      - database

  nginx:
    container_name: nginx
    image: nginx:stable-alpine
    ports:
      - '8080:80'
    volumes:
      - ./rapid-backend:/var/www/public
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - database

And here is my Dockerfile:

FROM php:8.0-fpm

RUN apt update \
    && apt install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
    && docker-php-ext-install intl opcache pdo pdo_mysql \
    && pecl install apcu \
    && docker-php-ext-enable apcu \
    && docker-php-ext-configure zip \
    && docker-php-ext-install zip

WORKDIR /var/www/html

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony5/bin/symfony /usr/local/bin/symfony
RUN git config --global user.email "[email protected]" \ 
    && git config --global user.name "Damian Kowalski"

The Dockerfile and docker-compose.yml files are in the root folder of the project, however the Symfony index.php file is situated in ./rapid-backend/public/index.php. How can I indicate that I want to serve that php file?

1 Answer 1

1

You would typically use only two containers: one for the database and one for the php-enabled web server.

You start from an php+apache image or an nginx image with php installed. Then you can bind-mount the web server's configuration files just as you did in your docker-compose.yaml for the nginx container.

You'd have something like this in your Dockerfile:

FROM php:8.2-apache AS production

ADD default.conf /etc/apache2/sites-available/default.conf

RUN # <...>
    # Install tools and PHP dependencies
    # apt-get update && \
    # apt-get install -y <...> && \
    # Configure docker php extensions, see image documenation on dockerhub
    # docker-php-ext-configure <...> && \
    # docker-php-ext-install <...> && \
    # Maybe you want xdebug? In that case you'll also want to copy a .ini file
    # pecl install xdebug && \
    # Cleanup
    # docker-php-source delete && \
    # Install latest composer 2
    # curl https://getcomposer.org/composer-2.phar > /usr/local/bin/composer && \
    # chmod +x /usr/local/bin/composer && \
    # Enable some apache mods if needed
    # a2enmod rewrite headers expires xsend && \
    # Some more cleanup if needed
    # apt-get remove -y <...> && \ 
    # apt-get clean

# If this image is to be used in a development environment, here you may want to create a user that you'll tell Apache to use with APACHE_RUN_USER, using the same UID as your own user on your host, so that you can bind-mount your sources in your docker-compose.yaml file without having permission issues.

# If however this is a deployment image, here you will probably want to "COPY" your source code in the image and to "RUN composer install".

with the appropriate apache configuration file default.conf that exposes your index.php (see the Symfony documentation at https://symfony.com/doc/current/setup/web_server_configuration.html for example configuration files).

Then your web server service in docker-compose.yaml could look like:

web-server:
  build:
    context: .
  ports:
    - '8080:80'
  # If this is a dev environment, bind mount your source code, maybe also
  # have a "user: ..." option here if you created one in the Dockerfile
  volumes:
    - ./rapid-backend:/var/www/public

It's a pretty open question but I hope this is a push in the right direction. Starting from here you can use docker-compose exec to run a bash shell inside your web server, and use composer to install Symfony and initialize your project.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.