demo icon indicating copy to clipboard operation
demo copied to clipboard

Create the best possible docker-compose.yml for this project

Open javiereguiluz opened this issue 6 years ago • 30 comments

Calling for help from Docker experts. We need to create the best possible docker-compose.yml file for this project. The application requirements are well defined (we use env vars, Webpack Encore, PHP 7.1, Symfony 4.1, SQLite database, etc.) so it should be possible to create that file.

javiereguiluz avatar May 21 '18 09:05 javiereguiluz

An important note mentioned to me by a person on Symfony Slack (I don't remember exactly who, I'm sorry). In the normal Symfony Demo app, we must use SQLite because it's our only option to use a database that requires zero setup.

However, when using Docker, we can use any database with zero setup. So, let's use PostgreSQL in the Docker config file. (I suggest PosgreSQL because to me it looks more popular among Symfony devs than MySQL).

javiereguiluz avatar May 25 '18 13:05 javiereguiluz

the hard part is that our config/packages/doctrine.yml file is currently incompatible with Postgresql as it defines a mysql-only charset (the proper UTF-8 charset is the utf8 on postgresql, as they got it right directly instead of creating a second charset like utf8mb4 for proper UTF-8 support)

stof avatar May 25 '18 13:05 stof

@stof could we run a quick sed command on that config file to replace the wrong value while setting up things in Docker?

javiereguiluz avatar May 25 '18 14:05 javiereguiluz

@javiereguiluz if we copy the file inside the image at build time, we could. If we want to mount the source code instead (to make it a dev-like environment where you can edit things), we cannot modify files in the image (as they are not in the image)

stof avatar May 25 '18 14:05 stof

@javiereguiluz we can remove charset: utf8mb4 in this file (https://github.com/symfony/demo/blob/master/config/packages/doctrine.yaml) and add it as a parameter ?charset=utf8mb4 in the env.dist file (https://github.com/symfony/demo/blob/master/.env.dist)

DATABASE_URL=sqlite:///%kernel.project_dir%/var/data/blog.sqlite?charset=utf8mb4

then we can create a new .env file for the docker environment

sebastianblum avatar May 28 '18 07:05 sebastianblum

Hay all, subscribed to this issue when it was being talked about in slack. Was toying around with docker the other day and got reminded of this. Not the most experienced with Docker but can try and help where I can 😄

Do we have any ideas what base image we would want to use. 7.1-jessie maybe?

syther101 avatar Jul 04 '18 10:07 syther101

we can take this : https://github.com/dunglas/symfony-docker ?

OlivierToussaint avatar Jul 04 '18 10:07 OlivierToussaint

I would vote for sqlite because you do not have to start an additional container for this database.

About the requirements:

  • mailing should also be possible? Or should we ignore this feature as it is disabled by default?
  • how about the webserver? Which one should be used or should the build-in webserver be used?
  • very often discussed: How about the phpunit and composer runner? Some ppl start the official images ignoring the PHP version used there. Others create own "dev" PHP images with phpunit and composer installed. I prefer the later one because imho it is very important that those runners run within the same PHP environment than your application does.

frastel avatar Jul 04 '18 11:07 frastel

@OlivierToussaint the aim of your linked docker setup is to start a new Symfony project not to use an existing one.

Btw: The project uses the second approach I mentioned in my prior comment.

frastel avatar Jul 04 '18 11:07 frastel

@frastel yes I know, but the base of dockerfile & docker-compose is here, we can be inspired

OlivierToussaint avatar Jul 04 '18 11:07 OlivierToussaint

@OlivierToussaint Ok, for inspiration it is good enough.

@Syther101 The question of the base image is a good one. php:7.1-jessie would be 378MB in size, php:7.2.7-fpm-alpine would be only 81MB. I would try it with the alpine image. I am aware of the fact that not everything is possible with alpine images. But the Docker workshops I hosted have shown it is very important to use the smallest Docker Images possible.

frastel avatar Jul 04 '18 11:07 frastel

There are several possibilities, but to summarize:

  • php:7.2-fpm-alpine with everything needed by Symfony 4.1 and Webpack Encore
  • nginx:1-alpine VS built-in webserver
  • postgres:alpine VS SQLite

I personally prefer the approach where everything is provided by a dedicated container. Also, I'm using djfarrelly/maildev on my local environment to test emails. Does someone know a better alternative?

ajardin avatar Jul 04 '18 13:07 ajardin

I am using this guy

  • https://github.com/tulik/symfony-4-docker-runtime-env

It's good (not include yarn support for assets yet)

moosti avatar Jul 12 '18 04:07 moosti

How about building each image from alpine with 3 Dockerfile inside project?
I needed such resources a few time ago and I think it could be nice

thomas-hiron avatar Oct 12 '18 16:10 thomas-hiron

This one is based on a setup from phpdocker.io, with a couple of modifications. I know it can be improved on. Contents are here

j4nr6n avatar Nov 03 '18 17:11 j4nr6n

I built this one recently: https://github.com/o-alquimista/symfony-demo-docker

  • None of the containers start as root. The user symfonyd was created for this.
  • Unix socket used to connect HTTPD with PHP-FPM.
  • Uses SQLite.
  • Node and Composer containers included. Composer creates the project on startup.
  • Commented code.
  • SELinux ready.
  • Well organized directory structure.

However:

  • Before starting it, you must edit the docker-compose.yml file and set your UID where needed. Node and Composer, for example, run as your UID and the GID of the web server.

If anyone knows how to automatically detect the UID for the compose file, then things get a lot more simple.

docker-compose up -d

After the containers node and composer exit, the application is ready at localhost.

I haven't discovered any errors or limitations so far.

o-alquimista avatar Aug 03 '19 16:08 o-alquimista

@javiereguiluz is help still wanted?

Update: #1275

mehdibo avatar Nov 26 '21 23:11 mehdibo

@mehdibo hi! Sadly, there's a big problem when trying to solve this.

The technical part is not a problem, because many people here know how to dockerize this app.

Problem is that there are many different ways of solving it ... and people using Docker can't agree on "the best way" to solve this for this project. Discussions usually end up being an endless debate about why doing XXX is better than doing YYY, and later someone comes and says that both XXX and YYY are wrong and we should do ZZZ.

It's a shame, but how things are 😐

javiereguiluz avatar Nov 29 '21 15:11 javiereguiluz

@javiereguiluz Oh, that's a shame, can't someone from the core team just decide "dectatorally"?

I don't think such a debate is important for a demo app.

If you have an example of a debate I'd be happy to take a look at it (I promise I won't jump in xD)

mehdibo avatar Nov 29 '21 15:11 mehdibo

The decision taken by the core team for the Symfony way of running a dev environment is to run PHP on the host and not in Docker (see the book for an example using that setup). So if we go for "the core team way" to fix this issue, it would get closed as "won't do".

stof avatar Nov 29 '21 16:11 stof

Yes, I would either implement the "core way"/"Symfony CLI way" or don't implement anything at all.

wouterj avatar Nov 29 '21 16:11 wouterj

I see, well as Javier said that's a shame, from 2018 and we still don't have a Dockerized demo 😅 I hope we all reach a conclusion after all these years, thanks for the information!

mehdibo avatar Nov 29 '21 18:11 mehdibo