简体   繁体   中英

docker-compose - running two services to execute a single PHP script

I just started with Docker few days ago, and I'm very happy with the results so far. I'm only trying it currently for development purposes.

I'm using docker-compose , and this is my docker-compose.yaml :

version: '2'

services:
    nginx:
        image: nginx:alpine
        ports:
            - "8070:80"
        volumes:
            - ./:/app
            - ./docker-nginx-default.conf:/etc/nginx/conf.d/default.conf

    php7:
        container_name: php7
        image: adrian1210/php
        volumes:
            - ./:/app:delegated
        environment:
            XDEBUG_CONFIG: "remote_host=192.168.5.14"

    composer:
        entrypoint: /bin/true
        image: composer
        volumes:
          - ./:/app
        command: install --ignore-platform-reqs --no-scripts

    mongodb:
        image: mongo
        environment:
          - MONGO_DATA_DIR=/data/db
          - MONGO_LOG_DIR=/dev/null
        volumes:
          - ./data/db:/data/db
        ports:
            - 8080:27017
        command: mongod --smallfiles --logpath=/dev/null # --quiet

Everything works just great.

But I have one issue now, maybe I don't understand this completely, but I have searched the official docs / SO and didn't find a satisfiable answer.

I have a PHP script which I just want to run from my host OS. Something similar to installing PHP dependencies using composer. When installing PHP dependencies, I would just run

docker run --rm --interactive --tty --volume $PWD:/app composer install --ignore-platform-reqs --no-scripts

and all works.

Now I have a PHP script that I need to execute in the same manner, meaning 'just execute, and close everything'.

The thing is, for the script to execute completely, I need to have my mongodb container up and running, because the script is writing to database.

So since I must have mongodb container up, I tried to execute the script with docker-compose , instead with docker :

docker-compose run php7 php /app/Apps/Php/Cli/install.php

but it throws error, saying that it cannot access the mongodb.

Which is true, if I type docker ps , container isn't there.

But, if I eg. ran the whole environment with docker-compose up -d and after typing docker ps , I would see the mongodb running, and of course, if I again ran the upper install.php script, it would work.

So my question is, can I somehow run the php7+mongodb services together, and close them immediately after the script is done?

UPDATE 1:

One thing that DID work is adjusting the docker-compose.yaml , the php7 service, now it looks like this:

php7:
    container_name: php7
    image: adrian1210/php
    volumes:
        - ./:/app:delegated
    environment:
        XDEBUG_CONFIG: "remote_host=192.168.5.14"
    links:
        - mongodb

So as you can see, I added links section and it worked. When running the script with this parameter (I used the same docker-compose run ... command), I can see in the console that mongodb is booting up.

Now, the only thing that didn't work is that after the script was done, mongodb was still running. php7 was closed correctly.

With all this, I'm afraid I'm not sure how the links parameter is working. I mean, in the official docs, I could read that links was something that was used in version 1 of the docker-compose.yaml , and that all services are automatically linked into a network by default. Maybe this behaviour applies only when calling docker-compose up , not with docker-compose run ?

The behavior of the links in this regard is explained in the documentation at https://docs.docker.com/compose/compose-file/#links . In particular:

Links also express dependency between services in the same way as depends_on, so they determine the order of service startup.

So, running docker-compose run is bringing up the dependent service. You would need to explicitly down the dependent service if you no longer need it running. It likely doesn't bring it down after a run as there could be multiple runs or other services that may need to depend on the service as well.

In addition to this, there is a newer depends_on which explicitly defines the dependencies and order at https://docs.docker.com/compose/compose-file/#depends_on . You don't necessarily need to use links in later Docker Compose versions as the service name is already added as a DNS entry to the /etc/hosts file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM