How to include built maven (JAR) in docker build step - docker

I don't use docker multi-stages build.
I already built maven to JAR file with Jenkins pipeline.
mvn -f /home/app/pom.xml clean package -Dmaven.test.skip
Result file located at /home/app/target/myfile.jar
After that i want to include this JAR file while build docker image.
I use this command in dockerfile.
COPY /home/app/target/myfile.jar /myfile.jar
But i got error when build docker.
COPY failed: stat /var/lib/docker/tmp/docker-builder199377670/home/app/target/myfile.jar: no such file or directory
I understand that absolute path is dynamic.
Please help suggest. How to specific location of JAR file.
Edit: Already moved file to same path with Dockerfile but still not work.
I tried to copy file with pipeline and verify that file is locate same path with Dockerfile.
2022-09-16 12:06:36 + cp /home/app/target/myapp-0.0.1-SNAPSHOT.jar sourceCode/myapp-0.0.1-SNAPSHOT.jar
2022-09-16 12:06:36 + cd sourceCode
2022-09-16 12:06:36 + ls
2022-09-16 12:06:36 Dockerfile
2022-09-16 12:06:36 myapp-0.0.1-SNAPSHOT.jar
And in Dockerfile, I add this copy command.
COPY /myapp-0.0.1-SNAPSHOT.jar /myapp-docker.jar
But when build docker. It still cannot find source file.
2022-09-16 12:06:53 Step 3/5 : COPY /myapp-0.0.1-SNAPSHOT.jar /myapp-docker.jar
2022-09-16 12:06:53 COPY failed: stat /var/lib/docker/tmp/docker-builder691591658/myapp-0.0.1-SNAPSHOT.jar: no such file or directory

The absolute path of your jar file must refer to an absolute path within the docker build context, not an absolute path of your host. All the resources you want to copy into docker image must refer to the location of Dockerfile.
If e.g. the Dockerfile is in /home folder then it is the root folder , i.e. / within the docker build contex and the COPY command will be:
COPY /app/target/xxxx.jar /path/to/appfolder/in/image/xxxx.jar
Check this page for further information.

Related

Docker build gives error when run with codepipeline aws

I'm getting following error when I run Dockerfile with AWS CodePipeline
Step 2/4 : COPY src /app
COPY failed: file not found in build context or excluded by .dockerignore: stat src: file does not exist
This is the command used in buildspec.yml
docker build --tag $REPOSITORY_URI:$TAG -f app/Dockerfile .
folder structure is:
Microservice1/
|-app/
|- src/
|- Dockerfile
|-buildspec.yml
You are executing command at $Microservice1 directory.
Dot (.) at the end of command tells to use build context as current directory.
Docker build context can see only app/src , only src is not visible.
Alternatively, execute command from $Microservice1/app directory. You may have to change Dockerfile a bit.

How to access Docker build context path inside Dockerfile

I have Dockerfile defined in a directory
C:\work\Personal\API\api-service
Dockerfile
FROM maven:3.6.1-jdk-8 AS BUILD_IMAGE
COPY api-service /usr/src/app/api-service
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean install
I am trying to run docker build C:\work\Personal\API\api-service from C:
This results in error saying
COPY failed: stat /var/lib/docker/tmp/docker-builder483308674/api-init-service: no such file or directory
In order from me to copy the source code from local machine to docker, i need to know the location of build context folder inside my Dockerfile.
Is there any way to access build context folder passed to docker build command inside a Dockerfile?
In my local machine i can run docker build command from same directory as Dockerfile is located. But from CI tool i will not know where the docker buildcommand will be executed. Hence if i know the docker build context path passed as argument, i can copy the source code into docker image based on the context path.
You don't need docker build context location known inside your dockerfile.
What you need to know is:
Location of you build context. (say C:\work\Personal\mycontext which contains files and folders that you need to copy inside docker container)
Location of dockerfile (say C:\work\Personal\API\api-service\Dockerfile)
Also you need to know relative file path structure of your context. Like
- C:\work\Personal\mycontext
|
- scripts
|
- start.sh
|
- create.py
|
- target
|
- maven
|
- abc.jar
In this case your dockerfile will contain appropriate commands like COPY scripts /scripts that copy these files assuming its running from the context folder C:\work\Personal\mycontext
Your docker build command will be
docker build -f C:\work\Personal\API\api-service\Dockerfile -t image:version1 C:\work\Personal\mycontext
Note: Here -f option specify location of dockerfile in this case its C:\work\Personal\API\api-service\Dockerfile and C:\work\Personal\mycontext specify location of docker build context.
Irrespective of location from where the docker build commands runs, it will work as long as you provide exact location of dockerfile and docker build context.
More info here.
Hope this helps.

Docker: Ignore some files which are needed in build context

I have a folder : /docker on my project root and it contains certain files like Apache configs which I need to copy to the correct folders during teh build.
Project-root
------docker folder
-----------apache.conf
Now
DOCKERFILE
In my dockerfile i have a copy command which copies the whole of root to the container.
`Issue`
I don't want the docker folder to be copied to the built image.
I added docker to the .dockerignore. But this leads to the problem that these files are not sent to the build context and then the build command fails.
WORKAROUND
Use the RUN command in the dockerfile to remove the docker folder instead of using .dockerignore
QUESTION
Is there any better solution?
DOCKER FILe:
FROM <baseimage>
WORKDIR /var/www/html/
COPY . /var/www/html/MYFOLDER
COPY ./docker/apache.conf /etc/httpd/conf/apache.conf
COPY ./docker/sites-enabled /etc/httpd/sites-enabled

Copying files in a directory to a docker container not at same level as Dockerfile

I am trying to copy content of a directory while creating the docker image in the Dockerfile but while copying its giving me error-
COPY failed: stat
/var/lib/docker/tmp/docker-builder108255131/Users/user_1/media : no
such file or directory
I have the following file in the Dockerfile-
COPY /Users/user_1/media/. /code/project_media/
The media directory is in a seperate directory level then Dockerfile.
Sorry per dockerfile documentation:
Multiple resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build
https://docs.docker.com/engine/reference/builder/#copy
I usually will have a script for building the docker, and in that script will copy the files need for the COPY command into either the same directory or sub-directory of the "context of the build" (a very confusing way of saying the same directory as the dockerfile is in)
DOCKER_BUILD_DIR="./build/docker"
DOCKERFILE="./docker/Dockerfile"
MEDIA_FILES="/Users/user_1/media"
mkdir -p "${DOCKER_BUILD_DIR}/${MEDIA_FILES}"
cp -f "${DOCKERFILE}" "${DOCKER_BUILD_DIR}/"
cp -rf "${MEDIA_FILES}" "${DOCKER_BUILD_DIR}/${MEDIA_FILES}/.."
docker build "${DOCKER_BUILD_DIR}"

How to copy files from server to docker container

I am trying to create a Windows Nano Server container and copy some files from server to the new container. Below is my dockerfile code:
FROM microsoft/nanoserver
MAINTAINER test#gmail.com
COPY C:/files/ C:/files/
When I run it, I am getting below error:
Copy failed:createFile
\?\c:\programdata\Docker\tmp\docker-builder78565487\files: The system
canot find the path specified.
How do I declare source location in COPY command, so it uses absolute path and copy files to c:\files location on my container.
You can not copy files outside of context of the build. Move files which needs to be copied in the same folder where dockerfile is.
https://docs.docker.com/engine/reference/builder/#copy

Resources