How to Dockerize a Bash Script
In this tutorial I will show you how to Dockerize a Bash Script. Dockerizing an app is an easiest way to pack and ship it. I like to use docker for all my apps. It is easy to use and deploy. So, lets get started dockerizing the bash script.
- Docker installed
- Bash Script or use following Script
You may also like: How to Dockerize go (golang) web app
Sample Bash Script
Here I am creating sample bash script for this tutorial. I am creating a hello world script.
$ vim sample.sh
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
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.