2017-10-13 3 views
2

J'ai essayé de construire un conteneur cli PHP en utilisant Docker & Docker Compose et je veux le garder en vie sans avoir besoin d'avoir un script PHP en cours d'exécution à l'intérieur.Comment conserver un conteneur PHP-CLI sans avoir besoin de script PHP?

J'ai essayé mon Dockerfile:

FROM ubuntu:16.04 
ENV PATH="/root/.composer/vendor/bin:${PATH}" 

RUN apt-get update && \ 
    apt-get -y -qq install software-properties-common \ 
    xvfb \ 
    locales && \ 
    locale-gen en_US.UTF-8 && \ 
    export LC_ALL=en_US.UTF-8 && \ 
    export LANG=en_US.UTF-8 && \ 
    add-apt-repository ppa:ondrej/php && \ 
    apt-get update && \ 
    DEBIAN_FRONTEND=noninteractive apt-get -y --allow-unauthenticated install \ 
    php7.1-curl \ 
    php7.1-cli \ 
    php7.1-bcmath \ 
    php7.1-json \ 
    php7.1-intl \ 
    php7.1-mbstring \ 
    php7.1-mcrypt \ 
    php7.1-mysql \ 
    php7.1-xml \ 
    php7.1-xsl \ 
    php7.1-zip \ 
    curl \ 
    git \ 
    wget \ 
    pkg-config && \ 
    apt-get clean && \ 
    apt-get autoremove && \ 
    rm -rf /var/lib/apt/lists/* 

COPY config/
RUN sh /usr/local/bin/install.sh 
ENTRYPOINT bash -C '/entrypoint.sh';'bash' 
CMD ["php", "-a"] 

Et celui-ci d'autres aussi (allant de main PHP images):

FROM php:7-cli-alpine 
ENV PATH="/root/.composer/vendor/bin:${PATH}" 
COPY config/
RUN sh /usr/local/bin/install.sh 
WORKDIR /var/wwww 
CMD ["php", "-a"] 

Le docker-compose.yml n'a rien que ceci:

version: '3.3' 
services: 
    webserver: 
     build: 
     context: . 
     restart: on-failure 
     env_file: .env 
     environment: 
      UID: ${UID} 
      GID: ${GID} 

Enfin here est le référentiel complet avec tout si vous voulez essayer.

Quel est le problème? Après avoir construit le conteneur en exécutant docker-compose up --build --force-recreate il sortira avec le code 0 mais docker-composer logs n'est pas utile non plus car il ne dit rien.

Jetez un oeil à la sortie suivante à partir de la ligne de commande (pour la 2ème Dockerfile sur ce poste, pour le premier est à peu près la même chose avec plus de choses):

$ docker-compose up --build --force-recreate 
Building webserver 
Step 1/6 : FROM php:7-cli-alpine 
7-cli-alpine: Pulling from library/php 
90f4dba627d6: Pull complete 
623a0217e4bc: Pull complete 
ecbf507e7e3c: Pull complete 
79a82dee0025: Pull complete 
457716c6bb05: Pull complete 
62e36287ffd0: Pull complete 
507e98c003bc: Pull complete 
b25e699baa37: Pull complete 
Digest: sha256:ecae911bd22279d4883ffdc631afddc434641c67eedf49914fc62dafb5329663 
Status: Downloaded newer image for php:7-cli-alpine 
---> 1e563ea5f552 
Step 2/6 : ENV PATH "/root/.composer/vendor/bin:${PATH}" 
---> Running in 29b65c1d7def 
---> d11c62bb5cf5 
Removing intermediate container 29b65c1d7def 
Step 3/6 : COPY config/
---> e42022d1de91 
Step 4/6 : RUN sh /usr/local/bin/install.sh 
---> Running in 749230f42e9e 
All settings correct for using Composer 
Downloading... 

Composer (version 1.5.2) successfully installed to: /usr/local/bin/composer 
Use it: php /usr/local/bin/composer 

Changed current directory to /root/.composer 
Loading composer repositories with package information 
Updating dependencies 
Package operations: 21 installs, 0 updates, 0 removals 
    - Installing hirak/prestissimo (0.3.7): Downloading (100%) 
    - Installing symfony/stopwatch (v3.3.10): Downloading (100%) 
    - Installing symfony/process (v3.3.10): Downloading (100%) 
    - Installing symfony/polyfill-php72 (v1.5.0): Downloading (100%) 
    - Installing paragonie/random_compat (v2.0.11): Downloading (100%) 
    - Installing symfony/polyfill-php70 (v1.5.0): Downloading (100%) 
    - Installing symfony/options-resolver (v3.3.10): Downloading (100%) 
    - Installing symfony/finder (v3.3.10): Downloading (100%) 
    - Installing symfony/filesystem (v3.3.10): Downloading (100%) 
    - Installing symfony/event-dispatcher (v3.3.10): Downloading (100%) 
    - Installing psr/log (1.0.2): Downloading (100%) 
    - Installing symfony/debug (v3.3.10): Downloading (100%) 
    - Installing symfony/polyfill-mbstring (v1.5.0): Downloading (100%) 
    - Installing symfony/console (v3.3.10): Downloading (100%) 
    - Installing php-cs-fixer/diff (v1.1.0): Downloading (100%) 
    - Installing gecko-packages/gecko-php-unit (v2.2): Downloading (100%) 
    - Installing doctrine/lexer (v1.0.1): Downloading (100%) 
    - Installing doctrine/annotations (v1.5.0): Downloading (100%) 
    - Installing composer/semver (1.4.2): Downloading (100%) 
    - Installing friendsofphp/php-cs-fixer (v2.7.1): Downloading (100%) 
    - Installing squizlabs/php_codesniffer (3.1.0): Downloading (100%) 
Writing lock file 
Generating optimized autoload files 
Cache directory does not exist (cache-vcs-dir): 
Clearing cache (cache-repo-dir): /root/.composer/cache/repo 
Clearing cache (cache-files-dir): /root/.composer/cache/files 
Clearing cache (cache-dir): /root/.composer/cache 
All caches cleared. 
---> ff310362f4a5 
Removing intermediate container 749230f42e9e 
Step 5/6 : WORKDIR /var/wwww 
---> fe10fd52d6b6 
Removing intermediate container 6e472a693c0c 
Step 6/6 : CMD php -a 
---> Running in 789688d1346a 
---> b1286e96580b 
Removing intermediate container 789688d1346a 

Successfully built b1286e96580b 
Successfully tagged dockerlamp_webserver:latest 
Recreating dockerlamp_webserver_1 ... 
Recreating dockerlamp_webserver_1 ... done 
Attaching to dockerlamp_webserver_1 
webserver_1 | Interactive shell 
webserver_1 | 
dockerlamp_webserver_1 exited with code 0 

$ docker-compose logs 
Attaching to dockerlamp_webserver_1 
webserver_1 | Interactive shell 
webserver_1 | 

Comment je peux garder la le conteneur fonctionne sans avoir besoin d'exécuter un script PHP?

Note: vous demandez peut-être pourquoi ne pas simplement construire php-fpm|apache mais la chose est que je ne pas besoin de serveur Web car le conteneur sera utilisé pour un projet de ligne de commande uniquement (ce qui est un WIP)

Répondre

4

Vous exécutez php -a qui est un REPL et il a besoin de TTY interactif. Donc, vous devez activer ceux de votre Compose

version: '3.3' 
services: 
    webserver: 
     build: 
     context: . 
     restart: on-failure 
     env_file: .env 
     environment: 
      UID: ${UID} 
      GID: ${GID} 
     tty: true 
     stdin_open: true 

plus tard si vous devez attacher à exécuter des commandes que vous devrez utiliser docker

docker attach <containernameOrID>