Docker runs local script without hosts

The goal is to add data to the database server containers from a multi-container web application from boot using curl when the database containers are running. I can do this from docker-compose.yml or from docker startup independently of the web app if I am using host volumes.

How can I do this without using host volumes or specific Dockerfiles?

Example Docker Compose with host volumes:

dbinit:
build: ./webtools_config/initdb
command: bash -c "/tmp/webtools_config/dbinit.sh"
volumes:
 - ./webtools_config:/tmp/webtools_config
links:
 - db1
 - db2

      

A docker run example that I would like to pass to a local script file for a docker client, eg. /dbinit.sh:

docker run -a stdin -a stdout -i -t \
--link dir_db1_1:db1 \
--link dir_db2_1:db2 \
initdb /bin/sh -c "./dbinit.sh"

      

+3


source to share


2 answers


The solutions I have found are as follows:

docker run tomdavidson/initdb bash -c "`cat initdb.sh`"

      

and

Set ENV VAR equal to your script and configure the Docker image to run the script (ADD / COPY and use of host volumes of course, but that's not the question), for example:



docker run -d -e ADD_INIT_SCRIPT="`cat custom-script.sh`" tomdavidson/debian 

      

tomdavidson / debian CMD runs the script with:

if [ "${ADD_INIT_SCRIPT}" != "**None**" ]; then
  echo "Executing ADD_INIT_SCRIPT ..."
  bash -c "${ADD_INIT_SCRIPT}"
fi

      

https://registry.hub.docker.com/u/tomdavidson/debian/

+1


source


If you understand correctly, you can remove the host volume mapping using building with this script.

Your Dockerfile:



# Dockerfile 
...
ADD your-script-on-host.sh /app/your-script-in-container.sh
RUN /app/your-script-in-container.sh
# Your CMD here

      

Please note that you will only be able to update and run this script in the building .

0


source







All Articles