How to execute PHP artisan on Docker PHP container - php

I have set a Docker environment for a Laravel project with Docker-compose on a Windows 10 machine:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name : nginx
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image : mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: admin
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
I have also created a DockerFile:
FROM php:8.0-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
Everything is working fine. However, I've having difficulties understanding how to run artisan. From what I have seen online, I should run docker-compose exec php php /var/www/html/artisan .... But I'm getting this message from Docker:
`Could not open input file: C:/Users/lharr/AppData/Local/Programs/Git/var/www/html/artisan`
which makes sens since this path doesn't exist. But why is Docker going to this specific repository C:/Users/lharr/AppData/Local/Programs/Git/ ? and isn't the volumes defined in Docker-compose supposed to create /var/www/html/artisan repository ? How can I find out where PHP is stored and how can I properly execute artisan command inside this container ?

It turns out the solution to this problem was quite simple : No need to specify the full path to artisan while executing the command. Meaning, instead of docker-compose exec php php /var/www/html/artisan ..., docker-compose exec php php artisan is enough.

Related

How to run docker containers on another machine, like virtual box?

I am a new docker user, so some things may be not correctly explained.
I have a php project (on Windows 10), and I created docker containers and images for it. All project files, like html and css are located in src folder. Near src folder I created Dockerfile and docker-compose.yml, and they are working good. After that, I created tags to all my images, and uploaded them to dockerhub. I did that to be able to run my containers on another machine using images from repository. So, in virtual box I installed linux mint, and installed docker on it. Using console, I logged into my dockerhub account, and pulled all images from it. After that I copied my docker-compose.yml file from windows to linux on virtual box, and modiffied the path of images to take them directly from docker hub, and run docker-compose up command. Everything worked fine, all images were downloaded, but when I opened localhost:8000, I saw error 403 forbidden, and also I observed that automatically was created src folder, but it was empty. So, I think that error 403 appeared because there were nothing in src folder.
So, the question is: How to upload my src folder with all project files into docker image, to be able to use them on any other machines by using only docker-compose.yml and docker-compose up command (it is impoartant, because that was the condition) ?
On windows:
Docker file code:
FROM php:8.0-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y
docker-compose.yml code:
version: '3.8'
services:
php-apache-environment:
container_name: myPHProject
build:
context: ./
dockerfile: Dockerfile
depends_on:
- db
volumes:
- ./src:/var/www/html/
ports:
- 8000:80
db:
container_name: db
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: MYSQL_ROOT_PASSWORD
MYSQL_DATABASE: MY_DATABASE
MYSQL_USER: MYSQL_USER
MYSQL_PASSWORD: MYSQL_PASSWORD
ports:
- "9906:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- '8080:80'
restart: always
environment:
PMA_HOST: db
depends_on:
- db
docker-compose.yml that I used on linux(without dockerfile, it is important, because that was the condition):
version: '3.8'
services:
php-apache-environment:
container_name: myPHProject
image: <my dockerhub name>/project-lab4-php-apache-environment
build:
context: ./
depends_on:
- db
volumes:
- ./src:/var/www/html/
ports:
- 8000:80
db:
container_name: db
image: <my dockerhub name>/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: MYSQL_ROOT_PASSWORD
MYSQL_DATABASE: MY_DATABASE
MYSQL_USER: MYSQL_USER
MYSQL_PASSWORD: MYSQL_PASSWORD
ports:
- "9906:3306"
phpmyadmin:
image: <my dockerhub name>/phpmyadmin
ports:
- '8080:80'
restart: always
environment:
PMA_HOST: db
depends_on:
- db

Docker php:php7.4-apache boot-loop after installing mysqli extension

i am currently trying to run my Website in a Docker container using mysql and php with apache.
Docker-Compose:
version: '3.7'
services:
mysql:
image: mysql:latest
container_name: mysql
restart: always
environment:
//Database configuration variables
volumes:
- ./data/mysql/database:/var/lib/mysql
webserver:
image: php:7.4.12-apache
depends_on:
- mysql
restart: always
volumes:
- ./data/webserver:/var/www/html/
ports:
- 8888:80
command: bash -c "docker-php-ext-install mysqli && kill -HUP 1"
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin:latest
container_name: phpmyadmin
links:
- mysql:db
restart: always
ports:
- 8889:80
volumes:
- /sessions
The problem began after i added the command-block to the webserver-container. Without it, the container runs perfectly and i can access the website. But with the command, the container gets stuck in a boot-loop and it seems that it tries to run the command over and over. At least thats what i guess after looking at the log of the webserver container.
However when i use docker exec -it *webserver* bash and run the installation command directly in the container, it works perfectly. I then restart apache with kill -HUP 1 and the Website works as intended. Does anyone know what the problem is here?
Have you tried doing the install and apache restart inside a Dockerfile instead?
Something like:
FROM php:7.4.12-apache
RUN apt-get clean && apt-get update && apt-get install -y php7.4-mysqli;
RUN service apache2 restart;
Then your docker-compose could be:
[...]
webserver:
build:
context: .
dockerfile: docker/webserver/Dockerfile
depends_on:
- mysql
restart: always
volumes:
- ./data/webserver:/var/www/html/
ports:
- 8888:80
[...]

Docker: PhpMyAdmin has an upload limit of 2048KiB

i have a docker-compose.yml that looks like this:
webserver:
build: ./_docker/php
ports:
- 80:80
links:
- mysql
volumes_from:
- app
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: "${DB_NAME}"
MYSQL_USER: "${DB_USER}"
MYSQL_ROOT_PASSWORD: "${DB_ROOT_PW}"
MYSQL_PASSWORD: "${DB_PW}"
volumes:
- ./_docker/data/db:/docker-entrypoint-initdb.d
volumes_from:
- data
data:
image: mysql:5.7
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
links:
- mysql
environment:
PMA_HOST: mysql
app:
image: tianon/true
volumes:
- .:/var/www/public_html
Dockerfile looks like this:
FROM php:7.0-apache
#php:7.2.2-apache
#php:5.6.33-apache
COPY php.ini /usr/local/etc/php/
COPY 000-default.conf /etc/apache2/sites-available/
RUN a2enmod rewrite
RUN a2enmod expires
RUN a2enmod headers
RUN apt-get update
RUN apt-get install -y zlib1g-dev libxml2-dev libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng12.0 imagemagick
RUN docker-php-ext-install mysqli zip soap
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd
# Install opcache
RUN docker-php-ext-install opcache
and the php ini like this:
max_input_vars = 1500
max_execution_time = 300
post_max_size=50M
upload_max_filesize=50M
when i start the container i have my webserver located on http://localhost.
i put an index.php with a phpinfo(); inside it and it shows, that the php.ini works.
When i open http://localhost:8080 and login to PMA it shows me that my upload limit i set to 2048KiB.
Where can i change this?
Thanks in advance!
Use like this UPLOAD_LIMIT env in docker. This is from my docker-compose.yml. Default value for UPLOAD_LIMIT is 2048K which is 2048KiB. Setting value to 300M increases it to 300MiB.
Reference: https://github.com/phpmyadmin/docker#environment-variables-summary
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'php7.3-phpmyadmin'
restart: 'always'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
UPLOAD_LIMIT: 300M
ports:
- '8082:80'
volumes:
- /sessions
I implemented UPLOAD_LIMIT ENV variable in
https://github.com/phpmyadmin/docker/pull/261/files#diff-80edf79b0f382a3c6e871ac209ffd6abR57
you need to use UPLOAD_LIMIT inside enviroment, and you need to specify values with = sign, like example: UPLOAD_LIMIT=300M
version: '3.1'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: somerandompasswordgenerated
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
- UPLOAD_LIMIT=300M
UPLOAD_LIMIT: 500M
mariadb:
image: mariadb:latest
container_name: w-mariadb
restart: always
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: db_name
MYSQL_USER: root
MYSQL_PASSWORD: db_password
MYSQL_ROOT_PASSWORD: db_password
volumes:
- 'w-mariadb:/var/lib/mysql'
networks:
- w-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: w-phpmyadmin
links:
- mariadb
environment:
MYSQL_ROOT_PASSWORD: db_password
PMA_HOST: mariadb
UPLOAD_LIMIT: 500M
restart: always
ports:
- '8383:80'
networks:
- w-network
In my case I couldn't fix it with UPLOAD_LIMIT, so without further debugging I needed a quick solution even if it's temporary:
open phpmyadmin container terminal: docker exec -it container_name bash
If you don't have vim or nano editor install one: apt-get update, apt-get install vim
create config file: vi /usr/local/php/conf.d/myconf.ini
with this content: post_max_size=50M upload_max_filesize=50M
restart container
Remember these changes will be gone when container is recreated, it's just a temporary solution. Try working with UPLOAD_LIMIT as suggested in previous answer.
UPDATE
Tried again with setting upload_limit environment, but still without luck, so found another solution:
created a file say uploads.ini with this content:
post_max_size=50M
upload_max_filesize=50M
and link it to container volume :
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- mysql:db
ports:
- 8084:80
volumes:
- ./uploads.ini:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
environment:
MYSQL_ROOT_PASSWORD: something
#UPLOAD_LIMIT: 3000000000 <-- didn't work
the following worked very well.
My docker-compose.yml contains this:
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: some-name
env_file:
- phpmyadmin.env
depends_on:
- mariadb
volumes:
The following entry was added to the phpmyadmin.env: UPLOAD_LIMIT=256MB
The larger number (Maximal: 256MiB) showed up right after the container was brought down and was back up again.
If you want to install from phpmyadmin docker image and with docker run, you can exec this.
docker run --name your-image-name -d --link mysql-docker:db --env "UPLOAD_LIMIT=256M" phpmyadmin
This works for me and enable upload to 256MB SQL file.
I edit this file :
/usr/local/etc/php/conf.d/phpmyadmin-misc.ini
and set new values :
post_max_size=64M
upload_max_filesize=64M
It works fine.

