As a developer, working with Git, Docker, and Shell is a very common task. The command-line interface can be intimidating for beginners, but it is a powerful tool that can save time and streamline workflows. In this blog post, we will cover essential commands that I needed a lot recently and will help you as a developer to make your work more efficient and effective.
You can also find all commands explained in this post listed in this public gist that I update regularly.
Add execute rights to every shell script in the folder, to avoid that somebody else or your CI/CD pipeline cannot execute a script you have checked in.
git update-index --chmod=+x *.sh
List current files with details, to check which access rights your files have at the moment
git ls-files -s
List and stop Docker containers containing a string (SEARCH_STRING) in their name
docker ps | grep SEARCH_STRING | awk '{print $1}'
docker stop $(docker ps | grep SEARCH_STRING | awk '{print $1}')
After unnecessary containers have been stopped, it is essential to free up host resources. Use the following commands to achieve this:
docker container prune
docker image prune --filter "until=40h" --all
docker network prune
Get port of a container running in Docker Compose
docker-compose port SERVICE_NAME INT_PORT | grep -P ':\\d+' -o | grep -P '\\d+' -o
Common commands that you can use in Shell/Bash under Linux/Unix. If you are on windows most of the commands should also work if you use WSL. I’m using WSL heavily meanwhile especially to try deploy scripts, before pushing them to GitLab and can highly recommend it. Here are Shell commands I find especially useful:
Check if a directory is present
if [ -d "./asdf" ]
Check if a file is present
if [ -f "./asdf.txt" ]