Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash scripts exits on entering docker container

Im trying to create a bash script for setting up docker and an application on a fresh server.

pretty fresh to bash itself but i think ive got the just

heres a snippet where my issue lies -

#
docker run -d --name application -p 80:80 -d  tutum/apache-php
docker run -d -p 3306:3306 --name=database --
env="MYSQL_ROOT_PASSWORD=password1" mysql:latest
echo "--------------------------------------------------------------------"
echo "Docker is all done - run docker ps -a to see all created 
containers!"
echo "--------------------------------------------------------------------"
echo "Moving onto installing application into app container!"
echo "--------------------------------------------------------------------"
docker exec -it application bash

apt-get update & apt-get install git & cd /var/www/html
#

on the line - docker exec -it application bash

It enters the container as expected but the bash script then stops because of this meaning the following commands after don't run

Is there anyway I can get around this? I don't think there is but in case there are any bash wizards out there!

Any help appreciated!

like image 804
Kolvin Avatar asked Jul 25 '26 05:07

Kolvin


1 Answers

That is what -it is supposed to do. It means a interactive bash and it will only exit when you are done with it. Change your code as below

docker run -d --name application -p 80:80 -d  tutum/apache-php
docker run -d -p 3306:3306 --name=database --
env="MYSQL_ROOT_PASSWORD=password1" mysql:latest
echo "--------------------------------------------------------------------"
echo "Docker is all done - run docker ps -a to see all created 
containers!"
echo "--------------------------------------------------------------------"
echo "Moving onto installing application into app container!"
echo "--------------------------------------------------------------------"

docker exec application bash -c "apt-get update & apt-get install git -y"
docker exec -it application bash -c "cd /var/www/html; exec bash"
like image 163
Tarun Lalwani Avatar answered Jul 27 '26 19:07

Tarun Lalwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!