skip to Main Content

I have a image defined as follows:

php-enc:
      container_name: php-apache
      image: php:8.0-apache
      volumes:
        - ./php/src:/var/www/html/
      ports:
        - 8000:80
      
      depends_on:
            - db

Now i want to run a command inside this container after building this. What I am doing is using the command

docker exec -it php-apache bash

This takes me into the container and I use another command

docker-php-ext-install mysqli

After this mysqli is installed in the php-apache container.

Now I want include all of this into the docker-compose file, So I tried

command: sh -c  "docker-php-ext-install mysqli"
php-enc:
  container_name: php-apache
  image: php:8.0-apache
  command: sh -c  "docker-php-ext-install mysqli"
  volumes:
    - ./php/src:/var/www/html/
  ports:
    - 8000:80
  
  depends_on:
        - db

But it is not working. The container stops running

I want to know how to use command in docker-compose.yml

2

Answers


  1. the command in your docker-compose.yml file should like this:

    command: sh -c 'docker-php-ext-install mysqli && apache2-foreground'
    

    And why your php-apache stopds running, cause’s:

    the command: sh -c "docker-php-ext-install mysqli" does not remain in foreground process after it’s execution, which is required for the container to remain running

    Login or Signup to reply.
  2. You should run any installation and setup commands like this in a Dockerfile. This will give you a custom image that contains your application’s dependencies.

    The minimal form of this would contain a Dockerfile with just a FROM line with your current image name, and then RUN any commands you need.

    FROM php:8.0-apache
    RUN docker-php-ext-install mysqli
    
    version: '3.8'
    services:
      php-enc:
        build: .
        volumes:
          - ./php/src:/var/www/html/
        ports:
          - 8000:80
        depends_on:
          - db
      db: { ... }
    

    Make sure to delete the image: line from your Compose file. Leaving it in will apparently work, but replace the actual php:8.0-apache image with what you’ve built, which leads to confusing results later.

    Also consider using the Dockerfile COPY directive to include your application code in the image as well

    COPY ./php/src/ /var/www/html/
    

    This will also let you delete the volumes: from your Compose file and give you a self-contained runnable image. You could push that image to a registry and run it elsewhere without needing to have a separate copy of the application code. This also would mean that edits on your host system won’t be visible in a container without rebuilding the image (this is a normal workflow in most compiled languages but may not be what you’re used to in PHP).

    There are other Dockerfile directives like EXPOSE and CMD but you don’t need them here; the information about how to run the main container process gets inherited from the base image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search