Docker PHP Composer

I've the following docker-compose file, and need some help with PHP composer part commented below:
version: '3'
services:
proxy:
image: jwilder/nginx-proxy
container_name: proxy
ports:
- '80:80'
- '443:443'
volumes:
- './certs:/etc/nginx/certs'
- '/var/run/docker.sock:/tmp/docker.sock:ro'
restart: always
web:
image: 'nginx:latest'
container_name: nginx
volumes:
- './volume1:/volume1'
- './volume2:/volume2'
- './volume3:/volume3'
- './site.conf:/etc/nginx/conf.d/site.conf'
environment:
- 'VIRTUAL_HOST=host1.local,host2.local,host3.local'
restart: always
php:
build: .
container_name: php
volumes:
- './volume1:/volume1'
- './volume2:/volume2'
- './volume3:/volume3'
restart: always
# Start How TODO this?
composer:
image: 'composer:latest'
container_name: composer
command: install
volumes:
- './volume1:/app'
- './volume2:/app'
- './volume3:/app'
# End HOW TODO this?
db:
image: mariadb
container_name: mariadb
ports:
- '3306:3306'
environment:
- MYSQL_ROOT_PASSWORD=toor
volumes:
- './db:/var/lib/mysql'
restart: always
pma:
image: phpmyadmin/phpmyadmin
container_name: pma
environment:
- PMA_ARBITRARY=1
- 'PMA_ABSOLUTE_URI=https://pma.local/'
- VIRTUAL_HOST=pma.local
restart: always
I've multiple app that needs to use composer, but I can't overwrite /app folder inside the composer container. Should I write a Dockerfile inside each single app folder? I don't want to specify the full path of PHP app inside the docker-compose, because I can have multiple version of an app (like 1.0, 2.0, ecc.ecc.).
Instead of putting the Composer configuration in your Docker Compose file, you should probably just run it once for each PHP app before you run the system.
docker run --rm -v $(pwd)/volume1:/app composer:latest install
This will run composer and bind the directory to your host filesystem so that the vendor folder will be available.

docker compose - add python to LAMP

In linux I build a blog application that is based on mysql, apache2 and php. But I use some python for it. The python is for adapting python Pygments that is described there https://davidwalsh.name/pygments-php-wordpress .
I prepared docker compose stage that works fine but python does not work in it. How to add python to docker compose?
Here are my files:
root#debian:/usr/local/share/a22php7m55# cat docker-compose.yml
version: "2.1"
services:
apachephp:
build: ./a22php7/
ports:
- 8888:80
volumes:
- "/etc/passwd:/etc/passwd:ro"
- "/etc/group:/etc/group:ro"
- "${PROJECT_ROOT}:/var/www/html"
networks:
- database
- server
depends_on:
- mysql
container_name: ap47
mysql:
image: mariadb:5.5
volumes:
- ${MYSQL_DATA}:/var/lib/mysql
networks:
- database
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_DATABASE: "${MYSQL_NAME}"
MYSQL_USER: "${MYSQL_USERNAME}"
MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
container_name: maria47
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
networks:
- database
depends_on:
- mysql
environment:
PMA_HOST: mysql
container_name: pma47
volumes:
mariadb:
networks:
database:
server:
root#debian:/usr/local/share/a22php7m55# cat a22php7/
root#debian:/usr/local/share/a22php7m55# cat a22php7/Dockerfile
FROM php:7.1.3-apache
RUN docker-php-ext-install pdo pdo_mysql
root#debian:/usr/local/share/a22php7m55#
Looking at the base image for php:7.1.3-apache we see it's built on debian:jessie so we can install python, pip and pygments by adding these lines to your Dockerfile:
RUN apt-get update && apt-get install -y python python-pip
RUN pip install pygments

Categories