tomsowerby
tomsowerby

Reputation: 498

Recommend way to Artisan on Docker

I am yet to find an elegant and efficient way to run Laravel Artisan commands in my Docker based local dev environment.

Could anybody suggest the recommended or "proper" way to do things like migrations?

Or, has anybody found a neat way of doing this? Ideally with examples or suggestions.

Things that I've considered:

I'm still getting my head around Docker and it's new-container-for-everything approach.

I suppose I want to balance cool-dev-ops stuff with why-do-I-need-another-fake-server-just-get-it-working-already.

I'd love to commit to it for my dev workflow, but it seems to become awkward to use under certain circumstances, like this one...

Any suggestions and ideas are welcome. Thanks all.

Upvotes: 18

Views: 15219

Answers (5)

Aswin Kumar
Aswin Kumar

Reputation: 5278

I'm in the process of figuring out creating Docker images for Laravel projects, this is what I have so far.

FROM base_image_with_LAMP_stack_and_dependencies

WORKDIR /var/www/html/app

COPY composer.json composer.json

RUN composer install --no-scripts --no-dev --no-autoloader

COPY . .

RUN echo 'chown -R www-data:www-data /var/www/ \
&& composer dump-autoload \
&& cp .env.example .env \
&& php artisan key:generate \
&& php artisan migrate \
&& apachectl -D FOREGROUND' >> /root/container_init.sh && \
 chmod 755 /root/container_init.sh

EXPOSE 80

CMD /root/container_init.sh

This way, there is no dependency on database during build time, and the migration process can run every time a new container is started.

Upvotes: 0

terbooter
terbooter

Reputation: 399

Docker 1.3 bring new command exec So now you can "enter" running container like

docker exec -it my-container-name /bin/bash

After that you can run any command you want

php artisan --version

Upvotes: 12

Dylan Lindgren
Dylan Lindgren

Reputation: 361

The best practice regarding Docker is to run each process inside it's own container. Therefore, the ideal way to run artisan commands is to have an image for creating containers specifically for this purpose.

I've created an image which can be pulled from the Docker Hub dylanlindgren/docker-laravel-artisan and it works really well. It's on GitHub as well if you want to take a look at the Dockerfile behind it.

I've also just written a blog post describing the way that all these seperate containers fit together.

Upvotes: 9

mtmacdonald
mtmacdonald

Reputation: 15110

I use SSH and run migrations from a terminal inside the container.

I personally like Phusion's approach of using Docker as a 'lightweight virtual machine'. So I used their baseimage-docker which I've extended to create my own Docker image for Laravel applications.

I'm aware Phusion's image can be contentious in the Docker community, and that SSH is frowned upon by some who advocate Docker containers as microservices. But I'm happy with Phusion's approach until there are more established tools and practices for the multi-container approach.

Upvotes: 1

Kryten
Kryten

Reputation: 15780

There are a couple of possibilities...

  1. Mounting a host directory in your container as the folder in which your Laravel app lives. That way you can just run php artisan migrate or composer update from the host. You might have problems with deployment, though, since you would have to replicate that part of your environment on the server.

  2. adding an SSH server to your container (which is not recommended; here's a good discussion of that).

  3. build and use nsenter, a tool for "entering" a running container and getting shell access. Note, I haven't used it, I just found it a while ago via a reference in the link above.

If you're primarily interested in deployment and you're doing it via a dockerfile, then the answer would be to add composer install and php artisan migrate to your dockerfile so they run when the container is built.

I'm interested in hearing more answers to this. It's something that I'm just getting into as well and would like to learn more about.

Upvotes: 1

Related Questions