Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker production ready php-fpm and nginx configuration

Tags:

I have a small theoretical problem with combination of php-fpm, nginx and app code in Docker.

I'm trying to stick to the model when docker image does only one thing -> I have separate containers for php-fpm and nginx.

php:     image: php:5-fpm-alpine     expose:         - 9000:9000     volumes:         - ./:/var/www/app  nginx:     image: nginx:alpine     ports:         - 3000:80     links:         - php     volumes:         - ./nginx/app.conf:/etc/nginx/conf.d/app.conf         - ./:/var/www/app 

NOTE: In app.conf is root /var/www/app; Example schema from Symfony

This is great in development, but I don't know how to convert this to production ready state. Mount app directory in production is really bad practice (if I'm not wrong). In best case I copy app source code into container and use this prebuilded code (COPY . /var/www/app in Dockerfile), but in this case is impossible or I don't know how.

I need share app source code between two contatiner (nginx container and php-fpm container) because booth of that need it.

Of course I can make own nginx and php-fpm container and add COPY . /var/www/app into both of them, but I thing that is wrong way because I duplicate code and the whole build process (install dependencies, build source code, etc...) must be in both (nginx/php-fpm) containers.

I try to search but I don't find any idea how to solve this problem. A lot of articles show how to do this with docker-compose file and mount code with --volume but I didn't find any example how to use this on production (without volume).

Only one acceptable solutions for me (in this time) is make one container with nginx and php-fpm together but I'm not sure when is a good way (I try to find best practice).

Do you have any experiences with this or any idea how to solve it?

Thanks for any response!

like image 644
Budry Avatar asked Apr 25 '17 22:04

Budry


People also ask

Is it possible to run PHP-FPM in a docker container?

You can argue that PHP-FPM depends on a webserver to actually function and this responsibility can be considered as one. Thus, one container running both Nginx (or your webserver of choice) and PHP is desirable. Docker containers can run multiple services just fine.

How to run PHP-FPM and Nginx in the same container?

This script runs php-fpm first in the background, and then Nginx without exiting. In this way, Nginx always starts listening to ports after PHP-FPM is initialized. Show activity on this post. You should deploy two container, one with fpm, the other with nginx, and you should link them.

How do I run a PHP/Nginx server in Docker?

Now you can run the PHP/Nginx instance using: Run docker-compose up and then open php-nginx.localhost in Chrome (or another browser if you have dnsmasq installed). Create an index.php file that simply contains <?php phpinfo (); to test this out.

How to set up a modern PHP development environment with Docker?

Setting Up a Modern PHP Development Environment with Docker 1 A Little Background. One of the problems with web development is that things change at a rapid pace. ... 2 Setting Things Up. That’s the theory out of the way. ... 3 docker-compose.yml for NGINX. ... 4 Running the Service. ... 5 MySQL. ... 6 Done! ... 7 TL;DR! ...


1 Answers

I solve the problem by making a shared volume with the docker-compose file:

version: '3'  volumes:  share_place:  services:  php:   image: php:5-fpm-alpine   ports:    - 9000:9000   volumes:    - share_place:/var/www/app  nginx:   image: nginx:alpine   ports:    - 3000:80   volumes:    - share_place:/var/www/app 

This will create a volume share_place that will share the data between the two container.

like image 154
mhlsf Avatar answered Sep 20 '22 08:09

mhlsf