-1

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

sudo docker run -d -p 8080:80 --env-file /home/test/localwork/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.

Update : In the dockerfile I already have the CMD command :

CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

How can I execute the above bash script with this CMD already being present?

dockerLearner
  • 49
  • 2
  • 9
  • can you post your Dockerfile? – Mihai Sep 15 '22 at 05:46
  • The usual way to do this is to create a script and put it in your ENTRYPOINT or CMD statement in your Dockerfile. The script should set up the credentials as you've shown and then do whatever the container does now on startup. – Hans Kilian Sep 15 '22 at 06:59
  • @HansKilian I already have the CMD command for apachectl. Please check the Update on the question. – dockerLearner Sep 17 '22 at 06:25
  • You can only run one command on container startup, so it needs to be a script that creates your config file and then runs apachectl. – Hans Kilian Sep 17 '22 at 06:30
  • @HansKilian Could you give me an example? I am trying hard to understand how CMD works in such scenarios – dockerLearner Sep 17 '22 at 06:32

2 Answers2

2

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
rezshar
  • 570
  • 1
  • 6
  • 20
1

Create a script that creates the config file and starts apache like this

#!/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
/usr/sbin/apachectl -D FOREGROUND

Let's assume that you've called the script 'myscript.sh' and placed it in the same folder as the Dockerfile.

Then modify your Dockerfile to copy the script, making sure that it's executable and then run it when the container starts (You might already be doing some of this).

... Rest of your Dockerfile
COPY myscript.sh .
RUN chmod +x myscript.sh
CMD ["./myscript.sh"]

Then build it and run it like you normally would with

sudo docker run -d -p 8080:80 --env-file /home/test/localwork/0014-test/dockerBuildScripts/scripts/env_variables.list test:1.0
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • This will work, but the problem is the env-file has the environment variables so those must load first in order to have the above script fetch the values from environment variables. – dockerLearner Sep 17 '22 at 07:17
  • My bad, this actually worked as expected. After doing some research I understood that the CMD command gets executed immediately after the docker run is executed to create a container. Hence the script could fetch the values from the env-file – dockerLearner Sep 17 '22 at 08:46