Docker - how to set up Apache + PHP in docker-compose.yml - php

I use this to set up nginx for PHP:
nginx:
image: nginx:latest
ports:
- 8080:80
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:7-fpm
volumes:
- ./code:/code
But how about Apache? How can I set up Apache + PHP in docker-compose.yml?
Following this guide:
version: '2'
services:
php:
build: php
ports:
- "80:80"
- "443:443"
volumes:
- ./php/www:/var/www/html
Error:
ERROR: In file './docker-compose.yml' service 'version' doesn't have any configuration options. All top level keys in your docker-compose.yml must map to a dictionary of configuration options.
Any ideas? I'm on Xubuntu 16.04.
EDIT:
After managing to upgrade docker-compose to 1.9, I try with this file below:
version: '2'
services:
php:
build: php
expose:
- 9000
volumes:
- ./php/www:/var/www/html
apache2:
image: webdevops/apache:latest
args:
- PHP_SOCKET=php:9000
volumes:
- ./php/www:/var/www/html
ports:
- 80:80
- 443:443
links:
- php
Error:
$ sudo docker-compose up -d
Building php
ERROR: Cannot locate specified Dockerfile: Dockerfile
Docker is such as pain!
Any ideas how to fix this?

I would choose webdevops dockerized apache, because it has simple configuration:
version: '2'
services:
php:
build: php
expose:
- 9000
volumes:
- ./php/www:/var/www/html
apache2:
image: webdevops/apache:latest
args:
- PHP_SOCKET=php:9000
volumes:
- ./php/www:/var/www/html
ports:
- 80:80
- 443:443
links:
- php

Since the example above does not work, here is a different approach:
docker-compose.yml
version: '3.1'
services:
php:
image: php:apache
ports:
- 80:80
volumes:
- ./php/www:/var/www/html/
Launch the server with
docker-compose up

We need to create a new folders /php/www in current path
Create a file under php folder save as "Dockerfile" which contains as below without quote
"FROM php:5.6-apache
RUN docker-php-ext-install mysqli"
Copy your docker-compose.yml file in your current folder where your "php" folder has.
Create a sample file "index.php" under www folder (/php/www/index.php)
Run in command prompt docker-compose up -d
Open your browser type "localhost" you can see your sample file results.
Note: Above steps as per above mentioned docker-compose.yml file.

You can check this question.
If you use build instead of image, then you need "Dockerfile". Dockerfile would be use as configuration file for building image.
You maybe miss part in guide, where you should create file with name "Dockerfile" inside directory "php". Directory "php" must be in the same directory, where your "docker-compose.yml". In "docker-compose.yml" you have this line.
build: php
The line mean, that configuration file (by default: "Dockerfile") is inside of directory "php". So you should create directory "php" and file "Dockerfile" inside of it.
This is "Dockerfile" from your guide.
FROM php:5.6-apache
RUN docker-php-ext-install mysqli
docker-compose.yml reference version 2
Dockerfile reference

I found an elegant way to dynamically configure the ports and other parameters: In apache2's configuration files you can reference environment variables.
#/etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
#APACHE_HTTP_PORT_NUMBER:80
#APACHE_HTTPS_PORT_NUMBER:443
Listen ${APACHE_HTTP_PORT_NUMBER}
<IfModule ssl_module>
Listen ${APACHE_HTTPS_PORT_NUMBER}
</IfModule>
<IfModule mod_gnutls.c>
Listen ${APACHE_HTTPS_PORT_NUMBER}
</IfModule>
you can set the variables in Dockerfile or docker-compose.yml

You can set a directory with diferente Dockerfiles an declare in each service:
...
image: php:custom
build:
context: .
dockerfile: ./dockerfiles/Dockerfile-php
...

I have created a working example of PHP, APACHE, MYSQL, and PHPMYADMIN for PHP developers. You may find it useful if you need the original old-school working style. Please note that I am using port 8080 for my website and port 8081 for PHPMyAdmin. You can change these as you like.
version: '3.8'
services:
php-apache-environment:
container_name: php-apache
image: php:7.4-apache
volumes:
- ./php/src:/var/www/html/
ports:
- 8080:80
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: admin
MYSQL_DATABASE: ezapi
MYSQL_USER: root
MYSQL_PASSWORD: password
ports:
- "6033:3306"
volumes:
- dbdata:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
volumes:
dbdata:

