I'm trying to set up my symfony project in docker, and i'M struggling building the php container.
I've put all my source code in \var\www\app\
Here is an extract of my docker-compose.yml:
php:
build:
context: .
dockerfile: docker/php/Dockerfile
volumes:
- '/var/www/app:/var/www'
restart: on-failure
env_file:
- .env
user: 1000:1000
nginx:
image: nginx:1.19.0-alpine
restart: on-failure
volumes:
- '/var/www/app:/var/www'
- './docker/nginx/app.conf:/etc/nginx/conf.d/app.conf:ro'
ports:
- '80:80'
depends_on:
- php
here's the Dockerfile:
FROM composer:2.0 as composer
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN pecl install apcu
RUN apt-get update && \
apt-get install -y \
libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-enable apcu
WORKDIR /var/www
COPY --chown=1000:1000 var/www/app /var/www
RUN PATH=$PATH:/usr/src/app/vendor/bin:bin
RUN composer install --no-scripts --prefer-dist \
&& rm -rf "$(composer config cache-dir)" "$(composer config data-dir)"
and here's the error I get while trying to build the php container:
Step 9/11 : COPY --chown=1000:1000 var/www/app /var/www
ERROR: Service 'php' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder883740312/var/www/app: no such file or directory
The problem, I guess, is that it tries to fetch file within the docker build folder instead of /var/www/app. I thought it was because of the context, and I've tried to change it but then it cannot find my Dockerfile anymore.
I don't see how to resolve this, yyet I have the feeling it is a veryy easy one...But I'm quite lost at the moment.
Thanks!
You try to COPY a folder at build time and then at runtime you replace the same by using a VOLUME in your docker-compose. Maybe the volume is not needed?
Regarding the error, docker expects your var folder to be at the same level as docker-compose.yml. If that is not the case and your var folder is in docker/php/, then that is what you have to set as context in your docker-compose.yml.
php:
build:
context: docker/php
dockerfile: Dockerfile
If the Dockerfile is in the context, you don't even need to declare it explicitly in the docker-compose.yml
Related
I installed laravel using composer inside of my docker container, however, i get this error when i try to access the index page
The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log:
I am using laravel 9, i have tried all possible solution i could find and it doesn't work.
I run on windows 10.
Here is my Dockerfile
FROM php:8.0.2-fpm
# Install dependencies for the operating system software
RUN apt-get update && apt-get install -y \
git \
curl \
zip \
unzip
# Install extensions for php
RUN docker-php-ext-install pdo_mysql
# Install composer (php package manager)
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Set working directory
WORKDIR /var/www
I have adding the below code to my Dockerfile
# Assign permissions of the working directory to the www-data user
RUN chown -R www-data:www-data \
/var/www/storage \
/var/www/bootstrap/cache
but when try to build the image again with the code in it, i get an error message saying
directory doesn't exist
Here is my docker-compose.yml file
version: '3.8'
services:
php:
build:
context: ./
dockerfile: Dockerfile
container_name: jaynesis-php
restart: always
working_dir: /var/www
volumes:
- ../src:/var/www
nginx:
image: nginx:latest
container_name: jaynesis-nginx
restart: always
ports:
- "8000:80"
volumes:
- ../src:/var/www
- ./nginx:/etc/nginx/conf.d
mysql:
container_name: jaynesis-db
image: mysql:8.0
volumes:
- ./storage/mysql:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
If you have an error: "The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log:"
Delete this laravel.log file.
Give this a go, it might work...
docker exec -it jaynesis-php bash
chmod 755 storage/ -R
This makes the storage directory a little more writeable.
I'm new to Docker and want to build the image myself (nginx + php-fpm + mariadb + phpadmin).
I want to have Xdebug so I've a Dockerfile to customize the php-fpm image.
And then I run into a problem (same as here) when I execute
docker-compose --build
or
docker-compose up --build
The image was built ok first time, but for next times it fails because the php-fpm somehow ended up with already installed xdebug:
+ pecl install xdebug
pecl/xdebug is already installed and is the same as the released version 3.1.5
install failed
It is like the image comes from cache but Dockerfile is still applied.
I've used the solution from that post but I am clearly missing something, it should not be like that. What am I doing wrong?
My docker-compose:
version: "3.7"
services:
web:
image: nginx:1.17
ports:
- 8080:80
volumes:
- ../logs:/var/log/nginx/
- ../wordpress:/var/www/myapp
- type: bind
source: ./site.conf
target: /etc/nginx/conf.d/default.conf
depends_on:
- php
- mariadb
php:
image: php:7.4-fpm
build:
context: ./php
volumes:
- ../wordpress:/var/www/myapp
- type: bind
source: ./php/conf.d/xdebug.ini
target: /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- type: bind
source: ./php/conf.d/error_reporting.ini
target: /usr/local/etc/php/conf.d/error_reporting.ini
depends_on:
- mariadb
mariadb:
image: mariadb:10.4
restart: always
ports:
- 127.0.0.1:3308:3306
environment:
- MYSQL_ROOT_PASSWORD=4321
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=1234
volumes:
- mariadb-data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- 8900:80
environment:
- PMA_ARBITRARY=1
- PMA_HOST=mariadb
depends_on:
- mariadb
volumes:
mariadb-data:
and my php-fpm dockerfile:
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
libicu-dev\
openssl \
git \
unzip\
nano\
&& docker-php-ext-install \
intl pdo pdo_mysql mysqli\
&& docker-php-ext-enable \
intl pdo pdo_mysql
RUN bash -c '[[ -n "$(pecl list | grep xdebug)" ]]\
|| (pecl install xdebug && docker-php-ext-enable xdebug)'
Discussion/Explanation
What am i doing wrong?
Perhaps it is a misunderstanding of the docker-compose.yml template, specifically in regards of the container for the php service (based on php:7.4-fpm) which might have been introduced in error.
When you specify both the image and the build attribute of a service, the image as you build it will be stored under the image name (see also later References section).
As you name the image as php:7.4-fpm and those build images are stored locally (there where you run docker-compose(1)) and you have in your Dockerfile within that build context that image same name php:7.4-fpm:
FROM php:7.4-fpm
# ...
Then when you build for the first time, the image php:7.4-fpm is not available locally. Then it is build from Docker Hub fetched php:7.4-fpm and the build result is then overwriting the image php:7.4-fpm locally as you are using the same name (!).
This is most likely not intended.
This can also explain why --build --force-recreate does not work either: The Dockerfile build instructions consider php:7.4-fpm to be the image to be built from but it is already the result (of the first local build).
Solution
Remove the image service keyword. You don't need it as you have the build keyword. Docker Compose will take care and name the image within your project automatically (based on service- and project name).
services:
php:
build:
context: ./php
volumes:
- ../.:/var/www/myapp:ro
Then remove the dangling/tainted php:7.4-fpm image:
$ docker image rm php:7.4-fpm
These two steps should already solve your issue and get your docker composition up without errors.
Additionally you can remove the workaround and do a plain pecl install xdebug && docker-php-ext-enable xdebug, here with better debugging abilities for the build (set -ex):
FROM php:7.4-fpm
RUN set -ex ;\
apt-get update ;\
apt-get install -y \
libicu-dev\
openssl \
git \
unzip \
nano \
;\
docker-php-ext-install intl pdo pdo_mysql mysqli ;\
docker-php-ext-enable intl pdo pdo_mysql ;\
:
RUN set -ex ;\
pecl install xdebug ;\
docker-php-ext-enable xdebug ;\
:
You could further tweak this by making the FROM image a build argument, bind it in the docker-composer.yml template (and use variables and defaults there-in, too). I'll leave that to your liking.
References
If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image:
build: ./dir
image: webapp:tag
This results in an image named webapp and tagged tag, built from ./dir.
From: https://docs.docker.com/compose/compose-file/compose-file-v3/#build
When starting the containers, you will need to instruct docker-compose to rebuilt the dockerfile.
Try this:
docker-compose up --build --force-recreate
I need to call composer install and unsure from which Dockerfile to call it from - Dockerfile-apache or Dockerfile-php-fpm?
Should I install composer in Dockerfile-apache (and PHP CLI?) and run it from there?
Running composer install from Dockerfile-php-fpm gives me this: Composer could not find a composer.json file in /var/www/html
Docker-php-fpm
# Do we target a specific version? ie. 7.4.25?
FROM php:7.4-fpm
# Need to add zip and other extensions
RUN buildDeps=" \
libonig-dev \
libzip-dev \
libxml2-dev \
" \
&& apt-get -y update \
&& apt-get install -y $buildDeps zip libicu-dev \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl mbstring json mysqli opcache pdo pdo_mysql xml \
&& apt-get purge -y --auto-remove $buildDeps \
&& rm -r /var/lib/apt/lists/*
# Copying of base conf files
COPY docker/php-fpm/conf/php-fpm.conf /usr/local/etc
COPY docker/php-fpm/conf/php.ini-development /usr/local/etc/php/php.ini
COPY docker/php-fpm/conf/www.conf /usr/local/etc/php-fpm.d
# Install Composer.
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/ \
&& ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
EXPOSE 9000
Docker-apache:
FROM httpd:2.4
# Copy config files
COPY docker/apache/conf/httpd.conf /usr/local/apache2/conf
COPY docker/apache/conf/httpd-vhosts.conf /usr/local/apache2/conf/extra
# Do something about log rotate?
# Create vhost directory
WORKDIR /var/www/html
#Set our application folder as an environment variable
ENV APP_HOME /var/www/html
#copy files
COPY bin ${APP_HOME}/bin
COPY config ${APP_HOME}/config
COPY plugins ${APP_HOME}/plugins
COPY src ${APP_HOME}/src
COPY webroot ${APP_HOME}/webroot
COPY .htaccess ${APP_HOME}
COPY index.php ${APP_HOME}
COPY composer.json ${APP_HOME}
COPY composer.lock ${APP_HOME}
Edit #1
docker-compose.yml
version: "3.2"
services:
php-fpm:
container_name: php-fpm
build:
context: .
dockerfile: Dockerfile-php-fpm
networks:
- backend
ports:
- "9000:9000"
apache:
container_name: httpd
build:
context: .
dockerfile: Dockerfile-apache
depends_on:
- php
networks:
- frontend
- backend
ports:
- "80:80"
- "443:443"
networks:
frontend:
backend:
Edit #2
This docker-compose file enables the communication via a common volume. I just need to compile, build and copy the files in Apache at this point.
version: "3.9"
services:
php-fpm:
container_name: php-fpm
build:
context: .
dockerfile: Dockerfile-php-fpm
volumes:
- mydata:/var/www/html:rw
networks:
- backend
ports:
- "9000:9000"
apache:
container_name: httpd
build:
context: .
dockerfile: Dockerfile-apache
depends_on:
- php
volumes:
- mydata:/var/www/html:rw
networks:
- frontend
- backend
ports:
- "80:80"
- "443:443"
networks:
frontend:
backend:
volumes:
mydata:
I would go with neither of the above; instead, run composer install locally and copy the resulting vendor directory as part of your application.
Fetching dependencies is fundamentally part of building an application, not part of running it, so Composer shouldn't even be installed on a production host or container. If you were writing a C application which needed to be compiled with gcc, you would run that as an earlier step, and then copy the binary into the container; composer install can be treated the same way.
So for instance, you might have a build script (to run manually, or in a CI server like Jenkins, Github Actions, Azure DevOps, etc) that went through the following steps:
Clone the repo from a git repository
Check out the latest tag
Run composer install
Run a script to minify the client-side JS
Run docker-composer, copying the source code, minified JS, and vendor directory
The software inside the Docker container therefore only needs to be able to run the application, not build it.
docker-compose.yml
version: '3.7'
services:
php:
build:
context: .
dockerfile: Dockerfile
image: php:7.3-rc-fpm
container_name: php_7.3-rc-fpm
volumes:
- .:/var/www/app
restart: unless-stopped
working_dir: /var/www
stdin_open: true
tty: true
Dockerfile
FROM php:7.3-rc-fpm
RUN apt-get update && apt-get install -y \
build-essential \
mysql-client \
locales \
zip \
vim \
unzip \
git \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip pcntl
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
Started containers with
docker-compose up -d
and when I execute
docker-compose exec php bash
followed by
mysql --version
result in
bash: mysql: command not found
the mysql-client is missing and the others RUNs installation as well...
Any idea what is going on?
....and stackoverflow need more details to approve my edit when I don't have any ............
image: php:7.3-rc-fpm should be dropped.
It tells docker-compose to build from the "php_7.3-rc-fpm" image and not from the image build with your Dockerfile (it's a question of precedence). So it's normal that nothing you ask to install in the Dockerfile is available...
I tested to be sure and indeed, if you drop this line, the commands docker-compose exec php bash followed by mysql --version gives you what you expected.
You are misusing container image name. In your docker-compose.yml you tell:
services:
php:
build:
context: .
dockerfile: Dockerfile
image: php:7.3-rc-fpm
That you want to build your own image and name it php:7.3-rc-fpm! But this is not your image' name - it is the name of a well-known php docker container! And in your Dockerfile you inherits from it:
FROM php:7.3-rc-fpm
So, you are overwriting public image but your own. And I can only guess, what will be the new image like..
Solution - remove image from your docker-compose file. It is not the image to be used, it is the name you want to give your image after being built, upon used in conjunction with build properties.
Trying to make docker container for my project and after using docker-compose up --build command I get this error: Composer could not find a composer.json file in /var/www/html.
My Dockerfile:
FROM php:7.2-apache
RUN docker-php-ext-install \
pdo_mysql \
&& a2enmod \
rewrite
.env file
#PATHS
DB_PATH_HOST=./databases
APP_PATH_HOST=./wedding-planner
APP_PATH_CONTAINER=/var/www/html
part of docker-compose.yml
version: '3'
services:
web:
build: ./web
environment:
- APACHE_RUN_USER=www-data
[...]
composer:
image: composer:1.6
volumes:
- ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
working_dir: ${APP_PATH_CONTAINER}
command: composer install
Where should I put composer.json to make it work?
I don't think your docker-compose.yml file is replacing the variables properly.
You can try something like this to export the variables for replacement:
set -a
. .env
set +a
docker-compose up -d --build