Reputation: 1719
I create a php-composer image using dockerfile:
FROM php:7
RUN apt-get update
RUN apt-get install curl
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y git
And I run following commands to create a container and start a laravel app.
docker run -p 127.0.0.1:3000:8000 --name MyTest -dt php-composer to create a container
docker cp laravelApp/ d4bbb5d36312:/usr/
docker exec -it MyTest bash
cd usr/laravelApp
php artisan serve
After thet, container's terminal will show the success info:
Laravel development server started: <http://127.0.0.1:8000>
But when I access 127.0.0.1:3000 at local browser, I get nothing.
So is it possible that simply run php artisan serve to start a laravel app inside docker container?
Or I must to using nginx or apache to run it?
Upvotes: 11
Views: 24050
Reputation: 421
This can be done so:
$ docker container run -it --rm -v /host/path/laravel:/app -p 3000:8000 php bash
$ cd /app
$ php artisan serve --host 0.0.0.0
By default containers start in bridge
network, inside which the host available by the address 0.0.0.0
.
When you start Docker, a default bridge network (also called bridge) is created automatically, and newly-started containers connect to it unless otherwise specified.
https://docs.docker.com/network/bridge
Or so (only Linux):
$ docker container run -it --rm --network host -v /host/path/laravel:/app php bash
$ cd /app
$ php artisan serve (or php artisan serve --port 3000)
If you use the host network driver for a container, that container’s network stack is not isolated from the Docker host.
https://docs.docker.com/network/host
Upvotes: 29
Reputation: 969
You can debug the issue with two commands:
Run this on the host machine to check if the port mapping is correct:
docker port MyTest
Run this on the host machine to check the output from inside your container:
docker exec MyTest curl 127.0.0.0:8000
You should see the raw HTTP response of your Laravel application.
Upvotes: 0