Pedrog
Pedrog

Reputation: 1390

How run artisan commands from Host with docker containers

It is possible run "php artisan commands" from Host if the server is running into container, and how?

Upvotes: 6

Views: 26471

Answers (7)

neisantos
neisantos

Reputation: 491

cd on my project

cd /Users/neisantos/src/laravel-project

considering I have a docker running using the name [APP]

alias artisan='[ -d /Users/neisantos/src/laravel-project ] && docker exec -it APP php artisan'

so I can only do the command below to execute artisan from my docker container.

artisan route:list

this alias will run only when running from the folder of my project.

Upvotes: 0

Ben jamin
Ben jamin

Reputation: 1018

when using package.json you could place it inside the "scripts" like

{
 ...
 "scripts":{
  "artisan": "docker exec -it CONTAINER-NAME php artisan --command process.env.command"
 }
...
}

then you could execute artisan-commands like that:

migrate:

npm run artisan migrate

version

npm run artisan --version

Upvotes: 0

Dev Matee
Dev Matee

Reputation: 5939

if using 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

Upvotes: 0

Pedrog
Pedrog

Reputation: 1390

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: This image shows how the trick works

Upvotes: -3

user6316291
user6316291

Reputation: 91

You'd need to change the path references to suit your own project, but this allows you to execute the artisan command from your host machine on the container without having to login.

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

Upvotes: 8

Vikram Hosakote
Vikram Hosakote

Reputation: 3674

Client (php artisan) on the host talking to server in a container is a solved problem:

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. More info is here.

    docker run -p hostPort:containerPort ...
    
  2. Use Docker's "host networking" by passing --network host to docker run. More info is here.

    docker run --network host ...     
    

Upvotes: 0

Sapnesh Naik
Sapnesh Naik

Reputation: 11636

To run commands from your host,

You can use Docker's exec command like this:

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

After that, you can run any command you want

php artisan --version

Upvotes: 16

Related Questions