How to create a Symfony project (only) in Docker container

Viewed 43

I wish to install Symfony and use it on a project, but do it without installing it on my system. So I figured it could be done using Docker, yet my efforts to make it work haven't paid off so far. I created a Dockerfile where I tried installing everything I could possibly need and then running Symfony, while I was setting up a simple docker-compose.yml. When I try to up it, the container just exists, and by its log, it seems that Symfony could not be found, even though the image seems to build ok.

So what would be the correct way to accomplish this?

Dockerfile:

FROM php:8.1-apache

RUN a2enmod rewrite
 
RUN apt-get update \
  && apt-get install -y libzip-dev git wget --no-install-recommends \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
 
RUN docker-php-ext-install pdo mysqli pdo_mysql zip;
 
RUN wget https://getcomposer.org/download/2.2.0/composer.phar \
    && mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer

RUN composer create-project symfony/skeleton:"6.1.*" app

RUN cd /app

CMD ["symfony", "server:start"]

docker-compose.yml:

version: '3.7'
 
services:

db:
    image: mysql:5.7
    environment:
        - MYSQL_ROOT_PASSWORD=somerootpass
        - MYSQL_PASSWORD=somepass
        - MYSQL_DATABASE=dockerizeme_db
        - MYSQL_USER=someuser
web:
    build: .
    ports:
        - 8080:8000
    volumes:
        - './app:/app'
    depends_on:
            - db
1 Answers

You have a couple problems here.

First, you did not install the Symfony's CLI in your container, see https://symfony.com/download.

Without this, you can not use the symfony command. The compose create-project command does not install the CLI, it's only creating the framework skeleton.

Next, you are mounting a local folder ./app on your container's /app thus, the result of create project in your Dockerfile is overwritten at run time.

If you want to create the project in your local folder mounted inside the container, you would have to do it in the ENTRYPOINT.

But, since it's something you will most likely want to do only once, if you really do not want anything on your local computer, you could take the following approach.

  1. Temporarily change your command, maybe in your docker-compose.yaml file to ["sleep", "infinity"] and re-up your containers
  2. Run a command docker compose exec web composer create-project symfony/skeleton:"6.1.*" app
  3. Change back your command and re-up your containers one last time

Bind mounts are mounted at run time so they are not yet mounted during your build.

Also, I see that you run Symfony's dev server but are using a Apache PHP image. I would normally do one or the other but not both.

Plus you do a RUN cd /app but the correct way to do that in that context would be WORKDIR /app

Related