user7847084
user7847084

Reputation:

PHP Docker container exits for no reason

I'm relatively new to docker and docker-compose so I made this file

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8050:80"
    volumes:
      - ./code:/code
      - ./site.conf:/etc/nginx/conf.d/default.conf
    links:
      - php
  php:
    image: php:7.3-fpm-alpine3.9
    command: apk --update add php7-mysqli
    volumes:
      - ./code:/code
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: example

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

For some reason the line

command: apk --update add php7-mysqli

Stops php container for no reason, just prints dock_php_1 exited with code 0

Thus my web container also stops and service doesn't work

What could be core of the problem and how to fix it?

Upvotes: 2

Views: 3991

Answers (2)

Y.R.
Y.R.

Reputation: 1

I assume what you actually need is to use Docker Compose "build" option, and provide Dockerfile which installing the desired package (RUN apk --update add php7-mysqli). Alternatively, you can build a new Docker image and use it directly in your Docker Compose file.

Docker Compose "command" is to override the default Docker image command (CMD command of the Dockerfile)

More explanation can be found here: https://medium.freecodecamp.org/docker-entrypoint-cmd-dockerfile-best-practices-abc591c30e21

Upvotes: 0

Dan Streeter
Dan Streeter

Reputation: 2381

This is because you are telling the container to run the apk update command when it starts, this completes and exits with a valid exit code 0...

To get it to run that apk update command and still use the php container, you need to extend the php image with your own build to create your own 'custom image' of the base php image (kinda like extending a PHP class), and then run that apk update as part of the dockerfile.

This is reasonably easy to do and your dockerfile would look something like:

FROM php:7.3-fpm-alpine3.9

RUN apk --update add php7-mysqli

You can save this as ./php/Dockerfile

Then update your docker-compose.yml file to say:

...
php:
  build: ./php
  volumes:
...

Removing the command: section

This would then, upon docker-compose up, build your extended image with the apk update inside it as an extra layer on the container, and continue running the standard php command that the original image provides.

Here is the documentation on the build: directive, as there are quite a few other cool things you can do with it, like specifying the Dockerfile if you don't want to put it into a subdirectory, and providing the context: should you wish to bake in files to your new image https://docs.docker.com/compose/compose-file/#build

Upvotes: 2

Related Questions