Check If the Binary Exists Before Installing It in Docker

📖 2 minutes read

When you’re setting up a self-hosted service in Docker, you might reach for apt-get install ffmpeg in your Dockerfile. But many Docker images already ship with it — and installing a second copy just adds build time and image bloat.

The Pattern: Check Before Installing

Before adding any system dependency to your Dockerfile, check if the base image already includes it:

# Inside a running container
which ffmpeg
ffmpeg -version

# Or in Dockerfile
RUN which ffmpeg || apt-get update && apt-get install -y ffmpeg

Many application images (Nextcloud, Jellyfin, various media servers) bundle ffmpeg because they need it for thumbnail generation or transcoding. Installing it again is wasteful at best and can cause version conflicts at worst.

The Broader Lesson

This applies to any binary dependency:

  • ImageMagick — often pre-installed in PHP images
  • curl/wget — present in most base images
  • ffprobe — ships alongside ffmpeg
  • ghostscript — common in document processing images

The habit: which <binary> first, apt-get install second. Your Docker builds will be faster and your images smaller.

Conditional Install in Dockerfile

RUN if ! which ffmpeg > /dev/null 2>&1; then \
      apt-get update && apt-get install -y --no-install-recommends ffmpeg \
      && rm -rf /var/lib/apt/lists/*; \
    fi

One line of defense against unnecessary bloat. Check before you install.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *