How to simplify a Docker run command

I recently wanted to create animated GIFs from videos. The idea was to get video previews, in a very lightweight file. After a quick search online, I found FFMPEG, a fantastic multimedia framework to manipulate media. There is also a few wrappers that exists in different languages (ex: C#, JavaScript) but you still need to install FFMPEG locally, and I didn't want that. In fact, I wanted a simple solution that doesn't require any installation locally and something in the cloud. In this post, I want to share how I achieved the first one.

All the code and the container are available on Github and Docker Hub.

First Contact

The ffmpeg framework is very powerful and can do so many things; therefore it's normal that it has a ton of possible parameters and extensions. After time spent on the documentation and a few trials and errors, I found how to do exactly what I needed calling it this way:

ffmpeg -r 60 -i $INPUTFILE -loop 0 -vf scale=320:-1 -c:v gif -f gif -ss 00:00:00.500 -r 10 -t 5 - > $OUTPUTFILE

This will create a five second animated GIF from a video. It speeds up the video and lowers the framerate of the GIF to keep the output lightweight. Here is an example.

Hello World episode 5 seconds preview

This is great, but this is not very friendly. How can someone who only creates a video once in a while be expected to remember all those parameters?! And even harder, when the video is vertical some parameters have different values. It was time to simplify, and here is how I did it. Note that I'm a Docker beginner and if you think there is a simpler or better way to do some steps, let me know, and let's learn together.

The Plan

The plan is simple: execute a simple Docker command like docker run fboucher/aciffmpeg -i NotInTheSky.mp4 and generate a video preview. To build our ephemeral container we will start with something lightweight like alpine, install ffmpeg and add a script that would be executed as the container runs. That sounds like an excellent plan, let's do it!

Writing the Script

The script is simple, but I learned a few things writing it. This is why it's included in this post. The goal was simple: execute the ffmpeg command using some values from the parameters: file path, and if the video is vertical. Here is the script:

#!/bin/sh

while getopts ":i:v" opt; do
  case $opt in
    i) inputFile="$OPTARG"
    ;;
    v) isVertical=true
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    exit 1
    ;;
  esac

  case $OPTARG in
    -*) echo "Option $opt needs a valid argument"
    exit 1
    ;;
  esac
done

if [ -z "$isVertical" ]; then isVertical=false; fi

# used for bash 
#IFS='.'
#read -a filePart <<< "$inputFile"
#outputFile="${filePart[0]}.gif"

# used for dash 
filename=$(echo "$inputFile" | cut -d "." -f 1)
outputFile="$filename.gif"

if $isVertical
then
  ffmpeg -r 60 -i $inputFile -loop 0 -vf scale=-1:320 -c:v gif -f gif -ss 00:00:00.500 -r 10 -t 5 - > $outputFile
else
  ffmpeg -r 60 -i $inputFile -loop 0 -vf scale=320:-1 -c:v gif -f gif -ss 00:00:00.500 -r 10 -t 5 - > $outputFile
fi

Things I learned: Parameter without values

The script needs to be as friendly as possible, therefore any unnecessary information should be removed. Most videos will be horizontal, so let's make the parameter optional. However, I don't want users to have to specify the value script.sh -i myvideo.mp4 -v true but instead script.sh -i myvideo.mp4 -v. This is very simple to do, once you know it. On the first line of code when I get the parameters: getopts ":i:v" notes that there is no ":" after the "v". This is to specify that we are not expecting any values.

Things I Learned: Bash and Dash

As mentioned earlier the container will be built from Alpine. And Alpine doesn't have bash but instead uses dash as a shell. It's mostly the same, but there are some differences. The first one will be the shebang (aka "#!/bin/sh" on the first line). And the second was the string manipulation. To generate a new file with the same name but a different extension of the script, split the file name at the ".". This can be done IFS ... read... <<< command (commented in the script) on bash but this will give syntax error: unexpected redirection and this is because there is no <<< in bash. Instead, you need to use the command cut -d "." -f 1 (where -d specifies the CHAR to use as the delimiter, and -f return only this field).

Building the image

It's now time to connect all the dots in the dockerfile.

FROM alpine:3.13
LABEL Name=aciffmpeg Version=0.0.2
RUN apk add ffmpeg
COPY ./src/myscript.sh /
RUN chmod +x /myscript.sh
ENTRYPOINT ["/myscript.sh"]

