Circumvent STDIN when installing packages with apt

lucassha

Shannon

Posted on December 20, 2021

Circumvent STDIN when installing packages with apt

Did you know there's an entire Unix command dedicated to generating y over and over? Or that you can make your apt package manager be completely noninteractive when installing or updating packages?

I recently went through this process when installing some packages and man pages in an Ubuntu Docker image and decided to write up a quick blurb about it for reference.


Here is the Dockerfile I started with:

FROM ubuntu:latest

WORKDIR /app

RUN apt-get update && apt-get install -y \
    openssl \
    curl \
    git \ 
    vim \
    dnsutils \
    man-db

CMD ["bash"]
Enter fullscreen mode Exit fullscreen mode

Unfortunately, this command fails because dnsutils tries to aggregate some information about your timezone region. Because of this, I wasn't able to just add a -y and call it good. After some Googling, I came across DEBIAN_FRONTEND=noninteractive, which forces zero interaction while updating or installing packages with apt. It will accept the default answer to all questions, which can be super useful in Dockerfiles, scripts, and I'm sure a million other things. So with this in mind, I updated the Dockerfile to include this as an environment variable:

FROM ubuntu:latest

WORKDIR /app

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    openssl \
    curl \
    git \ 
    vim \
    dnsutils \
    man-db

CMD ["bash"]
Enter fullscreen mode Exit fullscreen mode

Voila! First step solved.


However, I wanted one more thing in this Docker image by default, which is man pages. In order to get these, I need to run unminimize to expand all of the pages. Unfortunately, neither unminimize -y or unminimize --yes worked by default.

After some more Googling, I came across the yes Unix binary, which is incredibly straightforward: it just endlessly types y. For instance, you can just run yes in your terminal and watch it spam y endlessly.

In this case, I added RUN yes | unminimize to accept all of the defaults and run through the package installation, which is piping in y over and over to the command. And just like that, I have a working Docker image that can be spun up with some networking, man pages, and other stuff working by default!

Here is the final Dockerfile:

FROM ubuntu:latest

WORKDIR /app

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    openssl \
    curl \
    git \ 
    vim \
    dnsutils \
    man-db

RUN yes | unminimize

CMD ["bash"]
Enter fullscreen mode Exit fullscreen mode

And it could be run with something like this: docker build -t shannon:v0; docker run -it shannon:v0

💖 💪 🙅 🚩
lucassha
Shannon

Posted on December 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related