skip to Main Content

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


  1. You can use docker-compose map your changes in running containers.

    for example, your docker file must be bellow:

    FROM php:8.1-fpm
    
    # Arguments defined in docker-compose.yml
    ARG user
    ARG uid
    
    # Install system dependencies
    RUN apt-get update && apt-get install -y 
        git 
        curl 
        libpng-dev 
        libonig-dev 
        libxml2-dev 
        libldap2-dev 
        zip 
        unzip
    
    # Clear cache
    RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    
    # Install PHP extensions
    RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd soap
    
    # Get latest Composer
    COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
    
    # Create system user to run Composer and Artisan Commands
    RUN useradd -G www-data,root -u $uid -d /home/$user $user
    RUN mkdir -p /home/$user/.composer && 
        chown -R $user:$user /home/$user
    
    # Install app dependency
    RUN mkdir /app
    
    COPY . /app
    WORKDIR /app
    
    RUN chown -R www-data:www-data 
            /app/storage 
            /app/bootstrap/cache
    
    RUN php artisan optimize && 
        php artisan key:generate && 
        php artisan config:cache&& 
        php artisan route:cache
    
    VOLUME /app
    
    USER $user
    
    VOLUME /app
    

    And your docker-compose.yml file like bellow:

    version: "3.3"
    services:
      app:
        build:
          args:
            user: laravel
            uid: 1000
          context: ./
          dockerfile: ./Dockerfile
        image: laravel-app
        container_name: laravel-app
        restart: unless-stopped
        volumes:
          - ./:/app
        ports:
          - 9000:9000
    

    Now every change can when you did map into container

    Login or Signup to reply.
  2. 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:

    # artisan:
    ./vendor/bin/sail artisan migrate
    # composer:
    ./vendor/bin/sail composer install
    # npm:
    ./vendor/bin/npm install
    

    If you are in ubuntu, you can create an alias so that you can shorten ./vendor/bin/sail to sail

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search