The file is not extremely complex but let’s pass through it line by line. 

  • We start FROM Alpine version 3.13 and apply a LABEL
  • RUN Will execute the command to install ffmpeg. The apk is the default utility on Alpine to install apps just like apt on Ubuntu. 
  • COPY Is copying the script from our local machine into the container at the root. 
  • The second RUN command is to make sure the script is executable. 
  • Finally, ENTRYPOINT will allow us to configure the container to run as an executable in this case as the script. All parameters passed to Docker will be passed to the script.

The only things left now are to build, tag, and push it on Docker Hub.

docker build -t fboucher/aciffmpeg .

docker tag  0f42a672d000 fboucher/aciffmpeg:2.0

docker push fboucher/aciffmpeg:2.0

The Simplified version

And now to create a preview of any video you just need to map a volume and specify the file path and optionally mention if the video is vertical.

On Linux/ WSL the command would look like this:

docker run -v /mnt/c/dev/test:/video fboucher/aciffmpeg -i /video/sample.mp4 -v

And on PowerShell like that:

docker run -v c/dev/test:/video fboucher/aciffmpeg -i /video/sample.mp4 -v

I learned a lot about Docker doing that project and now I have a very useful tool. What are the tools you built using containers that simplify your life or work?

Video Version

I recorded a video version if you are interested. 


~frank



Reading Notes #502


Good Monday, Already time to share new reading notes. Here is a list of all the articles, blog posts, and podcast episodes that catch my interest during the week. 

If you think you may have interesting content, share it!

Cloud

Programming

Podcasts

Miscellaneous


~frank


Reading Notes #501


Another Monday, another Reading Notes. 😀

It's been five hundred weeks, this is more than nine years! Who knew I will keep doing that for so long... 

If you are new around here, welcome, The Reading Notes are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

If you think you may have interesting content, share it!

Cloud

Programming

Miscellaneous

Miscellaneous


~Frank


Reading Notes #500

It's Monday,
time to share my reading notes. Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

You think you may have interesting content, share it!

Cloud

Programming

Miscellaneous


~frank

Reading Notes #499

Good Monday, 

Monday means it's reading notes times. 
Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed. 

You think you may have interesting content, share it!

Suggestion of the week

Cloud

  • Azure Apps Autopilot (Justin Yoo) - A great DevOps post that great an automatic deployment process. Very inspiring, I think I may is some of it for my AzUrlShortener.

Programming

Podcasts


~frank

Reading Notes #498


Good Monday, Already time to share my reading notes of the week. A list of all the articles, blog posts, that catch my interest during the week.

You think you may have interesting content, share it!

Cloud

Programming

Miscellaneous


~frank

Reading Notes #497


Good Monday, 
Already time to share new reading notes. Here is a list of all the articles, blog posts, and podcast episodes that catch my interest during the week. 

You think you may have interesting content, share it!

Cloud

Programming

Podcast

Miscellaneous

~frank

Reading Notes #496

Good Monday, 

time to share my reading notes. Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed. 

You think you may have interesting content, share it!

Cloud

Programming

Podcasts

  • Make money with open source software (Software Engineering Unlocked) - Open source software is another world: Unknow, different, scary. This week guest shares his experience and story, while transitioning into Open-Source.

~Frank


Reading Notes #495


Good Monday, 
Already time to share new reading notes. It is a habit I started a long time ago where I share a list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week. 


Suggestion of the week

  • Include diagrams in your Markdown files with Mermaid (Martin Woodward, Adam Biagianti) - This is very interesting. For a simple little diagram, we can now do that in text in the same document... Looking forward to trying it. Mermaid is also supported in Azure DevOps

Cloud

Programming

Book



Author: Matthew McConaughey

This autobiography is funny, light, interesting, and even weird at some moments. But you know what? I liked it. I totally kept some quotes, as I found them good. I partially read it and listen to it. The audio version is done by Matthew and it's nice to hear him telling us his story.




~frank

 

Reading Notes #493


Good Monday, Already time to share new reading notes. It is a habit I started a long time ago where I share a list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week. 

Just for the of it, I did a little funny video to answer the question: Where really is cloud computing? 

Watch it on your favorite platform ;) YouTube - TikTok

Cloud

Programming

Podcasts


~Frank

Reading Notes #492


Good Monday, 
Already time to share new reading notes. It is a habit I started a long time ago where I share a list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week. 

You think you may have interesting content, share it!

Cloud

Programming

Podcasts

Miscellaneous


~frank


Reading Notes #491


Good Monday, already time to share new reading notes. 
It is a habit I started a long time ago where I share a list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week.

You think you may have interesting content, share it!

Cloud


Programming


Databases


Miscellaneous


~frank

Reading Notes #490

