简体   繁体   中英

How to set up Docker for Symfony project with MySQL?

How to set up a new Symfony project with MySQL database using Docker?

I've been trying to set up a new project using Docker for over a week now. I've read trough Docker documentation, found a few tutorials, but nothing really worked for me. And I'm just not able to crack how Docker set up works. Last time I tried I just got a RuntimeException and an ErrorException errors

Project Structure:

-myProject
  -bin
    -...
  -config
    -...
  -docker
    -build
      -php
        -Dockerfile
  -php
  -public
    -index.php
  -src
    -...
  -var
    -...
  -vendor
    -...
  -docker-compose.yaml
  -...

My docker-compose.yaml:

version: '3.7'
services:
  php:
    build:
      context: .
      dockerfile: docker/build/php/Dockerfile
    ports:
      - "8100:80"

  # Configure the database
  mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root}

My Dockerfile:

FROM php:7.3-apache
COPY . /var/www/html/

I expected to have "Welcome to Symfony" page but I got an error page. Errors:

ErrorException
Warning: file_put_contents(/var/www/html/var/cache/dev/srcApp_KernelDevDebugContainerDeprecations.log): failed to open stream: Permission denied

AND

RuntimeException
Unable to write in the cache directory (/var/www/html/var/cache/dev)

What I need is some help to set up my Symfony 4 project with MySQL using Docker

OK so to make it work I just needed to give permision to var folder using chmod in Dockerfile

FROM php:7.3.2-apache

COPY . /var/www/html/

RUN chmod -R 777 /var/www/html/ /var/www/html/

Found this answer in the comments, but the person that left it removed the comment

You actualy have no need to chmod your project root folder to something unnecessary open, like 0777.

In php:* containers php workers run from www-data user. So all you need to do is chown your current project root dir to www-data and verify that www-data user can actualy create folders in it (ls -lah will help you).

Here is my php stage from symfony 4.3 projects:

FROM php:7.3-fpm as runtime

# install php ext/libraries and do other stuff.

WORKDIR /var/www/app
RUN chown -R www-data:www-data /var/www/app
COPY --chown=www-data:www-data --from=composer /app/vendor vendor
COPY --chown=www-data:www-data bin bin
COPY --chown=www-data:www-data config config
COPY --chown=www-data:www-data public public
COPY --chown=www-data:www-data src src
COPY --chown=www-data:www-data .env .env

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM