AbdurRehman Khan
AbdurRehman Khan

Reputation: 925

docker-compose: run a command without overriding anything

I have a docker-compose file which looks like:

version: "3"

services:
    mongoDB:
        restart: unless-stopped
        volumes:
            - "/data/db:/data/db"
        ports:
            - "27017:27017"
            - "28017:28017"
        image: "andresvidal/rpi3-mongodb3:latest"
    mosquitto:
        restart: unless-stopped
        ports:
            - "1883:1883"
        image: "mjenz/rpi-mosquitto"
    FG:
        privileged: true
        network_mode: "host"
        depends_on:
            - "mosquitto"
            - "mongoDB"
        volumes:
            - "/home/pi:/home/pi"
            - "/boot:/boot"
        image: "arkfreestyle/fg:v1.8"
        entrypoint: /app/docker-entrypoint.sh
        restart: unless-stopped

I'm using a mongoDB container for the raspberry pi built by someone else (I just pulled it from docker-hub), the issue is if for any reason there is an improper shutdown, the container fails to deal with the mongod.lock file in the /data/db directory.

In this case, upon trying to restart all containers, the mongodb container keeps exiting with status code 100, and the only way to fix it is to manually remove the lock file by running: sudo rm /data/db/mongod.lock in my terminal. However, I wish to automate this and run this command in my docker-compose file, either before the mongodb container starts or if it's attempting to restart.

I don't want to mess with mongodb's Dockerfile because it's built by someone else (is it good practice to make changes in other people's Dockerfiles btw?), so I want to use my docker-compose file. I can't use the entrypoint or command options because I don't want to override any default commands in the Dockerfile, I just want to run this extra command without messing with anything else:

sudo rm /data/db/mongod.lock

And I want to run this either before the mongodb container starts (worst case it will just output no file found), or if mongodb is trying to restart.

I'm open to more ideas on how you recommend tackling this issue as well, so any help is appreciated!

Upvotes: 3

Views: 2638

Answers (1)

Mihai
Mihai

Reputation: 10757

You can create your own Dockerfile that extends the original "andresvidal/rpi3-mongodb3:latest". If that one is using a script as ENTRYPOINT you can append your commands at the end of that file (eg. RUN echo xxx >> /docker-entrypoint.sh).

Then in the docker-compose you should use your own images that are based on the one developed by somebody else. And yes it is not a good idea to change Dockerfile's from others but if you see problems or improvements you should try and propose the changes to them.

My advice would be to not use "latest" when you import the original Dockerfile but a specific version. This way you are more in control of how you import future changes.

Upvotes: 1

Related Questions