Docker PHP Composer - php

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.

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

How to execute PHP artisan on Docker PHP container

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.

Installing openssl in php 5.3 docker container

I have a php project with docker
The docker-compose file is as shown below
version: "3.1"
services:
php:
image: php:5.3-apache
restart: always
ports:
- 5030:80
volumes:
- ./:/var/www/html
# - /home/asish/Work/CodePoint/php/php.ini:/usr/local/lib/php.ini
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "5001:3306"
environment:
MYSQL_ROOT_PASSWORD: password
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8000:80
environment:
- PMA_ARBITRARY=1
- UPLOAD_LIMIT=300000000
I need to install openssl extension and activate it. I tried mapping a php.ini file with openssl extension enabled but it is not enabled in the server.
How to install and activate the openssl extension in this case

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

How to insert dockerised cron process in a docker-compose built LEMP stack?

I have a LEMP stack which is built by this docker-compose file:
cadvisor:
image: google/cadvisor:latest
container_name: lemp_cadvisor
ports:
- "8080:8080"
volumes:
- "/:/rootfs:ro"
- "/var/run:/var/run:rw"
- "/sys:/sys:ro"
- "/var/lib/docker/:/var/lib/docker:ro"
base:
build: ./base
container_name: lemp_base
volumes:
- /home/core/server-lemp/www/:/var/www/:rw
phpmyadmin:
build: ./phpmyadmin
container_name: lemp_phpmyadmin
links:
- base
volumes:
- /var/www/phpmyadmin
- ./phpmyadmin/var/www/phpmyadmin/config.inc.php:/var/www/phpmyadmin/config.inc.php:rw
mariadb:
build: ./mariadb
container_name: lemp_mariadb
environment:
- MYSQL_ROOT_PASSWORD=pwd
links:
- base
volumes:
- /var/run/mysqld
- /home/core/server-lemp/mariadb/:/var/lib/mysql/:rw
- ./mariadb/etc/mysql/my.cnf:/etc/mysql/my.cnf:ro
ffmpeg:
build: ./ffmpeg
container_name: lemp_ffmpeg
links:
- base
volumes:
- /usr/ffmpeg
cron:
build: ./cron
container_name: lemp_cron
links:
- base
volumes:
- /etc/cron.weekly
- /etc/cron.d
- /etc/cron.hourly
- /etc/cron.daily
- /etc/cron.monthly
php:
build: ./php
container_name: lemp_php
links:
- base
volumes:
- /var/run/php-fpm
- ./php/usr/local/php7/etc/php-fpm.conf:/usr/local/php7/etc/php-fpm.conf:ro
- ./php/usr/local/php7/etc/php.ini:/usr/local/php7/etc/php.ini:ro
- ./php/usr/local/php7/etc/php-fpm.d/www.conf:/usr/local/php7/etc/php-fpm.d/www.conf:ro
volumes_from:
- base
- phpmyadmin
- mariadb
- ffmpeg
- cron
nginx:
build: ./nginx
container_name: lemp_nginx
links:
- base
ports:
- "80:80"
- "443:443"
volumes:
- /var/cache/nginx
- ./nginx/etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
volumes_from:
- php
My ./cron/Dockerfile looks like this:
# Lanti/lempCron
#
# VERSION 1.0.0
FROM lemp_base:latest
MAINTAINER XY <info#domain.com>
LABEL Description="Cron" Vendor="XY" Version="1.0"
RUN apt-get -y update && apt-get -y dist-upgrade \
&& apt-get -y install \
cron
RUN rm -rf /var/lib/apt/lists/*
CMD ["cron", "-f"]
When in a Wordpress install I inspecting running cron jobs with WP Crontrol plugin, I got the following error message:
There was a problem spawning a call to the WP-Cron system on your site.
This means WP-Cron events on your site may not work. The problem was:
Failed to connect to 127.0.0.1 port 80: Connection refused
I assume because of the same error that causing this, the Cache Purge option in the Nginx-helper plugin also not working.
Wordpress is presumably running in your 'php' container, and the "wp-cron" function is presumably working there. Although it is "cron-like", it is actually part of Wordpress.
You have defined no ports for your `php' container, so it appears that you need to update your Docker networking so that wp-contain can access port 80 on the correct host.

Categories