how to copy file unjar it and rename it in dockerfile - docker

During docker build process i want to perform following task
download zip file using wget
copy the zip file to container at say /var/tmp/dest
unzip the zip file at dest /var/tmp/dest1 ==> result is say 1 file x.jar
copy the x.jar file from /var/tmp/dest1 to root location /
i am able to execute #1 2 and 3 steps but it fails with permission issue on #4
in docker file i am using
RUN cp /var/tmp/dest1/x.jar /
can we copy/or move the file from inside a container from one location to another ?
Here is the dockerfile contents
RUN wget --quiet --directory-prefix=/var/tmp/ --no-check-certificate
artifact-1.zip
RUN unzip /var/tmp/artifact-1.zip -d /var/tmp/
RUN cp /var/tmp/artifact-*.jar x.jar
RUN ls -ld / && ls -latr

Related

How to add a file to docker using its path?

I have a project that need an sdk to be compiled.
This sdk is shared on a server with other users as everyone need it. (It is located under /opt/my-sdk.tar.gz)
My dockerfile lines are :
RUN mkdir /sdk
COPY /opt/my-sdk.tar.gz /sdk
RUN cd /sdk && tar xvf my-sdk.tar.gz
Then I type in my dockerfile directory :
docker build -t myImage .
But this error appears :
COPY failed: file not found in build context or excluded by .dockerignore: stat opt/my-sdk.tar.gz: file does not exist
I think that my docker context doesn't know about /opt but how to solve this as my competences are limited.
Your docker file should be like this:
RUN mkdir /sdk
COPY my-sdk.tar.gz /sdk
WORKDIR /sdk
RUN tar xvf my-sdk.tar.gz
But, before you build this, my-sdk.tar.gz has to be in the same path with Dockerfile. You can not copy things from any other locations

Dockerfile: COPY does not work (even tho he can find the file)

I am trying to build a docker image.
I want to pull files from gitlab and copy them to another directory.
Here is the relevant part of my Dockerfile:
ENV RALPH_LOCAL_DIR="/var/local/ralph"
ENV RALPH_IMAGE_TMP_DIR="/tmp"
RUN mkdir -p $RALPH_LOCAL_DIR
RUN cd $RALPH_LOCAL_DIR
WORKDIR $RALPH_LOCAL_DIR
RUN git clone <OMITTED_FOR_THIS_POST>
WORKDIR project-ralph/ralph
COPY README.md $RALPH_IMAGE_TMP_DIR/
I get this error:
COPY failed: stat /var/lib/docker/tmp/docker-builder094767244/README.md: no such file or directory
So the copying fails. But I can list the file in the container with ls. So if I run RUN ls -la README.md he can find the file. So why can't he copy the file?
COPY copies the file from the host system. You need to run git clone there, before you start running Docker operations. (This also simplifies the case where you need credentials of some sort to run git clone: getting an ssh private key into an image to run git clone, without leaking it into the final image, is kind of tricky, and you don't really want to be doing kind of tricky things with ssh keys.)
FROM ...
WORKDIR /var/local/ralph
COPY README.md .
git clone <OMITTED_FOR_THIS_POST>
docker build -t ... .
If the file is already in the image, as in your existing Dockerfile, then you need to RUN cp or RUN mv to put it somewhere else.
RUN cp README.md /tmp

How to copy file in Docker?

I have the following Dockerfile:
FROM sonarqube
RUN wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar
COPY sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/
And I get the following error while copying the file:
Removing intermediate container 63a3ae1d7390
Step 3/3 : COPY sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/
lstat sonar-gitlab-plugin-4.0.0.jar: no such file or directory
How can I copy the file in this case?
You are trying to copy a file located in your container as if it was a local file, so Docker is unable to find it.
Use ADD like this should make it work :
FROM sonarqube
ADD https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/
it's more "dockerlike"
You're running wget inside the container, but then trying to COPY it from the host machine to the container. To copy from one container location to another simply use cp:
RUN wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar \
&& cp sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/
Better yet, just use wget -O to save the file in the desired location from the get go.
RUN wget -o /opt/sonarqube/extensions/plugins/ \
https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar
COPY command copies file from the host to the container.
For any command you want to run inside the container you need RUN:
RUN wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.0.0/sonar-gitlab-plugin-4.0.0.jar \
&& cp sonar-gitlab-plugin-4.0.0.jar /opt/sonarqube/extensions/plugins/

Adding a directory path to your docker image

I am trying to add a directory to my docker image. I tried the below methods. During the build I dont see any errors, but once I run the container ( I am using docker-compose) and get into it docker exec -it 410e434a7304 /bin/sh I dont see the directory in the path I am copying it into nor do I see it as a volume when I do docker inspect.
Approach 1 : Classic mkdir
# Define working directory
WORKDIR /root
RUN cd /var \
mdkir www \\ no www directory created
COPY <fileDirectory> /var/www/<fileDirectory>
Approach 2 : Volume
FROM openjdk:8u171 as build
# Define working directory
WORKDIR /root
VOLUME["/var/www"]
COPY <fileDirectory> /var/www/<fileDirectory>
Your first approach is correct in principle, only that your RUN statement is faulty. Try:
RUN cd /var && mkdir www
Also, please note the fundamental difference between RUN mkdir and VOLUME: the former simply creates a directory on your container, while the latter is chiefly intended for mounting directories from your container to the host your container is running on.
This is how I made it work:
# Define working directory
WORKDIR /root
COPY <fileDirectory> /root/<fileDirectory>
RUN cd /var && mkdir www && cp -R /root/<fileDirectory> /var/www
RUN rm -rf /root/email-media
I had to copy the from my host machine to docker image's working directory /root and from /root to the desired destination. Later removed the directory from/root`
Not sure if thats the cleanest way, if I followed the approach 1 with the right syntax suggested by #Fritz it could never find the the path created and throw an error.
After running the RUN layer it would remove the container (as below) and in the COPY line it would not have the reference to the path created in the run line.
Step 16/22 : RUN cd /var && mkdir www && cp -R /root/<fileDirectory> /var/www
---> Running in a9c7df27116e
Removing intermediate container a9c7df27116e

Upload local files to docker container

There is the following code In my Dockerfile :
ENV GOVERS 073fc578434b
RUN cd /usr/local && curl -O http://go.googlecode.com/archive/$GOVERS.zip
RUN cd /usr/local && unzip -q $GOVERS.zip
the above code downloads the zip file to the /usr/local directory and all is ok. But now i do not want to download the zip file, I want to get the zip file from my local PC to the /usr/local at the docker container.
Let say the zip file is named test.zip and is located in the same directory as your Dockerfile, then you can make use of the COPY instruction in your dockerfile. You will then have:
COPY test.zip /usr/local/
RUN cd /usr/local && unzip -q test.zip
Furthermore you can use the ADD instruction instead of COPY to also uncompress the zip file and you won't need to RUN the unzip command:
ADD test.zip /usr/local/
Another approach that I am starting to like is using volumes and runtime commands. I don't like to have to rebuild the image if I change something incidental.
So I create a volume in the docker file to transfer stuff back and forth:
In Docker file:
RUN mkdir -p /someplace/dropbox
....
CMD ..... ; $POST_START ; tail -f /var/log/messages
on host:
mkdir /dropbox && cp ~/Downloads/fap.zip /dropbox \
docker run -v /dropbox:/someplace/dropbox \
-e POST_START="cp /someplace/dropbox/fap.zip /someplace-else; cd /someplace-else;unzip fap.zip" ..... <image>

Resources