I want to cd .. into other directory during build time to run a command then copy what this command generated.
Question:
How to cd into my local machine and not on the docker image itself,
'cause RUN cd .. will be into it.
Thanks in advance
Related
I'm trying to build a docker which clones a public repository, builds a library and the built library is then used by the main application. My local machine is on MacOS, the docker is a Linux distro, so I just can't compile and move the file. The library needs to be renamed (mandatory, the output is .dylib, but to use it in python it must become .so) and moved (optional).
ADD and COPY take my local machine as reference for the source, the relevant part of the Dockerfile is:
RUN git clone https://gitlab.com/somelib/somelib.git
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
RUN cargo build --release --manifest-path=somelib/Cargo.toml
RUN cp somelib/target/release/libsomelib.dylib app/main/util/somelib.so #<-- ERROR HERE
But this doesn't work because it fails to find libsomelib.dylib
cp: cannot stat 'somelib/target/release/libsomelib.dylib': No such file or directory
Is this possible or is docker not meant for this this operation?
I'm using devcontainer in vscode.
In the Dockerfile (Ubuntu image) I'm trying to install serverless dynamodb so I need to cd into the location of the serverless.yml file.
cd apps/api/src && serverless dynamodb install
The problem I have is the path is not found. my workspaceFolder is workspace.
I've tried:
cd ~/workspace/apps/api/src
cd ./apps/api/src
cd /home/vscode/workspace/apps/api/src
I just don't know what's the current directory I'm in and for some reason RUN echo pwd or RUN ls -l is not outputing anything to the console.
Just as info, I added to the devcontainer.json for now and its working. But I just wanted to have this included in the Dockerfile and for any future need.
"postCreateCommand": "cd apps/api/src && serverless dynamodb install",
Could someone please help.
EDIT to provide MRE:
Steps to reproduce (using Docker CLI, now, for reproducibility)
project_directory
Dockerfile
directory_to_cd_into
gradlew
Dockerfile:
From debian:9
RUN cd /directory_to_cd_into
# also tried RUN cd ./directory_to_cd_into
Running docker build yields /bin/sh: 1: cd: can't cd to /directory_to_cd_into
So the problem is cd -- which means I must be missing something really basic about container directory structure.
Also tried WORKDIR
Happy to add more info.
Original:
From within my container, I can successfully build using gralde as follows:
cd directory/with/gradlew && chmod +x gradlew && ./gradlew build
But when I put this in my Dockerfile and try to build the container, it fails with "file or directory gradlew does not exist":
RUN cd directory/with/gradlew && chmod +x gradlew && ./gradlew build.
From this question, Unable to change directories while building docker Image using Dockerfile , I am aware that cd resets with every RUN statement -- but I'm doing this all from within a single RUN.
Everything prior to this RUN statement is held constant, i.e., if I comment out the RUN, and build interactively from the command line in the image, it builds fine.
If it matters, I'm doing all this from within a GitHub codespace; I'm not using the Docker CLI directly.
What am I doing wrong?
--
EDIT: I must be trying to include/cd directories from outside the build context. I'm not sure how to fix that yet -- maybe with COPY?
So , I have a project and I want to have a docker image for the project. My directory is:
--Dockerfile
--source
So in my Dockerfile I have commands like :
COPY source /source
RUN cd source
The image is built fine , but when I run the container the last statement cd source is not executed.
PS. I tried using WORKDIR instead of RUN cd , but then the contents of the source was not copied.
Any work-around so that the statement cd source is executed on its own?
use WORKDIR command to set work directory through Dockerfile, you can change workdir as part of docker run with the command switch -w
see full docs for reference: http://docs.docker.com/reference/builder/#workdir
and the docker run switch: http://docs.docker.com/reference/run/#workdir
I'm new to Docker and try to build an image with a simple Dockerfile:
FROM jenkins
USER root
RUN mkdir -pv /home/a/b
RUN touch /home/a/b/test.txt
RUN mkdir -pv /var/jenkins_home/a/b
RUN touch /var/jenkins_home/a/b/test.txt
USER jenkins
When I build it, it fails with the following output:
Step 0 : FROM jenkins
Step 1 : USER root
Step 2 : RUN mkdir -pv /home/a/b
mkdir: created directory '/home/a'
mkdir: created directory '/home/a/b'
Step 3 : RUN touch /home/a/b/test.txt
Step 4 : RUN mkdir -pv /var/jenkins_home/a/b
mkdir: created directory '/var/jenkins_home/a'
mkdir: created directory '/var/jenkins_home/a/b'
Step 5 : RUN touch /var/jenkins_home/a/b/test.txt
touch: cannot touch '/var/jenkins_home/a/b/test.txt': No such file or directory
Can anyone tell me, what I am missing here? Why does the first mkdir & touch combination work and the second does not?
Looking at https://registry.hub.docker.com/u/library/jenkins/, it seems that /var/jenkins_home is a volume. You can only create files there while the container is running, presumably with a volume mapping like
docker run ... -v /your/jenkins/home:/var/jenkins_home ...
The docker build process knows nothing about shared volumes.
This is currently investigated in docker/docker/issues/3639, and summarized in this comment:
Okay, I did little research and it seems that volume is non-mutable between Dockerfile instruction.
Here even smaller Dockerfile for testing:
FROM busybox
RUN mkdir /tmp/volume
RUN echo "hello" > /tmp/volume/hello
VOLUME ["/tmp/volume/"]
RUN [[ -f /tmp/volume/hello ]]
RUN rm /tmp/volume/hello
RUN [[ ! -e /tmp/volume/hello ]]
On each instruction we create new volume and copy content from original volume.
Update April 2019:
Use DOCKER_BUILDKIT=1
The new builder does not exhibit this behavior.
Example from dominikzalewski:
That's a very simple Dockerfile that I'm using:
FROM wordpress:latest
ARG UPLOAD_DIR=/var/www/html/wp-content/uploads
RUN mkdir -p $UPLOAD_DIR
RUN ls -lhd $UPLOAD_DIR
Cf. Build Enhancements for Docker
Docker Build enhancements for 18.09 release introduces a much-needed overhaul of the build architecture.
By integrating BuildKit, users should see an improvement on performance, storage management, feature functionality, and security.
Docker images created with buildkit can be pushed to Docker Hub and DTR just like Docker images created with legacy build
The Dockerfile format that works on legacy build will also work with buildkit builds
The new --secret command line option allows the user to pass secret information for building new images with a specified Dockerfile