9

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?

1
  • You should use nginx or Apache, as the php artisan serve server isn't for use in production, and your development environment should be as close to production as possible.
    – ceejayoz
    Sep 20, 2018 at 22:06

2 Answers 2

25

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

0
0

You can debug the issue with two commands:

  1. Run this on the host machine to check if the port mapping is correct: docker port MyTest

  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.