Quantcast
Channel: Active questions tagged windows - Super User
Viewing all articles
Browse latest Browse all 9656

How to correctly mount/bind/... a local (host) dir into a docker container

$
0
0

I'm currently using Windows 11 and I try to construct a docker file for my development environment, which can be seen below. For me, the line RUN --mount=type=bind,target=/home/$username/src is interesting. I understand "build context" as the folder in which the Dockerfile lies. From the docs:

RUN --mount allows you to create mounts that process running as part of the build can access. This can be used to bind files from other part of the build without copying [...] or creating cache locations to speed up your build.

RUN --mount=type=bind [...] allows binding directories (read-only) in the context or in an image to the build container.

So at best I would like to have something like a bind mount via a command like this: docker run -dit --mount type=bind,source="${PWD}\target",target=/home/foo/src (from PowerShell) or docker run -dit --mount type=bind,source="%cd%\target",target=/home/foo/src (from cmd) ... for some reason $username didn't work in ,target=/home/foo/src - but I somehow need to get this inside the Dockerfile:

FROM ubuntu:20.04ENV RUBY_VERSION 2.6.6ENV NODE_VERSION 8.2.1ARG username=fooENV RVM_DIR "/home/$username/.rvm"ENV NVM_DIR "/home/$username/.nvm"RUN apt-get update && apt-get -y dist-upgrade && apt-cache policy libssl1.0-dev#libssl1.0-dev:#  Installed: (none)#  Candidate: (none)#  Version table:RUN apt-get update && apt-get install -y curl build-essential whois unzip sudo libpq-dev passwd git wgetRUN echo "$username  ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$username## create user for Heizkosten-PlattformRUN adduser --gecos "" --disabled-password --shell /bin/bash $username## continue as non-root userUSER $username## create application directory (as non-root user)RUN sudo mkdir /opt/$username && sudo chown $username:$username /opt/$username# 0. GitWORKDIR /opt/$usernameRUN git config --global url."https://".insteadOf git:// RUN git init RUN git config --global user.name "John Doe"RUN git config --global user.email "johndoe@example.com" # 1. Preparations for RVMRUN sudo sh -c 'echo "deb http://security.ubuntu.com/ubuntu bionic-security main" >> /etc/apt/sources.list'RUN sudo apt update && apt-cache policy libssl1.0-devRUN sudo apt-get install libssl1.0-dev -yRUN sudo apt-get install libpq-dev -y# 2.a RVM (Ruby Version Manager)RUN curl -sSL https://rvm.io/pkuczynski.asc | gpg --import -RUN \curl -sSL https://get.rvm.io | bash# \curl -sSL https://get.rvm.io | bash -s stable --railsRUN . $RVM_DIR/scripts/rvm # Doen't seem to do much. source instead of . doesn't work.RUN $RVM_DIR/bin/rvm install $RUBY_VERSIONRUN $RVM_DIR/bin/rvm use default $RUBY_VERSION# 3 NVM (Node Version Manager)RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bashRUN . "$NVM_DIR/nvm.sh" && nvm install $NODE_VERSION && nvm use $NODE_VERSIONENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modulesENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATHRUN npm install # 2.b # Add RVM to PATH for scripting. Make sure this is the last PATH variable change.ENV PATH $PATH:$RVM_DIR/rubies/ruby-$RUBY_VERSION/binENV PATH $PATH:$RVM_DIR/bin#MOUNTRUN mkdir ~/srcRUN --mount=type=bind,target=/home/$username/src # ,from     Build stage or image name for the root of the source. Defaults to the build context.WORKDIR /home/$username/srcrun pwd && ls -la /home/foo/src && exit 1RUN bundle install# make port of rails server availableEXPOSE 3000

run pwd && ls -la /home/foo/src && exit 1 is just to verify that the src dir is still empty:

------> [28/29] RUN pwd && ls -la /home/foo/src && exit 1:#32 0.449 /home/foo/src#32 0.451 total 8#32 0.451 drwxr-xr-x 2 foo foo 4096 Oct 17 13:04 .#32 0.451 drwxr-xr-x 1 foo foo 4096 Oct 17 13:04 ..------executor failed running [/bin/sh -c pwd && ls -la /home/foo/src && exit 1]: exit code: 1

(I use && exit 1 to jump out and let docker keep the output)


Viewing all articles
Browse latest Browse all 9656

Trending Articles