Recently I had the need to set up a custom Linux server running Docker which I would use to host multiple web apps and websites from multiple domains.
My approach was fairly simple and straightforward, having had some previous experience with shell scripting in a Windows server environment. I felt fairly comfortable taking on this project without help. Although I believe this would be a good project for beginners looking to improve their Linux skills.
In the end, it was actually quite simple. The hardest part was determining the Linux commands and the specific order that I wanted them to run in order to automate a task with a bash script.
Below is a walkthrough of the commands I used to update an existing running docker container/image. I will go line by line explaining each:
If you need help with starting out in a terminal, navigating to folders, listing files/folders, creating/deleting both files/and folders… Check this out: https://www.guru99.com/linux-commands-cheat-sheet.html
This first line is required in all Linux bash scripts, it refers to the default location for your bash executable file. In my case:
Next, we will delete the existing docker container by name. In my case, the name is “pomodoro-app”. This name was specified during the container creation by using the –name flag in the docker run command, you will see this in my script later. Please also remember this script is to automate the updating of an existing docker container/image. So you will have to manually create and start a container or have a container already running, or else this script won’t be able to start.
Let’s continue deleting the “pomodoro-app” docker container with this command:
- docker rm -f pomodoro-app || true
Next, we also need to delete any remaining images associated with this container. Search for any docker images that use the “pomodoro-app” repository name and attempt to remove them with the following command:
- docker rmi -f $(docker images pomodoro-app)
Remove any dangling images – be careful this causes problems if not used properly. This is why it’s optional. I found that on my server I was able to reduce disk usage by over 50% by running this occasionally.
- # docker system prune
Next, navigate to the “your_user” directory (replace “your_user” with the username for your Linux server). Delete current “pomodoro-app” directory:
- cd /
- cd home
- cd “your_user”
Once in the “your_user” directory, we will delete the existing project folder for our app in question:
- rm -r pomodoro-app
Next, we will clone the latest version from GitHub, into the very same directory we just deleted using:
Navigate into the pomodor-app folder we just cloned from GitHub:
- cd pomodoro-app
Build a new docker image:
- docker build -t pomodoro-app .
Start a new docker container from the updated portfolio-2023 image. I am using NGINX as a reverse proxy for multiple apps running on my server. You can replace XX with your port # in the example below (i.e. 3000, 3001, etc)
- docker run –restart always -p XXXX:XXXX -e VIRTUAL_HOST=yourdomain.com,www.yourdomain.com -e VIRTUAL_PATH=/pomodor/ –name=pomodoro-app pomodoro-app
Now we save this in a your_script.sh file within your usr/bin directory. In order to execute this script we will need to give rights by using this command:
- chmod u+x your_script.sh
Now we should be able to execute the script from the usr/bin folder using:
- sh your_script.sh
You can also run this script from a GitHub workflow… The topic of a future post!
Thanks for reading.