简体   繁体   English

如何使用 docker 容器从主机运行 artisan 命令

[英]How run artisan commands from Host with docker containers

如果服务器运行到容器中,则可以从 Host 运行“php artisan commands”,以及如何运行?

您需要更改路径引用以适合您自己的项目,但这允许您从容器上的主机执行 artisan 命令,而无需登录。

docker exec -it <php-fpm-container> /var/www/html/artisan

To run commands from your host,要从主机运行命令,

You can use Docker's exec command like this:您可以像这样使用 Docker 的exec命令:

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

After that, you can run any command you want之后,您可以运行任何您想要的命令

php artisan --version

Client ( php artisan ) on the host talking to server in a container is a solved problem:主机上的客户端( php artisan )与容器中的服务器通信是一个已解决的问题:

This can be done in two ways.这可以通过两种方式完成。 They make the server's port in the container accessible on the host:它们使容器中的服务器端口可在主机上访问:

  1. Publish the server's port to the host by passing -p to docker run .通过将-p传递给docker run将服务器的端口发布到主机。 More info is here .更多信息在这里

     docker run -p hostPort:containerPort ...
  2. Use Docker's " host networking " by passing --network host to docker run .使用多克的“主机网络”所传递--network hostdocker run More info is here .更多信息在这里

     docker run --network host ...

if using docker-compose如果使用docker-compose

  1. first get into the container首先进入容器
docker-compose exec <container_name|id> bash
  1. now goto var/www/html directory and run your choice of command现在转到 var/www/html 目录并运行您选择的命令

This is the solution!这就是解决方案! Ok, for a while, I thought I was crazy, but I did not.好吧,有一段时间,我以为我疯了,但我没有。 The trick is set a PHP into host (for the CLI), matching with the docker PHP version, so:诀窍是将 PHP 设置为主机(用于 CLI),与 docker PHP 版本匹配,因此: 这张图片显示了这个技巧是如何工作的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM