In my PHP application i am using docker on my ubuntu machine. Previously it was working perfectly but now its not. I am trying to show image from my uplaoded folder. The link showing the the image src is correct.Plus file also exist in folder but the image is not showing . When i try to access image directly from browser URL its saying
Forbidden
You don't have permission to access this resource.
Apache/2.4.38 (Debian) Server at localhost Port 80
My Dockerfile is
FROM php:7.3.28-apache
RUN docker-php-ext-install mysqli
RUN chown -R www-data /var/*
RUN chown -R 777 /var/*
RUN a2enmod rewrite
EXPOSE 80
everything is working fine but only the image is not coming in my browser.
You can see in the above pic image src is there but its showing blank and image is exist in the folder.Also i tried to change the permission to 777 but didnt worked.
sudo chmod -R 777 uploads
Docker Compose.yaml
version: '3.1'
services:
php:
container_name: php_medix_uk
depends_on:
- db
build:
context: .
volumes:
- .:/var/www/html/
- ./config/vhosts:/etc/apache2/sites-enabled
- ./config/php/php.ini:/usr/local/etc/php/php.ini
ports:
- "80:80"
restart: "always"
# MySQL
db:
image: mysql:8.0.25
container_name: mysql_host
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: medix_pharma
MYSQL_USER: medix_pharma
MYSQL_PASSWORD: medix_pharma
volumes:
- ~/mysql/medix_uk:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
# phpMyAdmin
phpmyadmin:
# container_name: phpmyadmin
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOSTS=mysql_host
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- 8080:80
depends_on:
- db
inside ./config/vhosts directory i have default.conf file
DocumentRoot /var/www/html/
<Directory /var/www/html/>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Thanks
Related
I was able to access the mysql database from phpmyadmin using user: admin and password: root and the url: 127.0.0.1:3310 and I was able to load the website on 127.0.0.1:8008 but when i try to login or interact with the database i get the error below:
SQLSTATE[HY000] [2002] Connection refused
select * from users where email = boyiajas#gmail.com limit 1
and I also try to do a migration from within the app docker container but failed as well
root#58a709f18668:/var/www/html# php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravelvueblog_db and table_name = migrations and table_type = 'BASE TABLE')
below is my .env file
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3309
DB_DATABASE=laravelvueblog_db
DB_USERNAME=admin
DB_PASSWORD=root
below is my vhost.conf file
<VirtualHost *:8008>
DocumentRoot /var/www/html/public
<Directory "/var/www/html/public">
AllowOverride all
Require all granted
</Directory>
#ErrorLog ${APACHE_LOG_DIR}/error.log
#CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
below is my Docker file
FROM php:8-apache
USER root
RUN apt-get update -y && apt-get install -y openssl curl zip unzip git nano
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install mysqli pdo pdo_mysql opcache
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
COPY vhost.conf /etc/apache2/sites-available/000-default.conf
RUN chown -R www-data:www-data /app && a2enmod rewrite
RUN rm -rf /var/www/html && ln -s /app /var/www/html
RUN composer install
RUN php artisan optimize:clear
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
below is my docker-compose.yaml file
version: '3.8'
services:
db:
image: mariadb:latest
container_name: db
ports:
- 3309:3306
environment:
MYSQL_DATABASE: laravelvueblog_db
MYSQL_ROOT_PASSWORD: root
MYSQL_PASSWORD: root
MYSQL_USER: admin
volumes:
- mysql_file:/docker-entrypoint-initdb.d
networks:
- appnetwork
main:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
ports:
- 8008:8000
environment:
# MYSQL_DATABASE: laravelvueblog_db
# MYSQL_ROOT_PASSWORD: root
# MYSQL_PASSWORD: root
# MYSQL_USER: admin
DB_HOST: db
DB_USER: admin
DB_PASSWORD: root
DB_NAME: laravelvueblog_db
WAIT_HOSTS: db:3306
depends_on:
- db
links:
- db
networks:
- appnetwork
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 3310:80
links:
- mysql
environment:
PMA_HOSTS: db
PMA_PORT: 3306
depends_on:
- db
networks:
- appnetwork
volumes:
mysql_file:
driver: local
networks:
appnetwork:
driver: bridge
After several attempt I found the problem, since all the services are on the same network (appnework)
I used the internal port in my laravel .env file like below
DB_PORT=3306
And now it works fine
I have the following php service in docker-compse.yml
version: '3'
networks:
laravel:
driver: bridge
services:
nginx:
image: nginx:stable-alpine
restart: unless-stopped
ports:
- "${WEB_PORT}:80"
volumes:
- "${PROJECT_DIR}:/var/www/html"
- "${NGINX_CONFIG}:/etc/nginx/conf.d/default.conf"
- ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.29
restart: unless-stopped
user: "${HOST_UID}:${HOST_GID}"
tty: true
ports:
- "${SQL_PORT}:3306"
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker/mysql:/var/lib/mysql
networks:
- laravel
php:
build:
context: ./docker
dockerfile: Dockerfile-php
user: "${HOST_UID}:${HOST_GID}"
volumes:
- "${PROJECT_DIR}:/var/www/html"
- ./docker/php/php.ini:/usr/local/etc/php/php.ini
#- "${COMPOSER_CACHE_DIR}:/.composer/cache"
#- "${COMPOSER_CONFIG}:/.composer/config"
working_dir: /var/www/html
networks:
- laravel
npm:
image: node:13.7
user: "${HOST_UID}:${HOST_GID}"
volumes:
- "${PROJECT_DIR}:/var/www/html"
working_dir: /var/www/html
entrypoint: ['npm']
When I run whoami in the container, it returns:
whoami: cannot find name for user ID 1000
I think this is a problem because there is no home directory, docker-compose exec php ls ~ returns:
ls: cannot access '/home/clarg': No such file or directory
This then leads to docker-compose exec php php artisan tinker returning:
ErrorException
Writing to directory /.config/psysh is not allowed.
at vendor/psy/psysh/src/ConfigPaths.php:362
358▕ #\mkdir($dir, 0700, true);
359▕ }
360▕
361▕ if (!\is_dir($dir) || !\is_writable($dir)) {
➜ 362▕ \trigger_error(\sprintf('Writing to directory %s is not allowed.', $dir), \E_USER_NOTICE);
363▕
364▕ return false;
365▕ }
366▕
+20 vendor frames
21 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
From googling, I see this is in the home directory, which does not exist in the container.
How can I solve this?
EDIT:
Dockerfile-php:
FROM php:8.0-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 gd zip pdo_mysql
# check https://github.com/mlocati/docker-php-extension-installer#supported-php-extensions for more extensions
COPY --from=composer /usr/bin/composer /usr/bin/composer
php.ini
https://pastebin.com/T2iYTZz2
Your docker containers don't have any knowledge of the users that may or may not exist on the host machine, so unless you've built those in with their accompanying config and directory structure the only thing you're getting out of feeding docker your local UID and GID is "running the container as something other than root", which is good.
But generally you don't want to tie a docker container/image to the particular environment that it is launched from, eg: requiring a user with the same name as your local user exist within the container, plus all of its associated directories and such.
In this specific case it looks like artisan just wants to cache some config, and you can control where that lands with the environment variable:
XDG_CONFIG_HOME=/some/writeable/directory
Which you could set in the Dockerfile, docker-compose, or .env file of your project. I would suggest setting it to somewhere in your project directory, but outside of the docroot.
Ref: https://stackoverflow.com/a/62041096/1064767
This is well enough for local dev where you want to mount in your local work dir for testing, but will likely need a bit more consideration if you're going to build/deploy a final docker image.
I'm trying to run a Laravel app in my local environment via Docker. I want to setup separate containers for each service i.e. Apache, PHP, MySQL. I also want to keep Composer, Artisan and PHPUnit in separate containers as well. This is more for neatness than anything else.
All the containers spin up with no issues and I can access each one no problems via the 'docker-compose exec [container name] /bin/sh' command.
The problem I'm having is that the index.php in the public folder is not being executed correctly. Apache is just serving up the file contents.
I can't figure out what I'm doing wrong. I've tried using an Nginx container instead of Apache but I get the same issue. I'm guessing my Apache container does not recognize my PHP container.
Is there anything I'm doing wrong below?
My docker-compose.yml file is as follows:
version: '3.8'
networks:
cpw:
name: cpw_network
services:
apache:
build:
context: .
dockerfile: apache.dockerfile
container_name: cpw_apache
depends_on:
- php
- mysql
ports:
- 8080:80
- 8443:443
volumes:
- ./:/var/www/html
networks:
- cpw
php:
build:
context: .
dockerfile: php.dockerfile
container_name: cpw_php
volumes:
- ./:/var/www/html
networks:
- cpw
mysql:
image: mysql:5.7.32
container_name: cpw_mysql
environment:
MYSQL_DATABASE: cpw
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
networks:
- cpw
composer:
image: composer:latest
container_name: cpw_composer
volumes:
- ./:/var/www/html
working_dir: /var/www/html
networks:
- cpw
artisan:
build:
context: .
dockerfile: php.dockerfile
container_name: cpw_artisan
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: [ "php", "artisan" ]
networks:
- cpw
phpunit:
build:
context: .
dockerfile: php.dockerfile
container_name: cpw_phpunit
volumes:
- ./:/var/www/html
working_dir: /var/www/html
entrypoint: [ "/var/www/html/vendor/bin/phpunit" ]
networks:
- cpw
My apache.dockerfile is as follows:
FROM httpd:alpine
ADD ./apache/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
RUN sed -i 's,#Include conf/extra/httpd-vhosts.conf,Include conf/extra/httpd-vhosts.conf,g' /usr/local/apache2/conf/httpd.conf
RUN mkdir -p /var/www/html
My php.dockerfile is as follows:
FROM php:7.4.12-fpm-alpine
RUN mkdir -p /var/www/html
RUN apk --no-cache add shadow && usermod -u 1000 www-data
RUN docker-php-ext-install pdo pdo_mysql
My httpd-vhosts.conf is as follows:
<VirtualHost *:80>
ServerAdmin email#email.com
DocumentRoot /var/www/html/public
ServerName localhost
ServerAlias localhost
ErrorLog logs/localhost-error_log
CustomLog logs/localhost-access_log common
<Directory /var/www/html/public>
AllowOverride All
DirectoryIndex index.php index.php
Options -Indexes
Require all granted
</Directory>
</VirtualHost>
Any help appreciated.
Regards,
Stephen
Thanks for #NicoHaase for pointing me in the right direction.
This is the piece I was missing:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php-fpm-container:9000"
</FilesMatch>
More details here:
How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)
This might also fix:
<FilesMatch \.(?i:php)$>
SetHandler application/x-httpd-php
</FilesMatch>
i have tried creating a docker container on centos7 but
i could access phpmyadmin page but not index.php page
could you see the docker yml and docker file and tell what happened
like im running through kali linux to a centos7 docker its been difficult i have been trying since yesterday
the give code is docker-compose.yml
version: "3"
services:
www:
build: .
ports:
- "8001:80"
volumes:
- ./www:/var/www/html/
links:
- db
networks:
- default
db:
image: mysql:8.0.16
command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
ports:
- "8002:3306"
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- ./dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin:4.9
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: user
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
and the dockerfile
FROM php:7.1.19-apache
RUN docker-php-ext-install mysqli mbstring
i dont know what is the issue like when access localhost:8000 on browser
im getting forbidden access 403 error
i just need to access index.php when i enter localhost:8000 on browser
please help me out,
thank you in advance
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: