Develop bindings easily with Docker
Avoid installing dependencies on your machine
Imagine the situation: you want to write binding of FFmpeg for your favorite language, and of course you want to use your laptop to do so.
These are the steps:
- install FFmpeg on your computer
Wait, what do you say? You don’t install stuff? The only thing that runs on your laptop is docker containers?? Jessie, is that you?
Ok, let me try again:
- create a Docker image with FFmpeg in it.
- add to it your dev tools, your code, your dot files, and …
Ok, you don’t need to roll your eyes so hard, I get it!
So you want to basically edit your code on your laptop … but compile it from the container? Is that even possible?
Well, yes it is!
My development process
Spoiler alert: my solution to this is quite simple but I was so pleased with how it works that I decided to share it. Finally, for this example I will use Go as my programming language (after all, I’m a gopher) but you could do something similar with any language you like.
First I wrote a Dockerfile based on official golang Docker image that installs FFmpeg.
FROM golang# install FFmpeg
RUN apt-get update && apt-get install -y yasm pkg-config
RUN git clone https://github.com/FFmpeg/FFmpeg.git
RUN cd FFmpeg && ./configure && make && make install# this is the directory where the terminal will start
WORKDIR /go
CMD ["/bin/bash"]
Great, let’s build it and run it!
➜ docker build -t campoy/ffmpeg .
➜ docker run -it campoy/ffmpeg
Yes! We have now a bash running in a container where both go and FFmpeg are installed!
# go version
go version go1.4.2 linux/amd64# ffmpeg -version
ffmpeg version N-74181-gc1bfb99 Copyright (c) 2000–2015 the FFmpeg developers
...
But where is our code? We could add a line in our Dockerfile to add all the code we need, but then we should rebuild the container every time we change the code. Can we make this better?
Docker volumes FTW
This is where the cool stuff happens. We can add our code when we run our container rather than when we build it using Docker volumes.
➜ docker run -it -v $GOPATH:/go/src campoy/ffmpeg
Now if we do changes on any of the files in the mapped directory from your host the container will see the changes immediately and vice versa. So we can use our favorite editor from our laptop and run any tools that need the dependencies from the container.
And the best part is that this will help people contribute to your project! No need to explain how to install all the different dependencies your project might have, just add the Dockerfile to your repo!
Thanks for reading and please let me know your questions or comments either commenting here or at @francesc.