Docker is a powerful tool that can simplify your everyday tasks. Whether you want to download some videos from YouTube, transform documents, or convert media files, Docker can be used for this much faster and more efficiently.
By running different CLI tools in Docker containers, you can ensure that always the newest version of the tools are used, and configuration settings are stored across different machines. Also, you can avoid cluttering your machine with unwanted software and dependencies.
In this article, I want to show some of the most useful CLI tools that can be run in Docker containers, including youtube-dl, FFmpeg, and curl. Additionally, I will explain how to use Docker to create a Linux machine for testing stuff and how to create a Docker cronjob that runs automatically.
Docker is a software platform that simplifies multiple processes by virtualizing the operating system of a computer on which it is installed and running.
To understand this imagine two different Node-based applications you want to host on a single computer. Due to some packages, one of the Node applications has to use version 14 of Node the other has to use version 18. As it is not possible to run different versions of Node installed on the server (or it is rather complicated) you aren't able to run both applications on the same computer.
Without Docker, you can get two physical machines or a single machine running three virtual machines.
With Docker, you can create a Docker image for every application and run all images on the same host. This solves the problem of different dependencies and enables you to host several Node-based applications on a single machine that all run different versions.
Now instead of running applications on your machine, Docker can run CLI tools in a container. The container will be started to execute the command and destroyed afterward to clean up space.
Using this approach to run different tools has multiple advantages:
- You will avoid cluttering your machine with unnecessary software and dependencies.
- You can ensure that you are always using the latest version of the CLI tool without manually updating it.
- You can share the same configuration and settings across multiple machines by running the same Docker container.
Download Any Video With YouTube-dl
youtube-dl is a command-line tool for downloading and converting videos from multiple websites, including YouTube, Vimeo, and Dailymotion. It supports a wide range of formats and resolutions and is mainly used to download entire playlists or videos from channels.
To download any video from YouTube simply run the following command and replace
YOUR_YOUTUBE_VIDEO_URL with the video, you want to download:
docker run --rm -i \ -e PGID=$(id -g) -e PUID=$(id -u) \ -v "$(pwd)":/workdir:rw \ ghcr.io/mikenye/docker-youtube-dl:latest \ YOUR_YOUTUBE_VIDEO_URL
If you encounter videos that are behind user authentication you can use a configuration file before starting the Docker container. Also, you can schedule downloads of YouTube subscriptions. Have a look at the GitHub documentation to find out everything you can do with youtube-dl
Professional video editing with FFmpeg
FFmpeg is a command-line tool that can be used for converting and editing video and audio files. It supports a wide range of formats and is used to perform different video and audio processing tasks, including trimming, cropping, resizing, and adding subtitles.
Instead of installing it on your host and ensuring using the latest version, the tool can be used in a Docker container. FFmpeg has multiple commands that can be found in the official documentation and used with the Docker container.
In the following snippet, FFmpeg is used with Docker and will extract 5s from a given video starting from 00:49:42 and create a GIF:
docker run jrottenberg/ffmpeg -stats \ -i http://archive.org/download/thethreeagesbusterkeaton/Buster.Keaton.The.Three.Ages.ogv \ -loop 0 \ -final_delay 500 -c:v gif -f gif -ss 00:49:42 -t 5 - > throw_ball.gif
The following snippet is used to transform the previously created GIF into an MP4 video:
docker run -v $(pwd):$(pwd) -w $(pwd) \ jrottenberg/ffmpeg:3.2-scratch -stats \ -i throw_ball.gif \ throw_ball.mp4
More examples of using the Docker FFmpeg container can be found on the official Docker Hub page.
Have Fun With Curl
Curl is a basic Linux command-line tool for transferring data over various protocols, including HTTP(S), FTP, and SMTP (and many more). You can use it to perform HTTP requests, test REST API calls, and download all kinds of files.
By using Curl within a Docker container, you can avoid installing it on your host machine. Additionally, you can ensure that you are always using the latest version of the tool without having to manually update it. Furthermore, you can use it on systems where curl is not installed natively.
The following snippet will show how a simple file is downloaded from GitHub with Curl and displayed within the CLI:
docker run --rm curlimages/curl:8.00.1 -L -v https://raw.githubusercontent.com/paulknulst/paulknulst/master/README.md
If you want to download files and store them on your local machine, you can also do this with Curl and Docker:
docker run --rm -it -v "$PWD:/data" curlimages/curl curl -o /data/readme.md https://raw.githubusercontent.com/paulknulst/paulknulst/master/README.md
readme.md) and the URL with anything you want to download.
Spin up a Linux Instance
Although Docker is a containerization technology not designed to create full-fledged Linux instances like virtual machines, it is possible to use Docker to create a container that runs a Linux distribution like Ubuntu. This can be seen as a lightweight alternative to a virtual machine providing everything you need to work with Linux.
To create a Docker container that runs Linux and instantly connect to that container execute the following command:
docker run -it --name ubuntu-homelab ubuntu /bin/bash
-itargument attaches a pseudo-TTY to enable interaction with the container. Adding
/bin/bashwill start a Bash shell inside the container.
After the command executes, you are inside the Linux container and can run commands in the terminal. You can install any package by using
apt-get. For example, this code snippet installs
apt-get update apt-get install vim
If you want to exit the container, you can either type
exit or press CTRL+D which will exit and stop the container.
To reconnect to the container and start experimenting again, you first have to manually start it by executing (unless it isn't started):
docker start ubuntu-homelab
If it is started, you can access the container by executing:
docker exec -it ubuntu-homelab bash
To remove the container and free up space execute:
docker rm ubuntu-homelab
Automate The Boring Stuff With Docker
With Docker, it is possible to automate nearly everything using the Linux Crontab functionality. To do this, you need a Dockerfile that you can use as a starting point:
FROM alpine:latest RUN apk update && apk add curl RUN apk add dcron WORKDIR /app COPY ./crontab /etc/crontabs/root CMD crond -f -l 2
This Dockerfile creates a container based on Alpine Linux, installs
dcron, and sets up a cron job that will execute every minute if the container runs.
Save the Dockerfile in any directory and create another file called
crontab which will contain the command that should be executed:
# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To learn more about the format of this file, run 'man 5 crontab' # Execute my-script.sh every day at midnight 0 0 * * * /my-script.sh
Now, you can build and tag an image by running the following command in the same directory:
docker build -t personal-cron-container .
After the image is built and tagged, you can run the container by using the following command:
docker run -d --name midnight-script personal-cron-container
A new container named
midnight-script will be created from the
personal-cron-container image in detached mode that will run the
crond command and execute the cron job defined in the crontab file.
To get inspiration for a useful cron job, you can read a tutorial from me explaining how to encrypt database backups and upload them to the google cloud using a basic cron job.
In this guide, I showed that Docker can greatly simplify your workflow and improve your productivity if you use it. Many tools and techniques can be outsourced into a Docker container to avoid clutter on your host machine. Also, Docker can ensure that you are always using the latest versions of any tool.
This will help you to work more efficiently and with greater flexibility.
Additionally, Docker provides powerful features such as the ability to create a lightweight Linux machine which will be helpful if you want to test out new packages without cluttering your host system.
With these tools and techniques, you can take your development and workflow to the next level. So give Docker a try and see how it can help you in your everyday tasks.
How about you? Do you already use Docker containers? Do you have any other CLI tools? Also, do you have any questions about using Docker? I would love to hear your thoughts. Please share everything in the comments.