Home > front end >  How to run a bash script during docker run?
How to run a bash script during docker run?

Time:09-17

After building the image I am executing the below command to containerize the docker image:

sudo docker run -d -p 8080:80 \
  --env-file ./0014-test/dockerBuildScripts/scripts/env_variables.list \
  test:1.0

I have a bash script that sets the database credentials in a file:

#!/bin/bash
echo "SetEnv dbname ${dbname}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbuser ${dbuser}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbpass ${dbpass}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbhost ${dbhost}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbport ${dbport}" >> /etc/apache2/conf-enabled/environment.conf

I specifically want to run this file during or after the docker run within the docker since I am creating the environment variables during the docker run and using it in the above bash script.

CodePudding user response:

at first create your script as below:

cat > script.sh <<EOF
#!/bin/bash
echo "SetEnv dbname ${dbname}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbuser ${dbuser}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbpass ${dbpass}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbhost ${dbhost}" >> /etc/apache2/conf-enabled/environment.conf
echo "SetEnv dbport ${dbport}" >> /etc/apache2/conf-enabled/environment.conf
EOF

then use --entrypoint for run on startup.

sudo docker run -d -p 8080:80 \
  --entrypoint ["/bin/bash script.sh"] \
  --env-file ./0014-test/dockerBuildScripts/scripts/env_variables.list \
  test:1.0
  • Related