When I run my laravel application, I get the error Laravel docker image Could not open input file: /var/www/html/artisan. I finally found the root cause of the issue, let me share it with you.
I generated my application in Laravel sail but now I am working to make it run inside a docker container. My application runs well in I use the docker-compose run but during the docker build process to generate the image, I get an error.
Since I run it in a docker container, we are going to diagnose by Laravel is complaining.
The short answer is that your Laravel source code is not accessible by the process that is trying to run it.
In my case, I run Laravel in a docker container using supervisord.
To start, I run the application using the docker-compose up command (see below the configuration that I have). It worked well because I have a volume linked to the source code (see the plain line below). This means that the docker-compose run process has access to the source code, so it worked.
Read more: Vue cors policy no access control allow origin
When I run the build process to generate my docker image docker build docker/7.4 -t mypersonaltag there is no link to the source code (see the dotted line below). So the build process failed with that error :
Here is a schema of my setup:
graph direction LR; A[Source code] B[docker-compose up process] C[docker build process] B->A C-.->A
My setup is quite similar to the laravel setup I did with docker in this post. The idea is to bundle your application inside a docker image, then use the image in other docker services to build something bigger.
The script to start the container
I created this script to run the necessary steps to start the application.
It takes care of the permission issies and then uses supervisord to start the application.
At this stage, the source code of the application is assumed to be in the /var/www/html/ container image.
The supervisord configuration file is below:
The Dockerfile to build the image
Read more: Shadow boxing hair
Here is the Dockerfile I used to build the image. You can see that there is no instruction to copy the source code inside the container image.
But at the end of the file, there is an ENTRYTPOINT instruction asking the container to run the script we created above.
The docker-compose setup
Here is my docker-compose stack. It is intended to pack everything together. The Laravel application is build from the Dockerfile we created previously.
It maps the source code from the current folder (where the docker-compose.yml is located).
Why it failed during the docker build
The root cause is releated to the presence of the source code.
The error below is thrown when:
- the container image build
- ENTRYPOINT run the script to start the container
- supervisord tries to run the code and then ? (the source code is missing)
Read more: Campsite lighting
Why it worked during the
When I run the application using the docker-compose up command. It worked well because I have a volume linked to the source code
This means that the docker-compose run process has access to the source code, so it worked.
To solve the issue, you need to add a COPY instruction to the Dockerfile.
The source code is located in the current directory. The instruction will copy the source into the instance.
Here is the complete Dockerfile.
It is nice to have been able to pinpoint the root cause. Hope you liked it. Share your thoughts in the comments section.