Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use php artisan serve inside docker container?

Tags:

docker

laravel

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?

like image 748
jimmy Avatar asked Dec 03 '22 19:12

jimmy


1 Answers

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

like image 143
amlagoda Avatar answered Dec 11 '22 16:12

amlagoda