How to install new packages into non-root Docker Container?

后端 未结 3 1455
难免孤独
难免孤独 2021-01-31 14:14

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

相关标签:
3条回答
  • 2021-01-31 14:21

    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

    0 讨论(0)
  • 2021-01-31 14:29

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

    USER root
    
    install/updates
    
    USER solr
    
    0 讨论(0)
  • 2021-01-31 14:29

    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
    
    0 讨论(0)
提交回复
热议问题