I'm working on a docker project and I want to run a php file which will configure the database for the project.
My Docker file :
FROM php:7.2-apache
LABEL maintainer="admin#ksoftlabs.com"
COPY site/ /var/www/html
RUN chmod -R 777 /var/www/html
RUN docker-php-ext-install mysqli
COPY 000-default.conf /etc/apache2/sites- available/000-default.conf
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
My docker-compose.yml
version: '3.1'
services:
db:
image: mysql
command:
--default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: group_db
server:
image : apache
ports:
- "80:80"
I want to run the install.php which will be in /var/www/html/ directory. How can I do that?
Edit : I want to run this file automatically after the container is up
Would running it earlier work?
After RUN docker-php-ext-install mysqli, put RUN php /path/to/your/file.php?
Related
I'm working on a project in Laravel and using Docker. I don't know why I suddenly get this error message from PhpStorm.
I ran sudo chmod 777 -R my-project-name but I keep getting the error message.
How can I fix that?
The error message:
Docker file:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . .
RUN composer install
CMD php artisan serve --host=0.0.0.0
docker-compose.yml file:
version: '3.8'
services:
backend:
build:
context: .
dockerfile: Dockerfile
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- db
db:
image: mysql:5.7.22
restart: unless-stopped
ports:
- 33066:3306
environment:
MYSQL_DATABASE: admin
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- ./mysql:/var/lib/mysql
When you run docker-compose
It will creat a project with ower is root or name for docker set. You will see lock icon in the folder. Current, you are only a visitor. But you always change files that you are not owner. How can do it?
When start you can run.
sudo chmod 777 -R my-project-name
But it is not good. Because everyone can change your file without permission.
Solution:
check permission your file:
getfacl [relate_path_your_file]/TestController.php or ls -l [relate_path_your_file]/TestController.php
[enter image description here][1]
[1]: https://i.stack.imgur.com/9T85A.png
Add your account to group of this project
sudo adduser {USER-NAME-HERE} {GROUP-NAME-HERE}
Add write permission for group user
sudo chmod g+w [relate_path_your_file]/TestController.php
Right now your group user can edit file, include you
If you want to change others file, redo step 3 with path your file.
If not work, reopen phpStom
My goal is to get php dependencies in one stage of a docker file then copy those dependencies to the next stage (the vendor/ dir). However, once a volume is specified in docker-compose.yml that overrides the COPY statement as if it never happened.
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: docker/app/Dockerfile
volumes:
- .:/var/www/html
docker/app/Dockerfile
FROM composer AS php_builder
COPY . /app
RUN composer install --no-dev
FROM php:7.1-fpm
COPY --from=php_builder /app/vendor /var/www/html/vendor
The result of building and running this is a /var/www/html directory that doesn't have the vendor/ directory as I'd expect.
My guess is that this is because the volume specified in the docker-compose.yml service definition actually happens after the COPY --from statement which seems to be logical. But how do I get around this? I'd still like to use a volume here instead of an ADD or COPY command.
You can combine bind mounts & volume to make your aim, a minimal example for your reference:
docker-compose.yaml:
version: "3"
services:
app:
build:
context: .
dockerfile: docker/app/Dockerfile
volumes:
- my_folder:/var/www/html/vendor
- .:/var/www/html
volumes:
my_folder:
docker/app/Dockerfile:
FROM composer AS php_builder
COPY . /app
#RUN composer install --no-dev
RUN mkdir -p vendor && echo "I'm dependency!" > vendor/dependencies.txt
FROM php:7.1-fpm
COPY --from=php_builder /app/vendor /var/www/html/vendor
Results:
shubuntu1#shubuntu1:~/test$ ls
docker docker-compose.yaml index.php
shubuntu1#shubuntu1:~/test$ docker-compose up -d
Creating network "test_default" with the default driver
Creating test_app_1 ... done
shubuntu1#shubuntu1:~/test$ docker exec -it test_app_1 /bin/bash
root#bf59d8684581:/var/www/html# ls
docker docker-compose.yaml index.php vendor
root#bf59d8684581:/var/www/html# cat vendor/dependencies.txt
I'm dependency!
From above execution, you can see the dependencies.txt which generated in the first stage of Dockerfile still could be seen in the container, volume just manage the data by docker itself, while bind mounts give you chance to manage data for yourself.
Forgive me if this is an obvious question. I am fairly new to docker and I am having trouble understanding the installation instructions here:
https://hub.docker.com/_/composer/
I want to use PHP Composer in my Limesurvey docker image that was generated with the following docker-compose "yml" file:
limesurvey-md:
image: mariadb
restart: always
ports:
- "32805:3306"
environment:
MYSQL_DATABASE: limesurvey
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: limesurvey
MYSQL_PASSWORD: password
volumes:
- limesurvey-db:/var/lib/mysql
- limesurvey-dblog:/var/log/mysql
- limesurvey-dbetc:/etc/mysql
limesurvey:
image: fjudith/limesurvey
restart: always
ports:
- "32705:80"
volumes:
- limesurvey-upload:/var/www/html/upload
links:
- limesurvey-md:mysql
What do I need to add to my yml file to accomplish this? If it helps, there is a directory called "application" in the Limesurvey image:
/var/www/html/application
And how do I give this composer a command while it is in the container? I am using Windows 10 and the docker container is running the default linux environment. fjudith's Limesurvey container is using the last 2.X branch of Limesurvey (the one right before 3.X) and it is running PHP 7.2
You can create a custom image with a dockerfile build, yo can specify the dockerfile name in the build section, the docker-compose.yml and dockerfile are in the same folder here i attach and example:
docker-compose.yml:
version: '3.1'
services:
limesurvey-md:
image: mariadb
restart: always
ports:
- 32805:3306
environment:
MYSQL_DATABASE: limesurvey
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: limesurvey
MYSQL_PASSWORD: password
volumes:
- limesurvey-db:/var/lib/mysql
- limesurvey-dblog:/var/log/mysql
- limesurvey-dbetc:/etc/mysql
limesurvey:
build:
context: .
dockerfile: dockerfile
restart: always
ports:
- 32705:80
volumes:
- limesurvey-upload:/var/www/html/upload
links:
- limesurvey-md:mysql
volumes:
limesurvey-db:
driver: local
limesurvey-dblog:
driver: local
limesurvey-dbetc:
driver: local
limesurvey-upload:
driver: local
dockerfile:
FROM "fjudith/limesurvey:latest"
LABEL maintainer="ing.brayan.cm#gmail.com"
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Thanks Brayan! I just (10 minutes ago) figured out another way, you can also do a bash command. Since I was in Windows the two lines I had to do in my command prompt were
docker exec -it <my_container_name> bash
Then it put me into "/var/www/html#" where I did the following command:
$sudo curl -o /tmp/composer-setup.php https://getcomposer.org/installer && curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig && php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" && php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --snapshot && rm -f /tmp/composer-setup.*
I adapted the second line from here: Get composer (php dependency manager) to run on a docker image build
From there it's easy! You can just do
composer
and it guides you through the possible commands. It suggests not using the "root" administrator. I'll have to look into creating another user in the docker image.
I've been attempting to containerize my php wev app (built on laravel) but I've run into a few issues
first I've found that the container exits after executing a command so I moved the building script to a different container (migrations and composer install)
I made a script called init.sh that contains the build instructions but whenever I run them from the command in docker-compose.yml I get no such file or directory.
this is my dockerfile
FROM nimmis/apache-php7
COPY apache_default /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite
RUN service apache2 restart
this is my docker-compose.ym
version: "3"
volumes:
db-data:
external: false
services:
db:
image: mysql/mysql-server
env_file: .env
volumes:
- db-data:/var/lib/mysql-server
migrations:
build: .
env_file: .env
volumes:
- .:/var/www/html/books
depends_on:
- app
command:
# - /var/www/html/books/artisan migrate
# - chmod 777 /var/www/html/init.sh
- /var/www/html/books/init.sh
# - /var/www/html/init.sh
environment:
- ALLOW_OVERRIDE=true
app:
build: .
env_file: .env
volumes:
- .:/var/www/html/books
ports:
- "8081:80"
depends_on:
- db
environment:
- ALLOW_OVERRIDE=true
the commented parts a different attempts to run the script correctly.
I'm trying to run it on windows 10 using docker toolbox
What am I doing wrong? and is this the correct approach?
The RUN directive will execute something in the container when the container is building, not when the container is executing.
To that end, your Dockerfile should look something more like this:
FROM nimmis/apache-php7
COPY apache_default /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite
ENTRYPOINT ["bash", "/path/to/init.sh"]
CMD ["apache2", "-DFOREGROUND", "-k", "start"]
The Entrypoint directive will run right before the CMD directive, irrespective if you pass a command into the container at boot.
So, you'll want to make init.sh execute the command the container passes to it:
#!/bin/bash
# init.sh
# todo: whatever init.sh does...
# Execute the CMD passed to the container:
exec "$#"
So I figured out what was wrong ... it was a permissions issue.
when I set the permissions to be executable it worked fine ...
so now all I had to do was adjust my dockerfile to be
FROM nimmis/apache-php7
COPY . /var/www/html/books
COPY start.sh /
RUN chmod +x /start.sh
COPY apache_default /etc/apache2/sites-available/000-default.conf
RUN a2enmod rewrite
RUN service apache2 restart
Instead of this
volumes:
- .:/var/www/html/books
use
volumes:
- folder_containg_sh:/var/www/html/books #relative path to script
in docker-compose file
I need to install cURL compiled with OpenSSL and zlib via Dockerfile for Debian image with apache and php 5.6. I tried many approaches but due to the fact that I don't have string understanding in Linux a failed. I use docker-compose to up my container. docker-compose.yaml looks like:
version: '2'
services:
web:
build: .
command: php -S 0.0.0.0:80 -t /var/www/html/
ports:
- "80:80"
depends_on:
- db
volumes:
- $PWD/www/project:/var/www/html
container_name: "project-web-server"
db:
image: mysql:latest
ports:
- "192.168.99.100:3306:3306"
container_name: "project-db"
environment:
MYSQL_DATABASE: dbname
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
MYSQL_ROOT_PASSWORD: dbpass
As a build script I use Dockerfile:
FROM php:5-fpm
RUN apt-get update && apt-get install -y \
apt-utils \
curl libcurl3 libcurl3-dev php5-curl php5-mcrypt
RUN docker-php-ext-install -j$(nproc) curl
'docker-php-ext-install' is a helper script from the base image https://hub.docker.com/_/php/
The problem is that after $ docker build --rm . which is successful a don't get an image with cURL+SSL+zlib. After $ docker-compose up I have a working container with Apache+MySQL and can run my project but libraries I need are not there.
Could you explain how to add these extensions to my apache in container properly? I even tried to create my own Dockerfile and build apache+php+needed libs there, but had no result.
Your Dockerfile is not complete. You have not done a COPY (or similar) to transfer your source code to execute from the host into the container. The point of a Dockerfile is to setup an environment together with your source code which finishes by launching a process (typically a server).
COPY code-from-some-location into-location-in-container
CMD path-to-your-server
... as per the URL you reference a more complete Dockerfile would appear like this
FROM php:5.6-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./your-script.php" ]
notice the COPY which recursively copies all files/dirs (typically the location of your source code, etc like data and/or config files) in your $PWD where you execute the command onto the specified location internal to the container In unix a period as in . indicates the current directory so above command
COPY . /usr/src/myapp
will copy all files and directories in current directory from the host computer (the one you are using when typing in the docker build command) into the container directory called /usr/src/myapp
the WORKDIR acts to change directories into your container's dir supplied
finally the CMD launches the server which hums along once your launch the container