skip to Main Content

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


  1. 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:

    $ docker-compose up
    
    Creating network "tempphpdocker_default" with the default driver
    Pulling web (php:7.3-cli)...
    7.3-cli: Pulling from library/php
    b8f262c62ec6: Pull complete
    a98660e7def6: Pull complete
    4d75689ceb37: Pull complete
    639eb0368afa: Pull complete
    2cdbfdb779b1: Pull complete
    e0b637fa9606: Pull complete
    da7333b0ef25: Pull complete
    01d65ff46009: Pull complete
    673e50bed3b9: Pull complete
    bf6c6e34305d: Pull complete
    Digest: sha256:1453f5ef0d4d1d424ed8114dd90a775bdec06cc6fb3bbae9521dcb4ca0c8ca90
    Status: Downloaded newer image for php:7.3-cli
    Creating tempphpdocker_web_1 ... 
    Creating tempphpdocker_web_1 ... done
    Attaching to tempphpdocker_web_1
    web_1  | Interactive shell
    web_1  | 
    tempphpdocker_web_1 exited with code 0
    

    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:

    FROM php:7.3-cli
    
    RUN mkdir -p /opt/workdir/bin
    RUN mkdir -p /opt/workdir/vendor
    
    COPY bin/ /opt/workdir/bin
    COPY vendor/ /opt/workdir/vendor
    
    WORKDIR /opt/workdir
    CMD php ./bin/console COMMAND
    

    You can simply build and run this Dockerfile, or you if you prefer docker-compose, you can define docker-compose.yml in the same folder as the Dockerfile:

    version: "3.3"
    services:
        web:
            image: php-custom
            build: ./
    

    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.

    Login or Signup to reply.
  2. Quick and dirty fix to keep you container running just override the container command in docker-compose.

    version: "3.3"
    services:
      web:
        image: php:7.3-cli
        command: tail -f /dev/null
    
    

    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.

    docker exec -it php-cli_web_1 ash
    
    

    My question is how to run the commands inside my project root
    directory.

    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.

    version: "3.3"
    services:
      web:
        image: php:7.3-cli
        command: php:7.3-cli /app/your_script.php
        volumes:
          - /home/myporject:/app
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search