I am trying to run symfony using docker. This is my dockerfile
# Dockerfile
FROM php:7.2-cli
RUN apt-get update -y && apt-get install -y libmcrypt-dev
ENV COMPOSER_ALLOW_SUPERUSER=1
# install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer --ansi --version --no-interaction
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& composer --ansi --version --no-interaction
RUN docker-php-ext-install pdo mbstring
WORKDIR /app
COPY . /app
# RUN composer install --no-scripts --no-autoloader --ansi --no-interaction
EXPOSE 8000
CMD php -S localhost:8000 -t public 0.0.0.0:8000
After I build and run $ docker run -it -p 8000:8000 symfony-tutorial I get following response
D:\docker_test\symfony-4-tutorial>docker run -it -p 8000:8000 symfony-tutorial
PHP 7.2.34 Development Server started at Wed Jan 11 16:40:32 2023
Listening on http://localhost:8000
Document root is /app/public
Press Ctrl-C to quit.
But on browser, its empty.
This page isn’t workinglocalhost didn’t send any data. ERR_EMPTY_RESPONSE. There is nothing in source and network tab
Your web server binds to localhost, which means that it'll only accept connections from inside the container.
You need to change
CMD php -S localhost:8000 -t public 0.0.0.0:8000
to
CMD php -S 0.0.0.0:8000 -t public 0.0.0.0:8000
Related
I have successfully deployed a laravel application with horizon installed to AWS ECS. However horizon has remained Inactive and I am stuck trying to get it to work as my emails are getting queued and never sent. This is my docker file
FROM php:8.1.4-fpm-alpine3.14
RUN apk update
RUN apk add --no-cache git libzip-dev zip unzip php8-exif supervisor
RUN mkdir -p /usr/src/php/ext/redis; \
curl -fsSL https://pecl.php.net/get/redis --ipv4 | tar xvz -C "/usr/src/php/ext/redis" --strip 1; \
docker-php-ext-install redis;
RUN docker-php-ext-install pdo pdo_mysql zip exif \
&& curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY horizon.conf /etc/supervisor/conf.d/horizon.conf
COPY . .
RUN chown -R www-data:www-data /var/www/html
RUN composer install --ignore-platform-req=ext-pcntl
RUN chmod 544 startup.sh
CMD ["/usr/bin/supervisord"]
ENTRYPOINT ["./startup.sh"]
I created a supervisord.conf file with this content in it
[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D
a horizon.conf file with its content below
[program:horizon]
process_name=%(program_name)s
command=php /var/www/html/artisan horizon
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/var/log/horizon.log
stopwaitsecs=3600
and this is what my startup script startup.sh looks like
#!/bin/sh
php artisan migrate
php artisan db:seed
php artisan config:cache
php artisan route:cache
php artisan horizon
php artisan queue:restart
php artisan serve --host=0.0.0.0 --port 80
I am not exactly very knowledgable with supervisor on containers so I need help figuring out why I am not able to get horizon to be active and fix it.
I am stuck with that problem.
I have a docker compose file with volumes mapping that way :
volumes:
- ./:/var/www/html:rw
- ../epossobundle/:/var/www/epossobundle :rw;
In composer.json, there is a repo linked on a bundle which I am working on it like this :
"repositories": [
{
"type": "path",
"url": "../epossobundle"
},
But when I start my container I got the error
Warning: include(/var/www/html/vendor/composer/../epo/api-auth-sso-bundle/EpoApiAuthSsoBundle.php): failed to open stream: No such file or directory
How can I do ??
PS : It is not possible to install something in Docker image because it is intranet network
Thanks a lot
Serge
I assume that the vendor file is missing dependencies, specifically epo/api-auth-sso-bundle/EpoApiAuthSsoBundle.php so you'll need to install them.
Grab a root shell inside the container:
docker exec -it -u root <container_id_or_name> /bin/bash
Install composer (requires root):
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Then install your dependencies (if not in production, no need for --no-dev):
composer install --no-dev
Finally, change the permissions back (ls -rvla):
# Nginx
chown -R nginx:nginx .
# Apache2
chown -R www-data:www-data .
Note: This is a "hacky-fix", if you rebuild the container you will have to do this every time. You should look to do this inside your Dockerfile as a permanent solution.
Untested example of the Dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
# Nginx
RUN groupadd -g 101 nginx && \
useradd -u 101 -ms /bin/bash -g nginx nginx
# Apache2
RUN groupadd -g 101 www-data && \
useradd -u 101 -ms /bin/bash -g www-data www-data
# Download and install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Nginx
USER nginx
# Apache2
USER www-data
# Or add this as an entry-point (runs as user for permissions)
RUN composer install --no-dev
I want to download a public github project and run a project file through internal php server, through docker.
This is my file so far:
FROM php:7.2-cli
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
RUN git clone https://github.com/mygit src
WORKDIR src
CMD ["php", "-S", "0.0.0.0:80", "-t", "/src/src/examples/image.php"]
The process does not show up in docker ps when I run:
docker build -t myimage .
docker run -d -p 8080:8080 myimage
Try with this Dockerfile:
FROM php:7.2-cli
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
RUN git clone https://github.com/douma/langtons-ant src
WORKDIR src
ENTRYPOINT ["php"]
CMD ["-S", "0.0.0.0:8080","/src/src/examples/image.php"]
Note I don't know php but checking your github project's readme I think you do not need the -t argument.
I also added ENTRYPOINT command to make your Dockerfile clearer. For differences check this.
The build and run commands should be the same as these you posted.
docker build -t myimage .
docker run -d -p 8080:8080 myimage
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
I'm new to Docker and just made my first steps.
I wanted to play a little bit with docker and was about configuring a Dockerfile for yii:
FROM php:7.2.3-apache
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer
RUN apt-get update && apt-get install -y git unzip zip
EXPOSE 8080
RUN composer create-project --prefer-dist yiisoft/yii2-app-basic test
Then I run the container with:
docker container run -d --name test -p 8080:8080 test-yii
When I go to localhost:8080 i got an ERR_EMPTY_RESPONSE.
This is the network result of docker container inspect:
"Ports": {
"80/tcp": null,
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
I appreciate every hint that helps me to solve this issue!
Edit: I forgot to mention that i connect to the container and then run php yii serve to test if Yii runs and this results in the above described problem.
YII is not started when you create a container. That's because it isn't defined in Dockerfile. Only apache starts there, because that's come from the image php:7.2.3-apache.
The correct Dockerfile is:
FROM php:7.2.3-apache
RUN curl -sS https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer
RUN apt-get update && apt-get install -y git unzip zip
EXPOSE 8080
RUN composer create-project --prefer-dist yiisoft/yii2-app-basic test
CMD test/yii serve 0.0.0.0
Now, CMD layer with yii overlaps CMD layer from php:7.2.3-apache image.
If you want to start both yii and apache inside a container you should look to these pieces of advice.
Update to the Edit section in the question:
You need to run php yii serve 0.0.0.0. Otherwise yii binds to localhost:8080 and is only accessible inside a container