Running a custom script using entrypoint in docker-compose

An alternative solution that worked for me was modifying entrypoint by placing /bin/sh.It looked a bit like this afterwards

version: '3'
services:
  web:
    build: .
    volumes:
    - .:/code
    entrypoint :  
    - /bin/sh
    - ./test.sh
    ports:
    - "5000:5000 

where test.sh is the required bash script to be run inside the container.


It looks like you need to map your volume to /docker-entrypoint-initdb.d/

version: '3'
services:
  solr:
    image: solr
    ports:
     - "8983:8983"
    volumes:
      - ./solr/init.sh:/docker-entrypoint-initdb.d/init.sh
      - ./solr/data:/opt/solr/server/solr/mycores
    entrypoint:
      - docker-entrypoint.sh
      - init

From

https://hub.docker.com/_/solr/

Extending the image The docker-solr image has an extension mechanism. At run time, before starting Solr, the container will execute scripts in the /docker-entrypoint-initdb.d/ directory. You can add your own scripts there either by using mounted volumes or by using a custom Dockerfile. These scripts can for example copy a core directory with pre-loaded data for continuous integration testing, or modify the Solr configuration.

The docker-entrypoint.sh seems to be responsible for running the sh scripts based on the arguments passed to it. So init is the first argument which in turn tries to run init.sh

docker-compose logs solr | head

Update 1:

I had struggled to get this to work and finally figured out why my docker-compose was not working while the docker run -v pointing to the /docker-entrypoint-initdb.d/init.sh was working.

It turns out that removing the entrypoint tree was the solution. Here's my final docker-compose:

version: '3'
services:
  solr:
    image: solr:6.6-alpine
    ports:
     - "8983:8983"
    volumes:
      - ./solr/data/:/opt/solr/server/solr/
      - ./solr/config/init.sh:/docker-entrypoint-initdb.d/init.sh

my ./solr/config/init.sh

#!/bin/bash
echo "running"
touch /opt/solr/server/solr/test.txt;
echo "test" > /opt/solr/server/solr/test.txt;