I am new in docker and i would like to know how to have a web server environnement with docker.
What i need :
PHP
HTTPD, NGINX
I have installed docker on Debian 9.4 and iam able to run docker commands now.
I have issued docker pull php and docker pull nginx.
I have a application running on a lamp server (Debian 7) actually
All my application is on /var/www/application
How to create docker containers for my project ? (NGINX, PHP)
Does i need a dockercompose file ? How to write it for my need ?
Does i need a dockerfile ? How to write it for my need ?
Can u please guide me a bit ;)
Thanks in advance to the community.
In future i would like to export my application on AWS Elasticbeanstalk.
Database i will use is stored on AWS cloud (Amazon RDS) so i don't need a container for database.
My recommendation is to use docker-compose. this is a sample LEMP:
docker-compose.yml
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./your_project:/your_project
- ./nginx.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
build: .
volumes:
- ./your_project:/your_project
links:
- mysql
mysql:
image: mysql:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
Dockerfile
FROM php:fpm
RUN apt-get update
RUN docker-php-ext-install mysql mysqli
RUN echo "localhost localhost.localdomain" >> /etc/hosts
nginx.conf
server {
index index.php index.html;
server_name your.domain;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /your_project;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Related
I had two WordPress websites on a Synology NAS at home running on NGINX with PHP-FPM 7.4 with virtual hosts.
I am moving these websites on a Debian VM also at home, and run the services in rootless Docker:
MariaDB official Docker container
NGINX official Docker container
PHP-FPM official Docker container
These websites are exposed through a Traefik Docker container and my DNS and Let's Encrypt certificates are managed with the Cloudflare API.
When I try to access to www.wordpress1.com, I get "The Page Isn’t Redirecting Properly" error.
I tried to add a phpinfo.php files in my WordPress websites, and I can access these files from WAN without any problem.
I tried to add a static website, and I can access it without any problem from WAN.
I have checked the logs for the two websites, and I get a lot of these errors:
GET / HTTP/1.1" 301 5
Here are my configuration files.
NGINX compose.yaml:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
restart: always
security_opt:
- no-new-privileges:true
networks:
- backend
- traefik
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./conf/servers.conf:/etc/nginx/conf.d/default.conf
- ./log:/var/log/nginx
- nginx_data:/var/www/html
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.entrypoints=http"
- "traefik.http.routers.nginx.rule=Host(`static.com`,`www.wordpress1.com`,`www.wordpress2.com`)"
- "traefik.http.middlewares.nginx-https-redirect.redirectscheme.scheme=https"
- "traefik.http.routers.nginx.middlewares=nginx-https-redirect"
- "traefik.http.routers.nginx-secure.entrypoints=https"
- "traefik.http.routers.nginx-secure.rule=Host(`static.com`,`www.wordpress1.com`,`www.wordpress2.com`)"
- "traefik.http.routers.nginx-secure.tls=true"
- "traefik.http.routers.nginx-secure.service=nginx"
- "traefik.http.services.nginx.loadbalancer.server.port=80"
- "traefik.docker.network=traefik"
volumes:
nginx_data:
external: true
networks:
backend:
external: true
traefik:
external: true
NGINX servers.conf:
server {
listen 80;
server_name static.com;
root /var/www/html/static;
index index.html;
error_log /var/log/nginx/static_error.log;
access_log /var/log/nginx/static_access.log;
}
server {
listen 80;
server_name www.wordpress1.com;
root /var/www/html/wordpress1;
index index.php index.html;
error_log /var/log/nginx/wordpress1_error.log;
access_log /var/log/nginx/wordpress1_access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
server {
listen 80;
server_name www.wordpress2.com;
root /var/www/html/wordpress2;
index index.php index.html;
error_log /var/log/nginx/wordpress2_error.log;
access_log /var/log/nginx/wordpress2_access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
PHP compose.yaml:
version: '3'
services:
php:
build:
context: .
dockerfile: ./Dockerfile
image: custom/php:7.4-fpm
container_name: php
restart: always
security_opt:
- no-new-privileges:true
networks:
- backend
user: 1234:1234
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- nginx_data:/var/www/html
volumes:
nginx_data:
external: true
networks:
backend:
external: true
PHP Dockerfile:
FROM php:7.4-fpm
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && install-php-extensions mysqli exif imagick zip
In the nginx_data volume, my permissions are 1234:1234, which are my docker user UID and GID on my rootless Docker install.
My database is on a mariadb docker container and the connection works (modified in the wp-config.php).
I don't understand why I can access the static website, why the phpinfo in the WordPress websites works, and why the WordPress websites doesn't work.
I though it was a problem of permission, but I used the "user" parameter in my PHP Docker container to specify it.
Thanks for your help.
[Edit] It seems the second WordPress website is displayed. So only the first one has this redirection error problem.
I tried with PHP v7.4 and PHP v8.1, same result.
I would suggest you to delete the previous SSL if you copied it, also delete the .httacess file if you copied it from the old one, if the problem persists you should check if any php extension is not installed.
I'm using docker compose to boot up a development workspace, consisting of php, nginx and mysql. Everything boots, static html get's served, but when trying to start a laravel app, i get the following error:
The stream or file "/home/html/storage/logs/laravel-2019-06-10.log" could not be opened: failed to open stream: Permission denied
I searched around and it looked like a permissions issue? Do note, that the docker with just the database and the build in php server does seem to work.
My docker-compose.yml
version: "3"
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
ports:
- 3306:3306
php-fpm:
image: php:7.3-fpm-alpine
links:
- db
volumes:
- "./:/home/html/"
nginx:
image: nginx:1-alpine
ports:
- "8080:80"
links:
- php-fpm
volumes:
- "./site.conf:/etc/nginx/conf.d/default.conf"
- "./:/home/html/"
My nginx config:
server {
index index.php index.html;
listen 80 default_server;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /home/html/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Kind regards :)
Enter the php-fpm container:
docker-compose -i -t exec php-fpm /bin/sh
Then change access rights of storage folder:
chmod -r 777 /home/html/storage
Cause it's local development environment, correct rights doesn't matter.
I am working on my raspbberry pi (arm architecture) and I am using docker to run containers on a very lightweight OS (HyperiotOS). I have successfully set the nginx + php-fpm container that nginx config serves php file if the volume is mounted with code on both containers.
Now my problem is: I am trying to set the nginx pretty much as reverse proxy, meaning I don't want the nginx to access any code on drive I would like to forward all requests into php container and display results. I tried proxy_pass to it, but it did not work. Is there a way to do that ? Example of code below.
My docker compose:
version: '3'
services:
nginx:
image: arm32v7/nginx:latest
container_name: nginx
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./logs:/var/log/nginx/
- ./code:/code # this is what I want to get rid off
networks:
- webserver
php:
image: arm32v7/php:7.3-fpm
expose:
- "9000"
restart: unless-stopped
container_name: php-fpm
volumes:
- ./code:/code
networks:
- webserver
networks:
webserver:
driver: bridge
My nginx config:
server {
listen 80;
index index.php index.html;
server_name raspberry.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /code;
location ~ \.php$ {
try_files $uri /dev/null =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Now inside the /code folder, I have index.php with php info.
This configuration works well and after accessing raspberry.local:8080 I can see php info page.
However, in order to access it, I have that /code mounted in nginx container.
- ./code:/code # this is what I want to get rid off
Now I want to eliminate that so I can put the nginx as balancer somewhere else without the need to access code. I would like to proxy the requests straight to php container. I tried proxy_pass http://php:9000 but that broke the thing. What is the best way to "detach" the nginx from the code and just act as a standalone proxy? There are several reasons why to eliminate that if possible, one is production type build. I cannot share the folder it needs to be baked in. I don't want two containers carrying the code. I could just throw in apache into php container and proxy nginx inside if that would be the case and eliminate the fpm.
I'm currently stuck on a problem.
I got a Server where i run multiple docker.
here i need to put have an php nginx one.
i got a docker-compose file like this :
version: 2
services:
web:
image: nginx:latest
ports:
- 'XXXX:80'
- 'XXXX:443'
volumes:
- ./code:./code
- ./site.conf:/etc/nginx/conf.d/default.conf
environnement:
- VIRTUAL_HOST:...
- LETSENCRYPT_HOST:....
- LETSENCRYPT_MAIL:....
networks:
- default
php:
build:
context: ./php
volumes:
- ./code:/code
networks:
- default
networks:
default:
external:
name:webproxy
My networks make me getting a automatique letsencrypt ssl
My dockerfile for php is :
FROM php:7.2-fpm
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
My config is :
server{
listen 80
index index.php index.html
server_name localhost
error_log /var/log/nginx/error.log
access_log /var/log/nginx/access.log
root /code
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php 9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info
}
}
My structure of folder is something like :
- imgFolder
- GameFolder
- index.html
- service.php
- imgFolder
- cssFolder
- desktop.html
- mobile.html
- main.css
All seems to be fine web i make my docker-compose compose up -d
What i meen by all seems to be fine is :
. HTML is well render
. SSL is also well apply
But sometimes (not all the time but something like half time) my php file was not found on ajax request (got a 404 file not found in networks debug)
Did someone have an idea of why my service.php is sometimes not found ?
Dunno if it could help but i got exactly the same docker compose working alongside this one (different port) and there is no prob with my php files call.
You are having this issue because you have more than one container with same hostname (php) in same network.
I simulated same scenario, and here is the result.
I'm trying to create a simple LAMP stack in a Docker enviroment. It worked by running a third-party container phpdockerio/php71-fpm:latest, but I wanted a custom PHP container with XDebug installed, for the moment.
My problem is that, if I execute docker-compose up, the PHP container exit after startup before my webserver container can make usage of it. How can I successfully tell the PHP container to wait for a connection of my nginx container?
Command-Line Output
PS C:\playground> docker-compose.exe up
Starting playground_php_1
Starting playground_web_1
Attaching to playground_php_1, playground_web_1
playground_php_1 exited with code 0
playground_web_1 exited with code 1
Dockerfile
FROM php:latest
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug
ENTRYPOINT ["docker-php-entrypoint"]
CMD ["php", "-a"]
docker-compose.yml
version: '2'
services:
php:
build:
context: ./etc/php/
dockerfile: Dockerfile
volumes:
- './src:/usr/share/nginx/html'
web:
image: nginx:latest
ports:
- 8080:80
volumes:
- './etc/nginx:/etc/nginx/conf.d'
- './src:/usr/share/nginx/html'
depends_on:
- php
nginx configuration
...
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
I got it running, again, with my custom container. I changed the base image from php to php-fpm.
Next thing I did, was that I had to clear running containers and to delete already created images on my machine. Otherwise docker-compose would use the wrong/old container again.