Docker newbie here and I don’t understand how it all works together. How do I develop in the container? Or do I need to do development and then build and run the container as two separate actions?
I created a Laravel 10 app in ~/source/mediacenter. I dockerized it with this Dockerfile, built it and started it.
# Dockerfile
FROM php:8.2-cli
RUN apt-get update -y && apt-get install -y libmcrypt-dev
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# RUN docker-php-ext-install pdo mbstring
WORKDIR /var/www/html
COPY . /var/www/html
RUN composer install
EXPOSE 3100
CMD php artisan serve --host=0.0.0.0 --port=8000
I can access it from localhost:3100.
Now how do I make a change to my Laravel code and see it work? Changes are in my /source/mediacenter directory but code is running in /var/www/html. If I docker exec into the container and run artisan (create a new model for instancde), those changes happen in /var/www/html/ and not my source code.
Does this mean I need to develop and run locally and when I am happy with it, build the image and container to deploy wherever I want? Or, is there a trick that can let me develop and test within the running container? I did spend an hour searching for solutions but am probably not asking the right question.
2
Answers
You can use docker-compose map your changes in running containers.
for example, your docker file must be bellow:
And your
docker-compose.yml
file like bellow:Now every change can when you did map into container
If you are not sure about docker and you wish to focus on development first, you may use Laravel Sail instead.
Command:
curl -s "https://laravel.build/example-app" | bash
Where
example-app
is the root project folder.This will download a basic PHP docker image with a default Laravel project for you. Running
./vendor/bin/sail up
will build/start the container.You can update the code inside
example-app
and this will be reflected in your project.Sail also comes with composer and node/npm.
Commands:
If you are in ubuntu, you can create an alias so that you can shorten
./vendor/bin/sail
tosail