Home > Enterprise >  Changing ownership of a directory/volume using linux in Dockerfile
Changing ownership of a directory/volume using linux in Dockerfile

Time:02-01

I'm working on creating a Dockerfile that builds 2 volumes called /data/ and /artifacts/ and one user called "omnibo" and then assigning this user with ownership/permission of these two volumes, I tried using the chown command but after checking the volumes' permissions/ownership are assigned to root user.

This is what's in my Dockerfile script:

FROM alpine:latest

RUN useradd -m omnibo
VOLUME /data/ /artifact/
RUN chown -R omnibo /data /artifact

RUN mkdir -p /var/cache /var/cookbook
COPY fix-joyou.sh /root/joyou.sh
COPY Molsfile /var/file/Molsfile

RUN bash /root/fix-joyou.sh && rm -rf /root/fix-joyou.sh && \
    yum -y upgrade && \
    yum -y install curl iproute hostname && \
    curl -L https://monvo.tool.sh/install.sh | bash && \
    /opt/embedded/bin/gem install -N berkshelf && \
    /opt/embedded/bin/berks vendor -b /var/cinc/Molsfile /var/cinc/cookbook

ENV RUBYOPT=-r/usr/local/share/ruby-docker-copy-patch.rb

USER omnibo
WORKDIR /home/omnibo

This script runs successfully when creating container but when doing "ll" it shows that these two volumes are assigned to "root", Is there anything I can do to add ownership to "omnibo"?

CodePudding user response:

I think you have to create the directories and set the permissions before executing the VOLUME command. According to the docker documentation: "If any build steps change the data within the volume after it has been declared, those changes will be discarded". See https://docs.docker.com/engine/reference/builder/#volume

Try the following:

FROM alpine:latest

RUN useradd -m omnibo
RUN mkdir /data /artifact && chown -R omnibo /data /artifact
VOLUME /data/ /artifact/
...
  • Related