Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append command to docker-compose.yml without override existing?

In docker-compose.yml I have a service

app:
    image: yiisoftware/yii2-php:7.1-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '80:80'
    depends_on:
      - db

So I want to execute command yii migrate --interactive=0 when container is started. But if I just add string

command: "yii migrate --interactive=0"

It override command that already specified in yiisoftware/yii2-php:7.1-apache dockerfile. How can I append command, not replace? Is it possible?

I already googled for this problem, but most popular solution is "create your own dockerfile". Can I solve this without create/modify dockerfile or shell script, only with docker-compose?

like image 724
Rikipm Avatar asked Jun 12 '19 10:06

Rikipm


People also ask

Does docker compose override Dockerfile CMD?

Docker-compose command doesn't override Dockerfile CMD.

Does docker compose command override entrypoint?

All About Docker Compose Override Entrypoint Entrypoint helps use set the command and parameters that executes first when a container is run. In fact, the command line arguments in the following command become a part of the entrypoint command, thereby overriding all elements mentioned via CMD.

What does docker compose override yml do?

By convention, the docker-compose. yml contains your base configuration. The override file, as its name implies, can contain configuration overrides for existing services or entirely new services.


2 Answers

To do this, you would have to define your own ENTRYPOINT in your docker-compose.yml. Besides building your own Dockerfile, there is no way around this.

As much as I am searching, I cannot find a CMD instruction in this image's Dockerfile, though. So this is probably what's being used.

I'm not familiar with PHP, so I cannot estimate what you would have to change to run a container with your specific needs, but these are the points that you should have a look at.

like image 110
bellackn Avatar answered Sep 30 '22 06:09

bellackn


You can include the pre-existing command in your new command. Now understand that everything in those commands can be called from sh or bash. So look up the existing command by doing an:

docker image inspect yiisoftware/yii2-php:7.1-apache

Then check the command in Config { Command }. (Not ConatinerConfig.) So if the existing command had been:

"start-daemon -z foo-bar"

then your new command would be:

sh -c "start-daemon -z foo-bar; yii migrate --interactive=0"

Now you'll have the existing command plus your new command and the desired effect.

like image 42
Joe C Avatar answered Sep 30 '22 05:09

Joe C