I couldn't find a solution to this problem on the internet. I wrote a dockerfile file below.
FROM php:7.3.6-fpm
RUN docker-php-ext-install pdo_mysql
RUN apt-get update \
&& apt-get install -y sudo \
&& apt-get install -y \
curl \
sed \
zlib1g-dev \
git \
zip \
unzip \
nano
RUN cd ~
RUN sudo curl -sS https://getcomposer.org/installer | php -- --
install-dir=/usr/local/bin --filename=composer
RUN echo 'alias api="php api"' >> ~/.bashrc
RUN echo 'cd /var/www/html/app' >> ~/.bashrc
WORKDIR /var/www/html/app
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install
# Copy codebase
COPY . ./
running this code, installing dependencies with composer install doesn't seem to be a problem at all.
docker-compose up -d
and then
docker exec -it php /bin/bash
and then
cd app
vendor does not appear when I enter the directory with this command.I don't understand what the reason is. Can you help me how to solve the problem?
Related
I am trying to dockerize a PHP laravel app. I am using a PHP and a composer image to achieve this. However, when I run composer install, I get all my packages installed but then run into this error:
/app/vendor does not exist and could not be created.
I want composer to create the /vendor directory! Could this be a permission issue?
Here is my Dockerfile:
FROM php:7.4.3-cli
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
COPY --from=composer:2.4.4 /usr/bin/composer /usr/local/bin/composer
# Set working directory
WORKDIR /app
COPY . .
# Add a new user "john" with user id 8877
RUN useradd -u 8877 john
# Change to non-root privilege
USER john
RUN composer install
I created a user with an arbitrary ID since it's a bad practice to run composer install as root security-wise.
I was able to solve the problem by making some changes to my Dockerfile:
FROM php:7.4.3-cli
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
COPY --from=composer:2.4.4 /usr/bin/composer /usr/local/bin/composer
# Add a new user "john" with user id 8877
RUN useradd -u 8877 john
# Set working directory
WORKDIR /app
COPY . .
RUN chmod -R 775 /app
RUN chown -R john:john /app
# Change to non-root privilege
USER john
RUN composer install --no-scripts --no-plugins
I can't understand why this is happening. I am trying to create a symlink but it fails. It only works if I ssh into the container after it has been created. When I build my project using Docker I get this error message:
Step 11/14 : RUN php artisan storage:link
---> Running in bbfd87dcdbf6
Could not open input file: artisan
ERROR: Service 'php-container' failed to build: The command '/bin/sh -c php artisan storage:link' returned a non-zero code: 1
But if I try to ssh into my container and run the same command then it works.
$ docker exec -it php-container /bin/bash
root#053d9cbd22eb:/var/www# php artisan storage:link
The [/var/www/public/storage] link has been connected to [/var/www/storage/app/public].
The links have been created.
Here is my Dockerfile
FROM php:7.4-fpm
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
libzip-dev \
libmcrypt-dev \
libonig-dev \
zlib1g-dev \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
graphviz \
curl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql zip exif pcntl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg
RUN docker-php-ext-install gd
RUN docker-php-ext-install bcmath
# Permissions for Laravel
RUN chown -R www-data:www-data /var/www
RUN chmod -R 777 /var/www
# (!) This is not working.......
RUN php artisan storage:link
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD bash -c "composer install && chmod -R 777 /var/www && php artisan migrate --seed && php-fpm"
You must run composer install before php artisan command.
This is really important. you have error because artisan package not exist and actually vendor folder not created.
Finally you must change docker file to this :
CMD bash -c "composer install && chmod -R 777 /var/www && php artisan migrate --seed && php artisan storage:link
I'm trying to connect to mssql with PDO and Laravel but I think I'm having issues installing the driver. There are no build errors, but I've looked around everywhere to try to find a solution, and they produce either an error or just does not work. When attempting to make a connection to a mssql server, it gives this error:
Illuminate\Database\QueryException: could not find driver (SQL: SELECT * FROM SAMPLE_VIEW_SM1 WHERE STUDENTS_ID=1) in file /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
Caused by
PDOException: could not find driver in file /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php on line 70
Here is my dockerfile:
FROM php:7.3-fpm-buster
ENV DEBIAN_FRONTEND=noninteractive
RUN rm /etc/apt/preferences.d/no-debian-php
RUN apt-get update -y && apt-get install -y \
openssl \
zip \
unzip \
git \
curl \
freetds-common \
freetds-bin \
unixodbc \
unixodbc-dev \
php-smbclient \
php7.3-sybase
RUN docker-php-ext-configure pdo_odbc --with-pdo-odbc=unixodbc,/usr
RUN docker-php-ext-install pdo_odbc
RUN docker-php-ext-enable pdo_odbc
# Install node and dependencies
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
RUN mkdir /cache
WORKDIR /cache
COPY package.json ./
COPY package-lock.json ./
RUN npm install
# Set file size limits
RUN echo "post_max_size=50M" >> /usr/local/etc/php/php.ini-production
RUN echo "upload_max_filesize=10M" >> /usr/local/etc/php/php.ini-production
RUN echo "memory_limit=6400M" >> /usr/local/etc/php/php.ini-production
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# Create application's working directory
WORKDIR /var/www
COPY . .
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install
RUN cp -r ./vendor/. /cache/vendor/
RUN chmod -R a+rwX /var/www/storage
RUN chmod -R +x ./entrypoint.sh
EXPOSE 9000
ENTRYPOINT [ "./entrypoint.sh" ]
I just published a solution related to this, I hope it serves you, greetings.
https://stackoverflow.com/a/68536444/5639865
I am trying to dockerize a PHP / Laravel / VueJS app.
Here is the Dockerfile
FROM php:7.2-fpm
LABEL maintainer="contact#kendozone.com"
LABEL version="1.0.0"
LABEL description="Kendozone is a online tournament webapp coded with PHP / Laravel"
ENV node_version 10.8.0
ENV npm_version 6.4.1
ENV NVM_DIR /.nvm
ENV APP_DIR="/var/www"
ENV APP_PORT="80"
ENV DOCKER_FOLDER="docker/local"
RUN echo "deb http://ftp.de.debian.org/debian stretch main " >> /etc/apt/sources.list \
&& apt-get update -y
RUN apt-get install -y openssl zip unzip git gcc make automake \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libmagickwand-dev vim --no-install-recommends
RUN apt-get purge --auto-remove -y g++ \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install pdo pdo_mysql mbstring zip -j$(nproc) iconv -j$(nproc) gd
WORKDIR $APP_DIR
COPY . $APP_DIR
RUN ls $APP_DIR \
&& touch $APP_DIR/database/sqlite.db \
&& mv $DOCKER_FOLDER/.env.local .env \
&& chown -R www-data:www-data $APP_DIR
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer install --no-interaction
RUN mkdir -p $NVM_DIR && chown -R www-data:www-data $NVM_DIR \
&& curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash \
&& [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" \
&& nvm install ${node_version}
ENV NODE_PATH $NVM_DIR/v$node_version/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$node_version/bin:$PATH
RUN npm install --save-exact imagemin-pngquant#5.0.* \
&& npm install \
&& npm run production \
&& php artisan key:generate \
&& php artisan migrate --seed
EXPOSE $APP_PORT
CMD php artisan serve --host=0.0.0.0 --port=$APP_PORT
the last line is supposed to run local server
Then I build it with :
docker build . -t xoco/kendozone:local-1.0.3
Then I run it with:
docker run -p 80:80 xoco/kendozone:local-1.0.3 -d bash
I can see on terminal the output:
[25-Oct-2018 19:56:40] NOTICE: fpm is running, pid 1
[25-Oct-2018 19:56:40] NOTICE: ready to handle connections
EDIT:
➜ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fee456200b85 xoco/kendozone:local-1.0.3 "docker-php-entrypoi…" 2 hours ago Up 2 hours 9000/tcp, 0.0.0.0:8080->80/tcp youthful_keldysh
Which seems to mean that everything is ok
But I can't reach my app on 127.0.0.1
What am I missing
You have to add host configuration file while using docker and add an entry for it inside /etc/hosts and try again.
I solved it with this command:
docker run -p 80:80 xoco/kendozone:local-1.0.3
Just remove -d bash at the end, and it should be OK
I try to work out a way to create a dev environment using docker and laravel.
I have the following dockerfile:
FROM php:7.1.3-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev \
mysql-client libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install mcrypt pdo_mysql
&& chmod -R o+rw laravel-master/bootstrap laravel-master/storage
Laravel requires composer to call composer dump-autoload when working with database migration. Therefore, I need composer inside the docker container.
I tried:
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer
But when I call
docker-compose up
docker-compose exec app composer dump-autoload
It throws the following error:
rpc error: code = 13 desc = invalid header field value "oci runtime error: exec failed: container_linux.go:247: starting container process caused \"exec: \\\"composer\\\": executable file not found in $PATH\"\n"
I would be more than happy for advice how I can add composer to the PATH within my dockerfile or what else I can do to surpass this error.
Thanks for your support.
Also: this is the gitub repository if you need to see the docker-compose.yml file or anything else.
In Dockerfile :
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
I can install composer adding this line on my test dockerfile:
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Here is the dockerfile:
FROM php:7.1.3-fpm
RUN apt-get update && apt-get install -y libmcrypt-dev \
mysql-client libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install mcrypt pdo_mysql
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
It works for me, to test if the composer are installed i access to my container bash and execute:
composer --version
Composer version 1.6.5 2018-05-04 11:44:59
This is how i do it with Laravel 8.4 in 2021 to deploy it to CloudRun in Google Cloud:
Dockerfile
#Get Composer
FROM composer:2.0 as vendor
WORKDIR /app
COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install \
--no-interaction \
--no-plugins \
--no-scripts \
--no-dev \
--prefer-dist
COPY . .
RUN composer dump-autoload
// some more custom steps like
FROM node:14.9 as frontend
...
FROM php:7.4-fpm
...
// Copy Composer dependencies
# Copy Composer dependencies
COPY --from=vendor app/vendor/ ./vendor/
COPY . .
// Some more custom steps
...
End of my Dockerfile to launch app with cleared optimized cache
# Run Laravel commands
RUN php artisan optimize:clear
CMD php artisan serve --host=0.0.0.0 --port=8080
EXPOSE 8080
Create an executable of your composer file using
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer && chmod +x /usr/bin/composer
use composer in dockerfile using curl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Dockerfile
FROM 8.1.4-fpm
RUN apt-get update && apt-get install -y \
git \
curl \
zip \
unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www
We have basicly the same command running with the difference,
--install-dir=/usr/local/bin
Alternatively, you should add the composer bin files path to the $PATH variable.
export PATH=$PATH":/usr/bin"
I'd just like to suggest another way.
Dockerfile:
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
FROM php:7.3-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
RUN apk update
RUN apk upgrade
RUN apk add bash
RUN alias composer='php /usr/bin/composer'
My problem solved. I just stopped firewalld service on system