Docker image add editor

Problem

You have seen “unable to locate package” often enough. You have used apt-get update to add your editor to an Docker image so often that you already type it without thinking. First thing you enter into a docker container.

You are annoyed and want to automate having it.

Solution

In your Dockerfile as early as possible (but behind the “FROM” command) add this:

RUN apt-get -y update \
  && apt-get install -y $YourEditorOfChoice \
  # Cleanup apt cache
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

Explanation

This simple statement uses the RUN command of the Docker Build Environment to execute a statement within the container.

As I showed here, apt-get update retrieves the data required to create the cache for the actual installation process.

apt-get install $YourEditorOfChoice (mine is nano) lets you add your editor into the docker image. Which means the editor will be there after each restart of the container. Less hassle *yay*

A tip:

Each Docker Command in the Dockerfile is a separate piece. If you add

RUN apt-get -y update
RUN apt-get install -y $YourEditorOfChoice 

to your Dockerfile it will still give you the error message unable to locate package.

Just make sure that you combine the cache creation and the installation into one RUN command and you are good to go.

You likely don’t need to install other programs after your automate installing your editor. So apt-get clean cleans the cache required for the installation procedure.

Deleting the contents of /var/lib/apt/lists/ cleans up even further. Although, in comparison to the cache, it is minor.

Background

The Docker build environment offers multiple commands. Put together like a recipe they yield the finished container you wanted.

The RUN command allows you to run commands inside the container.

Let me know when it helped.

Best,

Frank

Sources:

https://stackoverflow.com/questions/27273412/cannot-install-packages-inside-docker-ubuntu-image

Leave a Reply