nordvpn

How to Dockerize Bash Script (Shell Script)

How to Dockerize Bash Script

In this tutorial, I will show you how to Dockerize Bash Script. Dockerizing an app is the easiest way to pack and ship it. I like to use Docker for all my apps. It is easy to use and deploy. So, let’s get started dockerizing the bash script.

Requirement

  • Docker installed
  • Bash Script

You may also like: How to Dockerize go (golang) web app

Sample Bash Script

Here I am creating a sample bash script for this tutorial. I am creating a hello world script.

$ vim sample.sh

#!/bin/bash
echo Hello World!

Dockerize the Script

Now its time to dockerize the Script. Firstly, add Dockerfile in the root of your app.

$ vim Dockerfile

Then paste the following content into Dockerfile:

FROM ubuntu
ADD ./sample.sh /usr/src/sample.sh
RUN chmod +x /usr/src/sample.sh
CMD ["/usr/src/sample.sh"]

Build your app

After creating Dockerfile its time to build your app. You can use docker build command to build the docker image. We will also tag our image with -t option.

$ docker build -t sample .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM ubuntu
---> f975c5035748
Step 2/4 : ADD ./sample.sh /usr/src/sample.sh
---> Using cache
---> 3c9637b399e3
Step 3/4 : RUN chmod +x /usr/src/sample.sh
---> Running in a1a4839148d3
Removing intermediate container a1a4839148d3
---> c8d7eb17a3a2
Step 4/4 : CMD ["/usr/src/sample.sh"]
---> Running in e5daa54f6cfa
Removing intermediate container e5daa54f6cfa
---> b968199ada0e
Successfully built b968199ada0e
Successfully tagged sample:latest

Test your Image

Now its time to test your docker image.

$ docker run -it sample
Hello World!

Now you can see it is working as expected.

Please let me know if you like this tutorial.

If you have any issues please feel free to comment below.

 

1 Comment Posted

  1. hello sir, thanks for trying to help others in this confusing field.

    please explain the code/commands.

    it may take you 3x the time to write a tutorial that way – but it will help 10x more people in the end.

    this style of writing a guide is what i often call a half-assed guide. this project is super simple, so it is not a big problem this time. but a project a little more complex and it would do more damage than good for the noobs who stumble over a half-assed guide.

    they are fine for professionals but not for those who typically search for basic programming solutions, and those who need help the most.

    half-assed guides are those who make it a living hell to learn programming at times. often you end up messing up your linux OS as a result etc…at its worst lol.

Leave a Reply

Your email address will not be published.


*



The reCAPTCHA verification period has expired. Please reload the page.

48-Hour Flash Sale! Courses from just $10.99