Docker Community Forums

Share and learn in the Docker community.

Creating a shell script to install and dependencies which could be run using docker command


(Lamgadekamal) #1

I am trying to create a shell script in mac which performs the following tasks as below in my dockerfile. I couldn’t find a proper resources which gives the format for shell script which can install and run various dependencies. Any link to good resources or idea would be helpful.

–docker file for reference–

#setting base image
FROM selenium/standalone-chrome-debug

USER root

#Set the working directory to /tinderbox2_frontend
WORKDIR /tinderbox2_frontend

#Access to the project within docker container - Bundle app source
COPY . /tinderbox2_frontend

#Install Node.js
RUN apt-get update
RUN apt-get install --yes curl

#configuring nodeSource repository to get enough version of nodejs
RUN apt-get install -y nodejs

#configuring debian package repository
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo “deb https://dl.yarnpkg.com/debian/ stable main” | tee /etc/apt/sources.list.d/yarn.list

#installing yarn
RUN apt-get update && apt-get install -y yarn

#installing binary git package
RUN apt-get install -y git

#running yarn
RUN yarn


(Sam) #2

sorry, unclear what you are seeking…

everything that is ‘run’ in the dockerfile ‘could’ be put into a shell script (except that last run yarn) that runs AFTER the container starts,
BUT… then the container readiness takes a lot longer, every time the container is started…

and, you might get new dependencies code, and have failures which you cannot recreate…


(Lamgadekamal) #3

so basically in shell script, there must be RUN command and then whatever the installation command of any dependency?

Also, is it possible to use the shell script file inside a docker file? So when i run the docker image, it automatically runs the script and install all the dependencies required.


(Sam) #4

a shell script is a unix bash or sh pgm script…

RUN is a docker file command… not in a bash script…

apt-get is a unix command to manage pgm installs… (on some versions of unix, there are multiple, mac is different, centos is different)
so, you can collapse the apt-get install xxx to one line install xxx yyy zzz qqq whatever.

copy moves files from outside the image at build time to inside the image… so that is a dockerfile only command. unless you mounted a host volume into the container at docker run time (see --v or --mount options of docker run), then you copy files from the host volume into the container (not image) at docker run time.
unless specified otherwise, the container runs as user root…

so this

USER root

#Set the working directory to /tinderbox2_frontend
WORKDIR /tinderbox2_frontend

#Access to the project within docker container - Bundle app source
COPY . /tinderbox2_frontend

#Install Node.js
RUN apt-get update
RUN apt-get install --yes curl

#configuring nodeSource repository to get enough version of nodejs
RUN apt-get install -y nodejs

#configuring debian package repository
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo “deb https://dl.yarnpkg.com/debian/ stable main” | tee /etc/apt/sources.list.d/yarn.list

#installing yarn
RUN apt-get update && apt-get install -y yarn

#installing binary git package
RUN apt-get install -y git
ENTRYPOINT ["yarn"]

could collapse to this
Dockerfile

#setting base image
FROM selenium/standalone-chrome-debug
copy setup.sh .
# execute yarn
ENTRYPOINT ["/setup.sh"]

and in a shell script (setup.sh)

#!/bin/bash
#Set the working directory to /tinderbox2_frontend
mkdir tinderbox2_frontend
cd tinderbox2_frontend

# update the catalog of available components
apt-get update

#Install curl to get info for later installs
apt-get install --yes curl  

#configuring debian package repository
# curl needed for this, before installing yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo “deb https://dl.yarnpkg.com/debian/ stable main” | tee /etc/apt/sources.list.d/yarn.list

#installing yarn and nodejs
RUN apt-get update && apt-get install -y yarn nodejs
# execute
yarn

git is a development time application, not typically deployed IN a container…
typicall you would do a git pull on the host, and then copy the files for the particular branch to the image
for use at container run time.

but… having the script means you will install all this EVERY TIME you start a container with this image.
vs one time with the dockerfile/build.


(Lamgadekamal) #5

this is great and detailed explanation. As I am new to docker and even env. got to learn a lot from your information. cheers


(Sam) #6

forget docker for a minute…

your task is to give a running application to someone, with the tiniest setup required,

what would you do?

pick a platform
pick a runtime library (or more than one)
install some stuff, 
make some folders (holds code, config,  logging, backups, whatever)
and set the app to start when the system is booted.

and then you would back that up to a tarball, and have the user restore it and voila!.

THAT is a Dockerfile. run build against it, create a runable image
poof…they run that image, they get all that work done… configured, its runs…

and maybe a place to put data for that app… in a database

you would do all that again

platform
database
install
configure 
run

the first could connect to the database remotely… you add some config…
and then u tell the first container a little bit


(Lamgadekamal) #7

Just wondering is it possible to run a bash script using docker command without creating a dockerfile ?


(Sam) #8

Well, yes, but u have to get the script into the container. Docker always runs some image. U could stat w Ubuntu, then figure out how to inject the script. Or use a volume mount