I'm working on configuring a docker setup for an old php project with apache, php 7.2 and mariadb. After I got the container up and running I get permission denied when I try to write a file from php.
What is the best approach to solve this?
docker-compose.yml
version: "3"
networks:
dirtbike:
services:
webserver:
build:
context: .
dockerfile: Dockerfile
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
volumes:
- ./public_html:/var/www/html
database:
image: mariadb:10.3
container_name: dirtbike-database
restart: 'always'
networks:
- dirtbike
ports:
- "127.0.0.1:3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
Dockerfile
FROM php:7.2-apache-stretch
RUN docker-php-ext-install pdo_mysql
RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/
COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf
index.php
<?php
file_put_contents(__DIR__.DIRECTORY_SEPARATOR.'test.txt', 'lorem ipsum');
The problem was that the user from the container (www-root) was different then the user from the host.
I added args: uid: ${UID} in the docker-compose file like this:
version: "3"
networks:
dirtbike:
services:
webserver:
build:
context: .
dockerfile: Dockerfile
args:
uid: ${UID}
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
volumes:
- ./public_html:/var/www/html
in Dockerfile I added ARG uid and RUN usermod -u ${uid} www-data && groupmod -g ${uid} www-data; like this:
FROM php:7.2-apache-stretch
ARG uid
RUN docker-php-ext-install pdo_mysql
RUN a2enmod ssl && a2enmod rewrite && a2enmod headers
RUN mkdir -p /etc/apache2/ssl
COPY ./Docker/ssl/*.pem /etc/apache2/ssl/
COPY ./Docker/config/apache/dirtbike.conf /etc/apache2/sites-available/000-default.conf
RUN usermod -u ${uid} www-data \
&& groupmod -g ${uid} www-data;
In your docker compose file you can to add your local machine user. First you need check current user id, in my case it is ubuntu:
echo ${UID}
Output:
1001
docker-compose.yml:
webserver:
build:
context: .
dockerfile: Dockerfile
container_name: dirtbike-webserver
restart: 'always'
depends_on:
- database
ports:
- "80:80"
- "443:443"
networks:
- dirtbike
user: 1001 # local machine user id
volumes:
- ./public_html:/var/www/html
Hope help you.
Related
It seems that when i run Docker compose up, docker is not reading from docker-compose.yml.
It seems like it is loading images from cache or i don't think where is finding them.
Bellow is my docker-compose.yml
version: '3'
services:
httpd:
image: httpd:latest
user: root
ports:
- "80:80" # Default Apache port (Default on PHP 7.4)
- "8073:8073" # PHP 7.3 Apache port
- "8074:8074" # PHP 7.4 Apache port
- "8081:8081" # PHP 8.1 Apache port
volumes:
- ./:/var/www/html/myApp/:rw
- ./dev/Docker/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
restart: on-failure
container_name: httpd
networks:
- mb-frontend
php8.1-fpm:
build: ./dev/Docker/php-fpm/8.1
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php8.1-fpm
networks:
- mb-frontend
- mb-backend
php7.4-fpm:
build: ./dev/Docker/php-fpm/7.4
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.4-fpm
networks:
- mb-frontend
- mb-backend
php7.3-fpm:
build: ./dev/Docker/php-fpm/7.3
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.3-fpm
networks:
- mb-frontend
- mb-backend
db:
image: mariadb:10.3.5
environment:
MYSQL_ROOT_PASSWORD: myPassword
MYSQL_USER: dev
MYSQL_PASSWORD: myPassword
ports:
- "3306:3306"
volumes:
- /root/Bureau/mysql:/var/lib/mysql/:rw
- ./dev/Docker/mariadb/conf.d/:/etc/mysql/conf.d/:rw
- ./dev/Docker/mariadb/config/init.sql:/docker-entrypoint-initdb.d/init.sql
restart: on-failure
container_name: db
networks:
- mb-backend
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
volumes:
- /root/Bureau/phpmyadmin:/var/lib/mysql/
networks:
- mb-backend
depends_on:
- db
redis:
image: redis:6.2
container_name: redis
ports:
- "6379:6379"
networks:
- mb-backend
networks:
mb-frontend:
driver: bridge
mb-backend:
driver: bridge
I commented some images on docker-compose.yml but when i tape the command Docker compose up on terminal, all images even commented images are Up.
Can anyone help me how i force docker to read images from the edited docker-compose.yml
Good practice would be do use:
docker compose down
and then
docker compose up
UPDATE:
Next I would suggest to clean up your containers:
List all containers:
docker ps -a
Remove those you don't want because they might still be in the system
docker rm <CONTAINER ID/NAME>
How to Stop & Remove a running container by ID or Name?
The command is actually docker compose up.
The command docker-compose has been deprecated as of latest version. We can now use docker compose without the hyphen(-).
You can use docker-compose -f <path-to-compose-file> to pass in the compose file.
Example:
docker compose -f docker-compose.yml up
Reference documentation: https://docs.docker.com/compose/reference/
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.)
I have a simple Dockerfile for php:7.2-fpm that is being referenced by my docker-compose.yml configuration. I want to get rid of the Dockerfile which is located at php/Dockerfile and integrate the commands to my docker-compose-yml file.
Here is my php/Dockerfile:
FROM php:7.2-fpm
RUN docker-php-ext-install mysqli
And here is my docker-compose.yml which brings up php-fpm, nginx, letencrypt certbot, mysql, and phpmyadmin:
version: '3'
services:
php:
container_name: dev_php
build:
context: ./php
ports:
- 9000:9000
volumes:
- ./website:/website
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
nginx:
container_name: dev_nginx
image: nginx:1.13.8
ports:
- 80:80
- 443:443
volumes:
- ./website:/website
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
depends_on:
- php
- mysql
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
container_name: dev_certbot
image: certbot/certbot
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
mysql:
container_name: dev_mysql
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- ./mysql:/docker-entrypoint-initdb.d
phpmyadmin:
container_name: dev_pma
image: phpmyadmin/phpmyadmin
links:
- mysql
depends_on:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8183:80
Since my php/Dockerfile is relatively simple, I thought I could do something like this:
php:
container_name: dev_php
image: php:7.2-fpm
ports:
- 9000:9000
volumes:
- ./website:/website
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
command: bash -c "docker-php-ext-install mysqli"
But this does not work. When I bring my project up using docker-compose up I can see dev_php (the name of the PHP container) perform a bunch of tasks that I do not ordinarily see, it then proceeds to install mysqli but then quits immediately after with exit code 0.
Thoughts?
Your original Dockerfile does much more than just installing the mysqli extension.
It has the entrypoint as shown in the source code here
It also has the command as shown here
These entrypoint and command are what will start the container and keep it running
Now back to your modification:
I can see dev_php (the name of the PHP container) perform a bunch of tasks that I do not ordinarily see
This is the output of docker-php-ext-install mysqli. You did not see it before is because it was done when building the docker image.
quits immediately after with exit code 0
This is expected because the command has finished processing and there is nothing else for it to do.
In order to keep the container running, you will have to manually add in the original entrypoint and command. Something like this:
php:
container_name: dev_php
image: php:7.2-fpm
ports:
- 9000:9000
volumes:
- ./website:/website
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
entrypoint:
- bash
- -c
command:
- |
docker-php-ext-install mysqli; \
docker-php-entrypoint php-fpm
I have not tested to confirm that the syntax is 100% correct but you should get the idea. The important bit is docker-php-entrypoint php-fpm
My recommendation is to just stick to using Dockerfile in this case.
A good side effect is that Docker will cache the build layers so you do not need to wait for the installing process of mysqli every time you want to start the containers.
You may consider enabling the extension too
When loading docker-compose up, wordpress loads on the url but the mysqli_connect function is undefined because of the absence of the extension.
I have tried to add the following under the fpm image
command: "docker-php-ext-install mysqli"
I have tried to add a Dockerfile into the directory of the docker-compose.yml file containing
version: "3"
services:
#database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# webserver
nginx:
image: nginx:latest
ports:
- "8080:80"
links:
- fpm
volumes:
- /Users/connergesbocker/Github/cbgesbocker/dotfiles/root/etc/nginx/conf.d/village.conf:/etc/nginx/conf.d/village.conf
- /Users/connergesbocker/WordPress:/WordPress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8888:8888"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: wordpress
networks:
- wpsite
fpm:
image: php:5.6.20-fpm
ports:
- "90:9000"
# command: "docker-php-ext-install mysqli"
links:
- db
volumes:
- /Users/connergesbocker/WordPress:/WordPress
working_dir: "/"
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:```
You could customize your dockerfile & add install in it:
Dockerfile:
FROM php:5.6.20-fpm
RUN docker-php-ext-install mysqli
Part of docker-compose.yaml:
fpm:
build: .
image: myphp:1
ports:
- "90:9000"
# command: "docker-php-ext-install mysqli"
links:
- db
volumes:
- /Users/connergesbocker/WordPress:/WordPress
working_dir: "/"
networks:
- wpsite
your override will install the extension but the container will stop after installation because php-fpm will not start by overriding CMD. Replace the command with below one.
command:
- /bin/sh
- -c
- |
docker-php-ext-install mysqli
echo "extension installed.....starting php-fpm........................................"
php-fpm
I want to run php website with letsencrypt and nginx using docker-compose. But it doesn't work. I tried to use other simple web page (not php), like: index.html and it works.
My docker-compose file is based on this project: https://github.com/gilyes/docker-nginx-letsencrypt-sample/blob/master/docker-compose.yml.
Help me, please with php, thanks.
version: "2"
services:
nginx:
restart: always
image: nginx
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- "/etc/nginx/conf.d"
- "/etc/nginx/vhost.d"
- "./website:/usr/share/nginx/html"
- "./volumes/proxy/certs:/etc/nginx/certs:ro"
depends_on:
- fpm
nginx-gen:
restart: always
image: jwilder/docker-gen
container_name: nginx-gen
volumes:
- "/var/run/docker.sock:/tmp/docker.sock:ro"
- "./volumes/proxy/templates/nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro"
volumes_from:
- nginx
entrypoint: /usr/local/bin/docker-gen -notify-sighup nginx -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
letsencrypt-nginx-proxy-companion:
restart: always
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: letsencrypt-nginx-proxy-companion
volumes_from:
- nginx
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
- "./volumes/proxy/certs:/etc/nginx/certs:rw"
environment:
- NGINX_DOCKER_GEN_CONTAINER=nginx-gen
fpm:
restart: always
build: ./php-fpm
ports:
- "9000"
container_name: fpm-website
volumes:
- "./website:/usr/share/nginx/html" # folder with web-site
- "./php-fpm/php.ini-production.ini:/usr/local/etc/php/php.ini:ro"
environment:
- VIRTUAL_HOST=website.com
- LETSENCRYPT_HOST=website.com
- LETSENCRYPT_EMAIL=ser#website.com
Here is my Dockerfile for "fpm" container:
FROM php:7.3.2-fpm-alpine
RUN apk update; \
apk upgrade;
RUN docker-php-ext-install mysqli
When I try to open page, I have a message:
502 Bad Gateway
nginx/1.15.12