Related

Docker how to run node js

This is my docker-compose.yml
version: '3.9'
networks:
bedrock:
services:
web:
container_name: kawa-web
image: nginx:stable-alpine
volumes:
- ./:/var/www/html:delegated
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- 8080:80
depends_on:
- php
- mysql
networks:
- bedrock
php:
container_name: kawa-php
image: nanoninja/php-fpm:8.0
volumes:
- ./:/var/www/html
- ./docker/config/php.ini:/usr/local/etc/php/conf.d/php.ini
ports:
- 9000:9000
networks:
- bedrock
mysql:
container_name: kawa-db
image: mysql:8
volumes:
- ./docker/db:/var/lib/mysql:delegated
ports:
- 3306:3306
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
networks:
- bedrock
node:
container_name: kawa-node
build:
context: .
dockerfile: node.dockerfile
volumes:
- ./:/var/www/html
networks:
- bedrock
Content of node.dockerfile
FROM node:18-alpine
WORKDIR /var/www/html
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
When I run docker compose up -d it shows
failed to solve: error from sender: open /path/to/project/docker/db/#innodb_temp: permission denied
How can I fix this? Or any another way to run nodejs inside PHP container maybe?
When your Compose setup has:
services:
mysql:
volumes:
- ./docker/db:/var/lib/mysql:delegated
node:
build:
context: .
The MySQL data directory is in the ./docker/db directory. That's inside the . build-context directory of the Node application, so docker-compose build among other things sends the entire MySQL data to itself, and if the database is currently running, you could get lock or permission problems like this.
The best approach to work around this is to split your application into separate directories, and have each language component only build its own subdirectory.
$ ls -1F
data/
docker-compose.yml
js/
php/
static/
$ ls -1F js
Dockerfile
index.js
node_modules/
package.json
package-lock.json
# docker-compose.yml
version: '3.8'
services:
...
mysql:
image: mysql:8
volumes:
- ./data/db:/var/lib/mysql:delegated
ports:
- 3306:3306
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
node:
build: ./js
(Note that I've used the short syntax for build: to use the default Dockerfile in the context directory; I've removed unnecessary container_name: and networks: options, using the Compose-provided default network; and I've removed the volumes: that overwrite the image's content. Make sure ./js/Dockerfile has a CMD instruction that says how to start the container.)

Symfony 5 DATABASE_URL with Docker containers

I Created a docker configuration for LEMP local server. I Tried to connect the Symfony app with MySQL database version 8 but the Connection is refused.
Error: SQLSTATE[HY000] [2002] Connection refused.
The problem is with DATABASE_URL in .env file required by Symfony 5.
What is the correct value for DATABASE_URL for a given docker-compose.yml configuration ? I run this on Docker for Windows. I was Able to connect to mysql bash with username root and root password.
docker-compose.yml:
###############################################################################
# Generated on phpdocker.io #
###############################################################################
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: sampleapp-memcached
mailhog:
image: mailhog/mailhog:latest
container_name: sampleapp-mailhog
ports:
- "8001:8025"
redis:
image: redis:alpine
container_name: sampleapp-redis
mysql:
image: mysql:8.0
container_name: sampleapp-mysql
working_dir: /app
volumes:
- .:/app
environment:
- MYSQL_ROOT_PASSWORD=Ur7HJWzZ2QK9
- MYSQL_DATABASE=maindatabase
- MYSQL_USER=sampleuser
- MYSQL_PASSWORD=FxGBWfJ86ykq
ports:
- "8002:3306"
elasticsearch:
image: elasticsearch:6.5.4
container_name: sampleapp-elasticsearch
webserver:
image: nginx:alpine
container_name: sampleapp-webserver
working_dir: /app
volumes:
- .:/app
- ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
php-fpm:
build: phpdocker/php-fpm
container_name: sampleapp-php-fpm
working_dir: /app
volumes:
- .:/app
- ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.4/fpm/conf.d/99-overrides.ini
Usually the DATABASE_URL string is a standard one:
mysql://user:pwd#host:port/db
In this case:
DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq#127.0.0.1:8002/maindatabase
Put it in your .env.local file and it should work. Note however that I work on unix and I do not have a Windows machine nearby.
Update
Following Alexander's suggestion, if you are running your Symfony app in a container (I didn't notice your webserver container, sorry!), then you should change the string to
DATABASE_URL=mysql://sampleuser:FxGBWfJ86ykq#mysql:3306/maindatabase
Note, however, that for container to container communications you should use the default port (or expose a new one).
As regards setting the serverVersion, you can use the query parameter or (IMHO a better approach) set it in your doctrine.yaml config file. In addition, note that your server version should match the one you've specified in your docker-compose file.

Why docker not syncing files inside container on Windows 10?

I have issue after last docker update (seems so) on Windows 10 (local development). When I changed files in PhpStorm (and in another editors - Sublime, Notepad+), after a while, files inside container didn't receive changes.
Steps that can help for a while:
If I completely shut down all containers and after that arise them again. docker-compose down && docker-compoes up
If I get into php-fpm container and for file that not changed ran touch file.php (this file will be immidiatly changed).
What I tried and it didn't help:
I restarted php-fpm and nginx containers docker-compose restart php-fpm nginx (Yes it's strange, because down|up for all container helped)
I changed inside PhpStorm setting Use Safe write(save changes for temporary file first)
Also I checked inode for file inside container. With ls -lai file.php. Before changes worked and after they broked I had the same inode number. There is no determined number of changes I must to do to break syncing, it's random, sometime 2 changes enough.
I have:
Docker version 19.03.5, build 633a0ea
docker-compose version 1.25.2, build 698e2846
docker-compose.yml
version: '3'
services:
nginx:
container_name: pr_kpi-nginx
build:
context: ./
dockerfile: docker/nginx.docker
volumes:
- ./:/var/www/kpi
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./docker/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf
ports:
- "8081:80"
links:
- php-fpm
networks:
- internal
php-fpm:
container_name: pr_kpi-php-fpm
build:
context: ./
dockerfile: docker/php-fpm.docker
volumes:
- ./:/var/www/kpi
links:
- kpi-mysql
environment:
# 192.168.221.1 -> host.docker.internal for Mac and Windows
XDEBUG_CONFIG: "remote_host=host.docker.internal remote_enable=1"
PHP_IDE_CONFIG: "serverName=Docker"
networks:
- internal
mailhog:
container_name: pr_kpi-mailhog
image: mailhog/mailhog
restart: always
ports:
# smtp
- "1025:1025"
# http
- "8025:8025"
networks:
- internal
kpi-mysql:
container_name: pr_kpi-kpi-mysql
image: mysql:5.7
command: mysqld --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
volumes:
- ./docker/storage/kpi-mysql:/var/lib/mysql
environment:
# We must change prod secrets, this is not good approach
- "MYSQL_ROOT_PASSWORD=pass"
- "MYSQL_USER=user"
- "MYSQL_PASSWORD=user_pass"
- "MYSQL_DATABASE=kpi_db"
ports:
- "33061:3306"
networks:
- internal
kpi-npm:
container_name: pr_kpi-npm
build:
context: ./
dockerfile: docker/npm.docker
volumes:
- ./:/var/www/kpi
- /var/www/kpi/admin/node_modules
ports:
- "4200:4200"
networks:
- internal
tty: true
# For xdebug
networks:
internal:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.221.0/28
P.S. There is opened issue:
https://github.com/docker/for-win/issues/5530
P.P.S. We need to update Docker from 2.2.0.0 to 2.2.0.3, Seems it's fixed
I have a separate container for syncing my folder:
app:
image: httpd:2.4.38
volumes:
- ./:/var/www/html
command: "echo true"
I just use the basic apache image, you could use anything really though. Then in my actual containers, I use the following volumes_from key:
awesome.scot:
build: ./build/httpd
links:
- php
ports:
- 80:80
- 443:443
volumes_from:
- app
php:
build: ./build/php
ports:
- 9000
- 9001
volumes_from:
- app
links:
- mariadb
- mail
environment:
APPLICATION_ENV: 'development'
I've never had an issue using this set up, files always sync fast, and I have tested both on Mac OSX and MS Windows.
If you're interested, here is my full LAMP stack on Github https://github.com/delboy1978uk/lamp
I have the same issue on Windows10 since 31st Jan.
I have commented a line in PhpStorm and checked it in the container using vim.
The changes were not there.
If I run docker-compose down and up, the changes go in the container.
Docker version 19.03.5, build 633a0ea
docker-compose version 1.25.4, build 8d51620a
Nothing changed in my docker-compose.yml since 2018.

Docker connection refused between multiple projects

I set up 2 projects (Admin and API) and try to move into docker on local.
I can access the running web instances on both without any problems, but when the Admin tries to make a Curl requests to the API, I get a cURL error:
cURL error 7: Failed to connect to localhost port 8080
This is my docker-compose.yml file contents:
version: "3.1"
services:
memcached:
image: memcached:alpine
container_name: project-admin-memcached
redis:
image: redis:alpine
container_name: project-admin-redis
mariadb:
image: mariadb:10.1
container_name: project-admin-mariadb
working_dir: /application
volumes:
- ./Projects:/application
environment:
- MYSQL_ROOT_PASSWORD=docker
- MYSQL_DATABASE=db_test
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- "8083:3306"
# docker-compose exec webserver sh
# docker exec -it project-admin-webserver nginx -s reload
webserver:
image: nginx:alpine
container_name: project-admin-webserver
working_dir: /application
volumes:
- ./Projects/Api:/application/api
- ./Projects/Admin:/application/admin
- ./Docker/nginx:/etc/nginx/conf.d
ports:
- "8080:8080"
- "8090:8090"
# docker-compose exec php-fpm bash
php-fpm:
build: Docker/php-fpm
container_name: project-admin-php-fpm
working_dir: /application
volumes:
- ./Projects:/application
- ./Docker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
I can access both projects from my browser with:
http://localhost:8080/ <= API
http://localhost:8090/ <= Admin
How can I fix this?
inside your docker network (create by default with a compose), you have to use the container name.
So inside a container you have to use http://webserver:8080

Docker: docker-compose.yml file of nginx+mariadb+php+redis

I want to write a docker-compose.yml of nginx+mariadb+php+redis,
I read the documentation about compose-file,url: https://docs.docker.com/compose/compose-file/#versioning
format is like this:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
networks:
- front-tier
- back-tier
redis:
image: redis
volumes:
- redis-data:/var/lib/redis
networks:
- back-tier
volumes:
redis-data:
driver: local
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge
But I don't know how to write the compose-file of nginx+mariadb+php+redis,I want to reference some examples.And,I use the official images of Docker Hub,url: https://hub.docker.com/explore/
**ps:**software version:
OS:centos7.2
nginx:latest
php:latest
mariadb:latest
redis:latest
I would go with something along these lines:
version: '2'
services:
web:
container_name: my_app
build: .
links:
- redis
- mariadb
nginx:
container_name: nginx
image: nginx
links:
- my_app
ports:
- 80:80
- 443:443
redis:
container_name: redis
image: redis
mariadb:
container_name: mariadb
image: mariadb
So create a dockerfile for your project, and extend the official PHP image by adding your files like in the readme.
This docker-compose.yml will start your container and link it to the nginx container. This means it will be available under my_app hostname, and you will need to add your own nginx config to pass the requests to that container.
Redis an mariadb will also be triggered by docker-compose and will be made available inside your app container under hostnames redis and mariadb.
Nginx should be the only container with ports exposed on the host.
The dockerfile above is not a complete solution, you will need to add an nginx config, and probably provide some environment variables here and there.
I hope this helps.

Categories