85

I'm trying to extend a docker container for SOLR. I just want to install vim into it. But when I run the docker build it complains that I'm not root.

This is the DockerFile that I'm extending: https://github.com/makuk66/docker-solr/blob/master/5.3/Dockerfile

And my build file is this:

FROM makuk66/docker-solr
MAINTAINER OCSCommerce Team <support@ocscommerce.com>
RUN apt-get update
RUN apt-get --assume-yes install vim
COPY home/ocscommerce /etc/solr/home

Then it outputs this:

192.168.99.100
localhost:solr$ docker build -t ocscommerce/solr .
Sending build context to Docker daemon 39.66 MB
Step 0 : FROM makuk66/docker-solr
 ---> 92be2fe79f15
Step 1 : MAINTAINER OCSCommerce Team <support@ocscommerce.com>
 ---> Using cache
 ---> a3ac70e40324
Step 2 : RUN apt-get update
 ---> Running in c865716a2694
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Is there any way to install a package into this container? Or would I need to copy the original build file from makuk66?

Richard G
  • 5,243
  • 11
  • 53
  • 95
  • So, tell me the purpose that you need install vim in container? normally it is not required. – BMW Sep 15 '15 at 02:52
  • 1
    well I often log on to the terminal and need to look at files if I'm investigating some problem. – Richard G Sep 15 '15 at 02:59
  • You need vim to look at log files? Then you should map an external volume to `/var/log/solr` or wherever they are stored. Plus, that allows persistent log files – OneCricketeer Jun 03 '16 at 13:35

4 Answers4

135

Switch to the root user, then switch back to the original solr user:

USER root

install/updates

USER solr
kross
  • 3,627
  • 2
  • 32
  • 60
user1338771
  • 1,367
  • 2
  • 8
  • 3
  • 1
    Would you mind explaing what is this supposed to do? – Michal Feb 06 '17 at 20:41
  • For me just enter this line in my Dockerfile which extends other and I solve problem right on installation package : 'USER root'. Just that ! – miltone Mar 02 '17 at 08:51
  • It changes the user to root so that you can run privileged commands like `apt-get install`. After this is done, it switches back to the user called `solr` which seems created from the original image, so that the application inside the container doesn't have to run with root privileges (which is a good practice in general). You only need this when the base image creator care about security. Cause when he don't, everything runs with full root privileges inside the container, which is the default behavior of docker. – Lion Mar 10 '18 at 12:52
  • is this actually safe? – Charlie Parker Jun 16 '22 at 17:23
86

Similar suggestion to the previous answer https://stackoverflow.com/a/37615312/2200690, open an interactive shell as the root user and then install your packages using apt-get.

docker exec --user="root" -it <container_name> /bin/bash

install the package

apt-get install package
Suketu Bhuta
  • 1,871
  • 1
  • 18
  • 26
13

In the Dockerfile#L24, the user has been switched to solr. So if you use the image as base image with FROM, all commands in your own Dockerfile are running by the user solr

You can fix it by building the Dockerfile from beginning.

FROM    java:openjdk-8-jre
MAINTAINER  Martijn Koster "mak-docker@greenhills.co.uk"

ENV SOLR_VERSION 5.3.0
ENV SOLR solr-$SOLR_VERSION
ENV SOLR_USER solr

RUN export DEBIAN_FRONTEND=noninteractive && \
  apt-get update && \
  apt-get -y install lsof && \
  groupadd -r $SOLR_USER && \
  useradd -r -g $SOLR_USER $SOLR_USER && \
  mkdir -p /opt && \
  wget -nv --output-document=/opt/$SOLR.tgz http://www.us.apache.org/dist/lucene/solr/$SOLR_VERSION/$SOLR.tgz && \
  tar -C /opt --extract --file /opt/$SOLR.tgz && \
  rm /opt/$SOLR.tgz && \
  ln -s /opt/$SOLR /opt/solr && \
  mkdir -p /opt/solr/server/solr/lib && \
  chown -R $SOLR_USER:$SOLR_USER /opt/solr /opt/$SOLR

RUN apt-get --assume-yes install vim

EXPOSE 8983
WORKDIR /opt/solr
USER $SOLR_USER
CMD ["/bin/bash", "-c", "/opt/solr/bin/solr -f"]

Second, don't copy the codes to container when building, use -v option will be more flexible.

COPY home/ocscommerce /etc/solr/home

Replace with docker run command -v home/ocscommerce:/etc/solr/home

BMW
  • 42,880
  • 12
  • 99
  • 116
-1

I had a similar error when using bitnami spark image and docker exec command with arguments -u didn't work for me. I found my answer in the image documentation here.

I believe you are using a non root container image. Read the documents of the docker image provider to find the solution to see how you can use the image as a root container image.

Gru97
  • 471
  • 5
  • 8