laravel 向 Docker 上的 Artisan 推荐方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24821859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recommend way to Artisan on Docker
提问by tomsowerby
I am yet to find an elegant and efficient way to run Laravel Artisan commands in my Docker based local dev environment.
我还没有找到一种优雅有效的方法来在基于 Docker 的本地开发环境中运行 Laravel Artisan 命令。
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:
我考虑过的事情:
- A new container (sharing the same volume and db link) with ssh, just for running commands (seems nasty).
- Hacks in supervisor that could then end up running on live (not ideal).
- Editing db configs, or trying to hack in a "host" environment, so that at least things like migrate can be run from the host.
- Creating web front ends to run things (really nasty).
- Trying to build a "signal" for it things.
- 一个带有 ssh 的新容器(共享相同的卷和数据库链接),仅用于运行命令(看起来很讨厌)。
- 主管中的黑客最终可能会在现场运行(不理想)。
- 编辑 db 配置,或尝试在“主机”环境中进行 hack,以便至少可以从主机运行 migrate 之类的东西。
- 创建 Web 前端来运行东西(真的很讨厌)。
- 试图为它建立一个“信号”。
I'm still getting my head around Docker and it's new-container-for-everything approach.
我仍然对 Docker 有所了解,它是一种适用于一切的新容器方法。
I suppose I want to balance cool-dev-ops stuff with why-do-I-need-another-fake-server-just-get-it-working-already.
我想我想平衡cool-dev-ops 的东西和为什么我需要另一个假服务器-只是-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.
欢迎任何建议和想法。谢谢大家。
采纳答案by Dylan Lindgren
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.
关于 Docker 的最佳实践是在它自己的容器中运行每个进程。因此,运行artisan
命令的理想方式是拥有一个专门为此目的创建容器的镜像。
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 GitHubas well if you want to take a look at the Dockerfile
behind it.
我创建了一个可以从 Docker Hub 中提取的图像,dylanlindgren/docker-laravel-artisan
它运行得非常好。如果你想看看它的背后,它也在GitHub 上Dockerfile
。
I've also just written a blog postdescribing the way that all these seperate containers fit together.
我还刚刚写了一篇博客文章,描述了所有这些单独的容器组合在一起的方式。
回答by terbooter
回答by Kryten
There are a couple of possibilities...
有几种可能性......
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
orcomposer 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.adding an SSH server to your container (which is not recommended; here's a good discussion of that).
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.
在容器中挂载一个主机目录作为 Laravel 应用程序所在的文件夹。这样你就可以直接运行
php artisan migrate
或composer update
从主机运行。但是,您可能会遇到部署问题,因为您必须在服务器上复制该部分环境。将 SSH 服务器添加到您的容器(不推荐这样做;这是一个很好的讨论)。
构建并使用nsenter,这是一种用于“进入”正在运行的容器并获得 shell 访问权限的工具。请注意,我没有使用过它,我刚刚通过上面链接中的参考找到了它。
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.
如果您主要对部署感兴趣并且通过 dockerfile 进行部署,那么答案是将composer install
和添加php artisan migrate
到您的 dockerfile 中,以便它们在构建容器时运行。
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.
我有兴趣听到更多关于这个的答案。这也是我刚刚接触并想了解更多的东西。
回答by mtmacdonald
I use SSH and run migrations from a terminal inside the container.
我使用 SSH 并从容器内的终端运行迁移。
I personally like Phusion'sapproach of using Docker as a 'lightweight virtual machine'. So I used their baseimage-dockerwhich I've extended to create my own Docker image for Laravel applications.
我个人喜欢Phusion将 Docker 用作“轻量级虚拟机”的方法。所以我使用了他们的baseimage-docker,我已经扩展了它来为 Laravel 应用程序创建我自己的Docker 镜像。
I'm aware Phusion's image can be contentious in the Docker community, and that SSH is frowned uponby 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.
我知道 Phusion 的形象在 Docker 社区中可能存在争议,而且SSH受到一些提倡将 Docker 容器作为微服务的人的反对。但我对 Phusion 的方法很满意,直到有更多适用于多容器方法的成熟工具和实践。
回答by Aswin Kumar
I'm in the process of figuring out creating Docker images for Laravel projects, this is what I have so far.
我正在为 Laravel 项目创建 Docker 镜像,这是我目前所拥有的。
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.
这样,在构建时就没有对数据库的依赖,并且每次启动新容器时都可以运行迁移过程。