Good Monday, time to share my reading notes. 
It's a habit I started a long time ago where I share a list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

You think you may have interesting content, share it!

Cloud


Programming


Podcast

  • 561: How to Reduce Burnout, with Jennifer Moss (Coaching for Leaders) - As I was listening that episode, I realized that the team I'm in we already did a lot of those things. The past months were hard for all of us but I think we did well. Very interesting episode, and it could be a good idea to share to your teammates.
  • 288: Turning Hacks into Reality (Merge Conflict) - Super interesting to listen Frank and James as they share their progress about there App development and how a simple "wind speed" question became a thing!
  • No More Lonely Friends with Marissa Meizz (A Bit of Optimism) - Wow! It's always very impressive what can happends when you mix social media and good intention.
  • 562: How to Make Progress When Starting Something New, with Michael Bungay Stanier (Coaching for Leaders) - Very nice episode with Michael Bungay Stanier, author of the book also poart of this Reading Notes (but not the book they talk in the episode), about how to get started. Love it, very honest, and real.

Miscellaneous


Books


The Advice Trap: Be Humble, Stay Curious & Change the Way You Lead Forever
 

(Michael Bungay Stanier) 

- I really liked this book. Yes, I read The Coaching Habit, that's a nice one too, but in The Advice Trap has something that feels more adapted for when we get started. The book shares many gems and important points to get us started on a better path (or to change our habits). It's may not necessarily be easy, but it's clear what needs to be done, well in this case not done.


~frank

Reading Notes #489


Suggestion of the week

Cloud

Programming

Miscellaneous


~frank

Reading Notes #488


Good Monday, time to share my reading notes. Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

You think you may have interesting content, share it!

Programming

Books

Effortless: Make It Easier to Do What Matters Most 
(Greg McKeown) 

- Nice continuity of his first book Essentialism. I appreciated this reading. Many simple and easy ideas that we can start to apply. If interested in this topic Greg also has a podcast: What's Essential.






~Frank

Reading Notes #487

It's... Tuesday! Yes I know one day later, but it's still time to share my reading notes. 

Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed. 

You think you may have interesting content, share it!

Suggestion of the week

Cloud

Programming

Podcasts

  • Egil Hansen on Blazor Testing with bUnit - Episode 169 (Azure DevOps Podcast) - It's the second time I hear good things about bUnit. This is an open-source library for testing Blazor components that can be paired with many popular test frameworks. I need to try it.

  • Podcast Tags: Are They Still Relevant? (Feed Your Brand) - It all makes sense. I am not 100% sure about YouTube however because all captions are indexed... I think. But if you create content listen to this.

  • Family with Ari Emanuel (A Bit of Optimism) - The best way to learn and grow is to try and fail ( aka fall). A great episode very interesting.



☁️ Frank

Reading Notes #486


It's Monday (the cyber one), time to share my reading notes. Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed. 

You think you may have interesting content, share it!

 

Cloud

Programming

~Frank

Reading Notes #485

It's Monday,
time to share my reading notes. Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed. 

You think you may have interesting content, share it!

Cloud

Programming

Miscellaneous

  • After Microsoft Ignite, keep the learning momentum going (Jeff Sandquist) - Very happy to see this news being public.

  • How to get Windows 11 (John Cable) - It's out now and honestly, it is great! If you didn't get the automatic upgrade maybe it's because you don't meet the requirement. Following the steps, it could be a simple checkbox to check-in your current version.

  • Introduction to GitHub Actions for .Net Developers (Dave Murray) - I'm not sure why but I always felt called by automation. Maybe because it's the ultimate proactivity of any task.GitHub Action is one of those extremely powerful tools that are worth your attention.

~frank

Reading Notes #484

It's Monday, time to share my reading notes. Those are a curated list of all the articles, blog posts, podcast episodes, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

You think you may have interesting content, share it!


Cloud

Programming


~Frank

Reading Notes #483


Already Tuesday! Time for a new Reading Notes post; a list of all the articles, blog posts, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

This week is the Cloud Summit (https://azuresummit.live) is an 11-day free conference that focuses on Azure, there is surely a session that will catch your interest.

You think you may have interesting content, share it!


Cloud


Programming


Miscellaneous


~Frank


Reading Notes #482


Another Monday, a new reading notes; a list of all the articles, blog posts, and books that catch my interest during the week and that I found interesting. It's a mix of the actuality and what I consumed.

You think you may have interesting content, share it!

Cloud

Programming

Podcast


~frank


Reading Notes #481

Frank's Lego R2D2

Cloud


Programming


Podcast


~Frank