DEV Community

loading...

Using Docker with Spatie's Laravel Package development

adithyasrinivasan profile image adithyasrinivasan Originally published at adithya.dev on ・2 min read

Originally published on adithya.dev

I have been building packages for my private use and came across this problem where I had to build a package and integrate it into a project that's running on Docker.

For local development, I make use of repositories in composer.json to achieve this. In Docker, since your host machine and docker images run separately, you cannot include the path as you would, if you ran it on the same machine (without Docker).

Tools used are

//composer.json
"require": { 
    "adithya/package-name": "dev-master"
},
"repositories": [
        {
            "type": "path",
            "url": "../package-name"
        }
    ]
Enter fullscreen mode Exit fullscreen mode

When I ran composer install, I got the following error

[RuntimeException]

The url supplied for the path (../package-name) repository does not exist

After a bit of researching, I found the solution. In your docker-compose.yml, add the absolute path to your package and a mount point under your volumes in composer image.

composer:
    build:
      context: .
      dockerfile: composer.dockerfile
    container_name: composer
    volumes:
      - ./src:/var/www/html
      - /usr/Code/package-name:/package-name
    working_dir: /var/www/html
    depends_on:
      - php
    user: laravel
    networks:
      - laravel
    entrypoint: ['composer']
Enter fullscreen mode Exit fullscreen mode

Similarly, make sure to add the volume to your php container!

Now on your Project where you want to include the package, change the reference for your URL

//composer.json
"require": { 
    "adithya/package-name": "dev-master"
},
"repositories": [
        {
            "type": "path",
            "url": "/package-name"
        }
    ]
Enter fullscreen mode Exit fullscreen mode

Docker should automatically mirror the changes in your package to the project so it should be a breeze. If you do choose to use ADD in your dockerfile instead of mounting the volumes, your changes won't be automatically reflected since it copies your folder at the build time.

ADD . /package-name

Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide