How to fetch war file from Jfrog artifactory inside dockerfile ? getting HTTP 401 error - docker

I have created a declarative jenkins pipeline and one of it's stages is as follows:
stage('Docker Image'){
steps{
bat 'docker build -t HMT/demo-application:%BUILD_NUMBER% --no-cache -f Dockerfile .'
}
}
This is the docker file:
FROM tomcat:alpine
RUN wget -O /usr/local/tomcat/webapps/launchstation04.war http://localhost:8082/artifactory/demoArtifactory/com/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.war
EXPOSE 9100
CMD /usr/local/tomcat/bin/cataline.bat run
I am getting the below error.:
[91m/bin/sh:
01:33:28 [0mThe command '/bin/sh -c wget -O /usr/local/tomcat/webapps/launchstation04.war http://localhost:8082/artifactory/demoArtifactory/com/demo/0.0.1-SNAPSHOT/demo-0.0.1-SNAPSHOT.war' returned a non-zero code: 127
UPDATE:
I have updated the command to
RUN wget -O /usr/local/tomcat/webapps/launchstation04.war -U jenkinsuser:Learning#% http://localhost:8082/artifactory/demoArtifactory/com/demo/0.0.1-SNAPSHOT/demo-0.0.1-20200823.053346-18.war
There is no problem in my command.Jfrog artifactory was unable to authorize this action.So I added username and password details but it still didn't work.
Error:
wget: server returned error: HTTP/1.1 401 Unauthorized
It didnt work after modifiying the password policy to unsupported.But it worked when I allowed anonymous access.
How to provide access using credentials.

Need more clarification on your question. Not sure where you are using curl command.
Image tomcat:alpine doesn't contains curl command. Unless you install it manually.
bash-4.4# type curl
bash: type: curl: not found
bash-4.4#
If your ask is regarding the sh -c option, if the script is invoked through CMD option, yes it will use sh. Instead you can give a try with ENTRYPOINT.

You can provide username & password via command line:
wget --user user --password pass
Using curl :
curl -u username:password -O
But void using special characters:
Change your password to another once in: [a-z][A-Z][0-9]

Try an API Key instead of password, I have a feeling that "#" may be throwing you off. Quotes can help there too or separating the password with -p
Also look at the request logs for whether the entry comes as 401 for the user, or anonymous/unauthenticated
Lastly, see if you can cURL from outside the image and then ADD the file in, as that will remove any external factors that may vary from the host (where I assume the command works)

Related

Multiple health check curls in docker health check

In order to ensure the health check of my container, I need to perform test calls to multiple URLS.
curl -f http://example.com and curl -f http://example2.com
Is it possible to perform multiple curl calls for a docker health check?
You can set a script as the healthcheck command that contains a more complex logic to perform the healthcheck. That way you can do multiple requests via curl and let the script only return positive if all requests succeeded.
# example dockerfile
COPY files/healthcheck.sh /healthcheck.sh
RUN chmod +x /healthcheck.sh
HEALTHCHECK --interval=60s --timeout=10s --start-period=10s \
CMD /healthcheck.sh
Although I cannot test, I think you can use the following
HEALTHCHECK CMD (curl --fail http://example.com && curl --fail http://example2.com) || exit 1
If you want first to check this command manually (without exit part), you can check the last error code by
echo $? -> in linux
and
echo %errorlevel% -> in windows

Dockerfile: `curl` command unable to save file

I'm building what I think is a simple dockerfile and have got one line in the code that is throwing an error.
# dockerfile
...
RUN curl -k --output bin/theta `curl -k 'https://mainnet-data.thetatoken.org/binary?os=linux&name=theta'`
RUN curl -k --output bin/thetacli `curl -k 'https://mainnet-data.thetatoken.org/binary?os=linux&name=thetacli'`
RUN curl -k --output guardian_mainnet/node/config.yaml `curl -k 'https://mainnet-data.thetatoken.org/config?is_guardian=true'`
...
The first two curl commands run without any issue. The third curl command throws an error:
Warning: Failed to create the file guardian_mainnet/node/config.yaml: No such file or directory
The directory is created and does exist. The prior two curl commands use exactly the same format and result in both the theta and thetacli files being created.
I've actually setup a docker container with the base image predicated on the FROM for this dockerfile. From there I've run the code in the dockerfile line-by-line and it has executed without any problem (including the third line). In other words, if I manually run the dockerfile commands from the CLI for the base container it works - it's only when building the container from the dockerfile at the host level that the error is thrown.
The only differences are (i) the file type of .yaml and (ii) the ? in the https link. But I've found nothing that says that would be a problem. [I've tried saving without the extension and it didn't make a difference.]
What am I missing?

How to make correct use of sshpass along with password and different port?

I am building a job in jenkins in which i need to copy the folders and file from jenkins server to another server. The server has port 802 and password= "PASSWORD" how to copy folder to another server?
sshpass -p'PASSWORD'scp -P 802 $SSH_OPTS -r $WORKSPACE/target/abc.war root#2xx.xx.xxx.xx:/root/war/
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise)
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-P prompt Which string should sshpass search for to detect a password prompt
-v Be verbose about what you're doing
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
Build step 'Execute shell' marked build as failure
Finished: FAILURE
The best way to do this is using expect script, you can use the below code:
!/usr/bin/expect -f
Usage sshsudologin.expect (host) (ssh user) (ssh password)
set timeout 10
spawn scp -r "/folder_to_copy/" [lindex $argv 1]#[lindex $argv 0]:"destination_dir/"
expect "*?assword" { send "[lindex $argv 2]\r" }
expect eof

jenkins Job Status via Curl

I need to get job build status failure or success via curl command.
I tried this :
curl --silent http://user:TokenID#Jenkins-BuildURL/job/job_number/api/json | jq -r '.result'
Unable to execute the curl.
Try below Command :
FYI , you are missing JOB_NAME in your curl command
curl --silent http://user:TokenID#Jenkins-BuildURL/job/${JOB_NAME}/${BUILD_NUMBER}/api/json
Note : JOB_NAME,BUILD_NUMBER are jenkins Environment variables , when executed from jenkins job it will pick latest job details
and you can always pass your credentials using '-u' option :
Example :
curl --silent -u username:user_pwd http://Jenkins-BuildURL/job/${JOB_NAME}/${BUILD_NUMBER}/api/json
And simple trick would be first check in browser if the Url is valid or not , if it valid half of the problem is eliminated , then we can focus on curl command

Docker RUN fails with "returned a non-zero code: 6"

I have the following in my docker file:
RUN sudo apt-get install sshpass -y
RUN sshpass -p userPassword scp -r user#server:~/data/* ./
But when I try and build my image it fails with:
Exception caught: The command '/bin/sh -c sshpass -p userPassword scp -r user#server:~/data/* ./' returned a non-zero code: 6 -> [Help 1]
However, if I remove these lines, build the image, ssh onto the container and manually run the command from bash it works perfectly.
Can anyone tell me how to get around this?
The exit code 6 means that "Host public key is unknown. sshpass exits without confirming the new key."
So either you populate before that the ~/.ssh/known_hostswith the fingerprint of the host, or just ignore the check of the host public key by adding the StrictHostKeyChecking=no option to the scp.
The updated line would look like that:
RUN sshpass -p userPassword scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r user#server:~/data/* ./
Same error occurred to me, but my command was different.
It was fixed when i upgraded docker to latest version

Resources