PHP Docker container exits for no reason

北城余情 提交于 2019-12-25 02:56:08

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/54599413/php-docker-container-exits-for-no-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!