Home > Enterprise >  'DEBIAN_FRONTEND=noninteractive' not working inside shell script with apt-get
'DEBIAN_FRONTEND=noninteractive' not working inside shell script with apt-get

Time:12-06

I'm buildinga a docker image using a Dockerfile to build it. I have put ARG DEBIAN_FRONTEND=noninteractive in the beginning of the Dockerfile to avoid debconf warnings while building.

The warnings does not show up when using apt-get install inside the Dockerfile. However when executing a sh script (install_dependencies.sh) from the Dockerfile that contains apt-get install commands, the warnings show up again. I also tried to set DEBIAN_FRONTEND=noninteractive inside the sh script itself.

I can solve this by adding echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selections in the sh script before the apt-get install commands but I would want to avoid that, since any fail in the script would leave debconf select to Noninteractive.

Dockerfile:

FROM ubuntu:18.04

# Avoid warnings by switching to noninteractive
ARG DEBIAN_FRONTEND=noninteractive

WORKDIR /tmp

# Configure APT --> HERE THE WARNINGS 'debconf: unable to initialize frontend: Dialog' ARE NOT DISPLAYED
RUN apt-get update \
    && apt-get -y upgrade \
    && apt-get install -y \
    apt-utils \
    dialog \
    fakeroot \
    software-properties-common \
    2>&1

# Install APT packages --> HERE THE WARNINGS 'debconf: unable to initialize frontend: Dialog' ARE NOT DISPLAYED
RUN apt-get update && apt-get install -y \
    #
    # System packages
    iproute2 \
    procps \
    lsb-release \
    sudo \
    unattended-upgrades \
    dnsutils \
    iputils-ping \
    xauth \
    openssl \
    tar \
    zip \
    #
    # Helpers
    && apt-get install -y \
    ca-certificates \
    curl \
    wget \
    lsof \
    gconf2 \
    gconf-service \
    #
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

# Install LTE stack dependencies --> HERE THE WARNINGS 'debconf: unable to initialize frontend: Dialog' ARE DISPLAYED
RUN chmod  x install_dependencies.sh \
    && export DEBIAN_FRONTEND=noninteractive; ./install_dependencies.sh

install_dependencies.sh:

#!/bin/sh

export DEBIAN_FRONTEND=noninteractive

APT_PACKAGES="lib32z1 \
    python-setuptools \
    libmysqlclient-dev \
    ninja-build"

install_apt_packages() {
    sudo apt-get install -y tzdata \
        build-essential \
        git

    for package in $APT_PACKAGES;
    do
        sudo apt-get -y install "$package";
    done
}

main() {
    sudo apt-get update && sudo apt-get upgrade -y 
    install_apt_packages
}

main

EDIT: Thanks to @arkadiusz-drabczyk for telling me to remove sudo from the apt-get commands, it makes perfect sense what he says, that the environment variables drop before executing the command.

CodePudding user response:

Drop sudo in your script, there is point to use it if you're running as root. This is also the reason that DEBIAN_FRONTEND has no effect - sudo drops your current user's environment for security reasons, you'd have to use with -E option to make it work.

  • Related