I have created a command line application using symfony 3.4 which doesn’t need to display any web page.
I generally run the commands like following:
php bin/console MY_COMMAND_NAME
I want to dockerize the application and share it with others, so inside the root directory of my project I created a docker-compose.yml file, which looks like following:
version: "3.3"
services:
web:
image: php:7.3-cli
Then I ran docker-compose up, after that I checked the PHP version by the following command and it showed my the correct version:
docker run php:7.3-cli php -v
However, when I ran docker ps, it didn’t show any container running.
My question is how to run the commands inside my project root directory. FYI, I am using Docker Toolbox, on windows 10 Home Edition and my project location is:
C:Users{my_user_name}Desktopfolder_1folder_2
2
Answers
The docker container need to have a long running process defined in CMD to stay running. php-cli is not that. If you run
composer up
, you’ll see something like this:The exit code is 0. This means your command in the docker image
php:7.3-cli
has successfully run and finished.To properly dockerize your applicaiton, you should override this by writing you own docker file with proper COPY calls that bundle your CLI program into it. Your
Dockerfile
should probably look something like this:You can simply build and run this
Dockerfile
, or you if you preferdocker-compose
, you can definedocker-compose.yml
in the same folder as the Dockerfile:Please noted that a dockerized application can only access files and folder in the docker image. You should bind volumes of your local file system to the container before it can actually work on your filesystem.
Quick and dirty fix to keep you container running just override the container command in docker-compose.
when you run
docker-compose up
it will keep the docker container but it will do not thing, just will give away to run command inside container.As mentioned by @David, you need to mount your host project to the container in docker-compose.
For instance
your project is placed on the host
/home/myporject
, mount the project within docker-compose and it will be available inside the container. then you can update the command of your docker-compose to run the script.keep in mind
The life of container is the life of docker-compose command
When the execution completed your container will be die after execution. so your container will run until the
php:7.3-cli /app/your_script.php
this script completed.