JollyRoger
JollyRoger

Reputation: 857

Can't edit environment file with sudo inside Docker container

I have a docker container which is builded on ubuntu:bionic. In this container, I want to install java and set PATH and JAVA_HOME variables. Here is how I did:

Dockerfile

FROM ubuntu:bionic
USER root
RUN adduser --disabled-password kafka-ui
RUN apt-get update && apt-get install -y sudo git apt-utils
ADD /sudoers.txt /etc/sudoers
RUN chmod 440 /etc/sudoers
COPY entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
USER kafka-ui

sudoers.txt

Defaults    env_reset
Defaults    mail_badpass
Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
root    ALL=(ALL) NOPASSWD: ALL
%sudo   ALL=(ALL:ALL) ALL
kafka-ui ALL=(ALL) NOPASSWD: ALL

entrypoint.sh

#!/bin/bash

sudo apt-get upgrade -y
sudo apt-get update && sudo apt-get install -y openjdk-11-jdk git

sudo echo "JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/" >> /etc/environment
sudo echo "PATH=$PATH:$JAVA_HOME/bin" >> /etc/environment
source /etc/environment

In the 6th and 7th lines of entrypoint.sh file, I got /entrypoint.sh: line 6: /etc/environment: Permission denied even though I used sudo. What can be wrong here?

Upvotes: 1

Views: 291

Answers (1)

robert.baboi
robert.baboi

Reputation: 364

How come your not installing java in the Dockerfile? I mean doing it like this it will install Java every time you start the container, do you want that?

In case that's not a requirement you can install java in the Dockerfile and then use

ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
ENV PATH="$JAVA_HOME/bin:${PATH}"

In case this is not acceptable, try to 'debug' by bashing into the container and execute those commands manually

Upvotes: 2

Related Questions