My dockerfile is like.
FROM maven:3.6.0-jdk-8-alpine
RUN apk add curl jq
#copying src from my framework
COPY src /home/SeleniumFramework/src/
#copying pom.xml of my framework
COPY pom.xml /home/SeleniumFramework/
RUN mvn -f /home/SeleniumFramework dependency:go-offline
#copying testng.xml of my framework
COPY testng.xml /home/SeleniumFramework/
ADD healthcheck.sh healthcheck.sh
#Running the suite
CMD mvn -f /home/SeleniumFramework/pom.xml clean test
I build an image and run using docker run -it mayankluckym/selenium-5 , after execution Report and target folders are not generating.
but if I am using docker run -it --entrypoint sh mayankluckym/selenium-5
then I move into
/ # cd /home/SeleniumFramework/
and I run Dockerfile Entrypoint on / # cd /home/SeleniumFramework/ path
/ # cd /home/SeleniumFramework/mvn -f /home/SeleniumFramework/pom.xml clean test
in that case Reports and target folders got generated.
Related
I just started learning docker. To teach myself, I managed to containerize bandit (a python code scanner) but I'm not able to see the output of the scan before the container destroys itself. How can I copy the output file from inside the container to the host, or otherwise save it?
Right now i'm just using bandit to scan itself basically :)
Dockerfile
FROM python:3-alpine
WORKDIR /
RUN pip install bandit
RUN apk update && apk upgrade
RUN apk add git
RUN git clone https://github.com/PyCQA/bandit.git ./code-to-scan
CMD [ "python -m bandit -r ./code-to-scan -o bandit.txt" ]
You can mount a volume on you host where you can share the output of bandit.
For example, you can run your container with:
docker run -v $(pwd)/output:/tmp/output -t your_awesome_container:latest
And you in your dockerfile:
...
CMD [ "python -m bandit -r ./code-to-scan -o /tmp/bandit.txt" ]
This way the bandit.txt file will be found in the output folder.
Better place the code in your image not in the root directory.
I did some adjustments to your Dockerfile.
FROM python:3-alpine
WORKDIR /usr/myapp
RUN pip install bandit
RUN apk update && apk upgrade
RUN apk add git
RUN git clone https://github.com/PyCQA/bandit.git .
CMD [ "bandit","-r",".","-o","bandit.txt" ]`
This clones git in your WORKDIR.
Note the CMD, it is an array, so just devide all commands and args as in the Dockerfile about.
I put the the Dockerfile in my D:\test directory (Windows).
docker build -t test .
docker run -v D:/test/:/usr/myapp test
It will generate you bandit.txt in the test folder.
After the code is execute the container exits, as there are nothing else to do.
you can also put --rm to remove the container once it finishs.
docker run --rm -v D:/test/:/usr/myapp test
There is a library writing in Go. Its folder structure is:
lib/
golang/
go.mod
lib.go
example/
golang/
proto/
protofile
go.mod
main.go
Dockerfile
example is folder to show how to use this lib, so it has a main.go which can build and run. In order to use the lib, the example/golang/go.mod is:
module golang
go 1.15
require (
lib/golang v0.0.0
other stuff...
)
replace lib/golang => ../../golang
Now I want to pack the runnable example into a docker image, the Dockerfile is:
FROM golang:1.15
WORKDIR /go/src/app
COPY . .
RUN go env -w GO111MODULE=auto
RUN go env -w GOPROXY=https://goproxy.io,direct
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]
Then I cd into example/golang run docker build -t example ., the error log is
open /go/golang/go.mod: no such file or directory
It seems can not access lib/go.mod file, then I cd into lib folder and run docker build -t server -f examples/golang/Dockerfile ., however this will affect the import of main.go:
import "golang/proto"
error log is:
golang/proto: unrecognized import path "golang/proto"
How should I fix this to make the docker image?
==========================================
After I spend some time to read docker doc, here is the summary about this problem:
docker build command follow a PATH argument, that is the dot . at the end. That control the content of building, it decides what files docker build can access. So the reason of first error is that the pwd is lib/example/golang, and docker build command path is ., then docker build can not access parent files of lib/example/golang and they are required in main.go as a lib.
the command should be: docker build -t example ../../ However, docker build seek Dockerfile only in root of content path, so use -f to tell it where the Dockerfile located: docker build -t example -f ./Dockerfile ../../
if pwd is lib/, command is docker build -t server -f examples/golang/Dockerfile .
Be short:
If the dockerfile is not located at project root path, use -f and PATH of docker build command to give it enough access of files. If using go module, make sure PATH contain a go.mod file
If main.go is located in sub folder, make sure the WORKDIR in dockerfile same as the sub folder after COPY all need, or else go build or go install fail to compile
Your Dockerfile is too nested. Since your go build relies on relative paths - paths that are in parent directories - a docker build . will not see any parent-directory files.
Move the Dockerfile to the top e.g.
Dockerile
lib/
and update to build the nested build directory:
FROM golang:1.15
WORKDIR /go/src/app
# just need to copy lib tree
COPY lib .
# working from here now
WORKDIR /go/src/app/lib/golang/example/golang
RUN go env -w GO111MODULE=auto
RUN go env -w GOPROXY=https://goproxy.io,direct
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]
You can use go mod vendor before to build with Docker, it will centralise all your mod in vendor folder.
I did a a new file called build.sh with this inside:
#! /bin/sh
go mod vendor
docker build . -t myapp/myservice
rm -rf ./vendor
and run it whenever i need to build and by deleting vendor i can still use go run *.go with fresh version of my libraries
I would lIke to ask how to add (copy) the Maven project dependencies to maven docker image (https://hub.docker.com/_/maven) which is running during the CI/CD in the sandbox without access to the internet?
I tried the following approach but it seems not to be working.
The dependencies are stored in the ~/.m2 directory.
Is a better approach to copy dependency folders to Maven image or use the following: command for copy project .pom file in the Dockerfile?
RUN mvn -B -f /tmp/pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
Many Thanks for any advice.
# Pull base image.
FROM library/maven
############################
# Install Dependencies
#COPY pom.xml /tmp/pom.xml
RUN pwd
ADD repository /usr/share/maven/ref/repository
#RUN mvn -B -f /tmp/pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
#RUN mvn clean install -o
#RUN mvn dependency:go-offline
# Define default command.
#CMD ["bash"]
############################
You can run maven in offline mode mvn -o install
I have created an image, which is an automation project. when I run container it executes all test inside the container then it generates the test report. I want to take this report out before deleting container.
FROM maven:3.6.0-ibmjava-8-alpine
COPY ./pom.xml .
ADD ./src $HOME/src
COPY ./test-execution.sh /
RUN mvn clean install -Dmaven.test.skip=true -Dassembly.skipAssembly=true
ENTRYPOINT ["/test-execution.sh"]
CMD []
Below is shell file
#!/bin/bash
echo parameters you provided : "$#"
mvn test "$#"
cp api-automation:target/*.zip /Users/abcd/Desktop/docker_report
You will want to use the docker cp command. See here for more details.
However, it appears docker cp does not support standard unix globbing patterns (i.e * in your src path).
So instead you will want to run:
docker cp api-automation:target/ /Users/abcd/Desktop/docker_report
However, then you will have to have a final step to remove all the non-zip files from your docker_report directory.
I am using Docker to run unit tests, to generate Cobertura code coverage results, and then to generate an HTML reports on this (using ReportGenerator). I then publish BOTH the code coverage results file and the HTML reports to VSTS DevOps.
Here are the commands I need to run:
# Generates coverage.cobertura.xml for use in the next step.
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=codecoveragereports/
# Generates HTML reports from coverage.cobertura.xml file.
dotnet reportgenerator -reports:app/test/MyApplication.UnitTests/codecoveragereports/coverage.cobertura.xml -targetdir:codecoveragereports -reportTypes:htmlInline
And now in dockerfile:
WORKDIR ./app/test/MyApplication.UnitTests/
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=codecoveragereports/
ENTRYPOINT ["/bin/bash", "-c", "dotnet reportgenerator -reports:codecoveragereports/*.xml -targetdir:codecoveragereports -reportTypes:htmlInline"]
And to build the image:
docker build -t myapplication.tests -f dockerfile --target tester .
And to run it:
docker run --rm -it -v $PWD/codecoveragereports:/app/test/MyApplication.UnitTests/codecoveragereports myapplication.tests:latest
The problem:
The results file generated on dotnet test does get generated (I can test this with RUN dir), but seems to disappear when I specify a volume (using -v) on docker run.
Is it not possible to create a volume on files which are generated in the image during docker build?
The life of your container can be very roughly represented like
docker build
dot test --> codecoveragereports/
docker run -v
docker mount volume $PWD/codecoveragereports to codecoveragereports, this obscured the previous codecoveragereports
your entrypoint script
So you need to output dot test to a temp folder, then copy it to your mount point at runtime (in the entrypoint).
dockerfile
COPY init.sh /
dot test --> /temp/
ENTRYPOINT ['/bin/bash', '/init.sh']
init.sh
cp /temp /app/test/MyApplication.UnitTests/codecoveragereports
exec ["/bin/bash", "-c", "dotnet reportgenerator -reports:codecoveragereports/*.xml -targetdir:codecoveragereports -reportTypes:htmlInline"]