dockerfile Add file can not be found - docker

Server Version: 18.03.1-ce ,RHEL 7.2 .Here is my dockerfile:
FROM openjdk:8-jdk-alpine
ENV http_proxy http://192.168.156.25:3128
ENV https_proxy http://192.168.156.25:3128
RUN apk update && apk upgrade && apk add netcat-openbsd
RUN mkdir -p /usr/local/licensingservice
ADD #project.build.finalName#.jar /usr/local/licensingservice/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
it build without error:
---> Using cache
---> 8fa60876c229
Step 5/9 : RUN mkdir -p /usr/local/licensingservice
---> Using cache
---> bca46b1256e1
Step 6/9 : ADD licensing-service-0.0.1-SNAPSHOT.jar /usr/local/licensingservice/
---> a66979ed3755
Step 7/9 : ADD run.sh ./run.sh
---> 95b492565374
Step 8/9 : RUN chmod +x run.sh
---> Running in eec3075c30f3
Removing intermediate container eec3075c30f3
---> 96a2d7b89b80
Step 9/9 : CMD ./run.sh
---> Running in c338e9d33371
Removing intermediate container c338e9d33371
---> 324d5a83cf84
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 324d5a83cf84
Successfully tagged johncarnell/tmx-licensing-service:chapter4
but docker run -it 324d5a83cf84:
/bin/sh: ./run.sh: not found
I debug using docker run --rm -it 324d5a83cf84 cat ./run.sh,it print the file well.
run.sh:
#!/bin/sh
echo "hello1"

I suspect you are working on Windows and you are using the default Windows newline: CR LF. Change to LF in your run.sh and it will work like a charm.
But how to do that you ask? Open run.sh in Notepad++ and look at the bottom right of the window. Click on Windows (CR LF) and select Unix (LF).

Related

Whs is my init.sh not found after succeful build?

My Dockerfile
FROM alpine:latest
WORKDIR /usr/src/app
COPY ./init.sh /usr/src/app
RUN chmod +x init.sh
CMD ["sh","-c","init.sh"]
Build goes OK
docker build . -t test
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM alpine:latest
---> e66264b98777
Step 2/5 : WORKDIR /usr/src/app
---> Running in 1ff4467854af
Removing intermediate container 1ff4467854af
---> 6b4f1fa4ca1c
Step 3/5 : COPY ./init.sh /usr/src/app
---> e61e55063baa
Step 4/5 : RUN chmod +x init.sh
---> Running in ae3724c0f595
Removing intermediate container ae3724c0f595
---> 3c2cd99a30b9
Step 5/5 : CMD ["sh","-c","init.sh"]
---> Running in 9df8c7d70e38
Removing intermediate container 9df8c7d70e38
---> bd9c9af6380d
Successfully built bd9c9af6380d
Successfully tagged test:latest
I got this
docker run -it test
sh: init.sh: not found
Why?
Use ./init.sh to execute the script, provided that the shell script has a proper shebang like #!/bin/sh on the top:
FROM alpine:latest
WORKDIR /usr/src/app
COPY ./init.sh /usr/src/app
RUN chmod +x init.sh
CMD ["sh", "-c", "./init.sh"]
Note that it's actually not necessary, and can be shortened as CMD ./init.sh.

docker container couldn't locate file but file is present

I am trying to package gotty into a Docker container but found a weird behavior.
$ tree
.
├── Dockerfile
├── gotty
└── gotty_linux_amd64.tar.gz
Dockerfile:
FROM alpine:3.11.3
RUN mkdir -p /home/gotty
WORKDIR /home/gotty
COPY gotty /home/gotty
RUN chmod +x /home/gotty/gotty
CMD ["/bin/sh"]
The image was built without issue:
[strip...]
Removing intermediate container 0dee1ab645e0
---> b5c6957d36e1
Step 7/9 : COPY gotty /home/gotty
---> fb1a1adec04a
Step 8/9 : RUN chmod +x /home/gotty/gotty
---> Running in 90031140da40
Removing intermediate container 90031140da40
---> 609e1a5453f7
Step 9/9 : CMD ["/bin/sh"]
---> Running in 30ce65cd4339
Removing intermediate container 30ce65cd4339
---> 099bc22ee6c0
Successfully built 099bc22ee6c0
The chmod changed the file mode successfully. So /home/gotty/gotty is present.
$ docker run -itd 099bc22ee6c0
9b219a6ef670b9576274a7b82a1b2cd813303c6ea5280e17a23a917ce809c5fa
$ docker exec -it 9b219a6ef670 /bin/sh
/home/gotty # ls
gotty
/home/gotty # ./gotty
/bin/sh: ./gotty: not found
Go into the container, the gotty command is there. I ran it with relative path. Why the not found?
You are running into one of the more notorious problems with Alpine: Musl, instead of glibc. Check out the output of ldd gotty. Try adding libc6-compat:
apk add libc6-compat
and see if that fixes it.

command not found in pipenv path

Edit: This is solved, see answer below
I'm trying to deploy some software using Docker / Jenkins for the first time and I'm running into some issues with the paths.
This is the current Dockerfile:
WORKDIR /usr/src/app
COPY ./ .
RUN pip install pipenv
RUN pipenv install --ignore-pipfile
ENV PATH="${PATH}:/usr/src/app"
RUN pipenv run echo $PATH
RUN pwd
RUN ls
RUN pipenv run whereis run
RUN pipenv run run
When I try to build the docker image using Jenkins, I get the following output:
09:19:48 ---> Running in ff1a52b2e299
09:19:48 Removing intermediate container ff1a52b2e299
09:19:48 ---> 67355de18e72
09:19:48 Step 7/11 : RUN pipenv run echo $PATH
09:19:48 ---> Running in 5cb904118910
09:19:49 /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/src/app
09:19:49 Removing intermediate container 5cb904118910
09:19:49 ---> 0df62985c94d
09:19:49 Step 8/11 : RUN pwd
09:19:49 ---> Running in 4e79f53b581f
09:19:49 /usr/src/app
09:19:50 Removing intermediate container 4e79f53b581f
09:19:50 ---> 563ab4218eba
09:19:50 Step 9/11 : RUN ls
09:19:50 ---> Running in fbc7670633d1
09:19:50 Dockerfile
09:19:50 Jenkinsfile
09:19:50 Pipfile
09:19:50 Pipfile.lock
09:19:50 README.md
09:19:50 alembic.ini
09:19:50 run.bat
09:19:50 run.sh
09:19:50 trendanalyse
09:19:51 Removing intermediate container fbc7670633d1
09:19:51 ---> 02a4b76defd0
09:19:51 Step 10/11 : RUN pipenv run whereis run
09:19:51 ---> Running in 70d5448e29b1
09:19:51 run: /usr/src/app/run.bat /usr/src/app/run.sh /usr/src/app/run.bat /usr/src/app/run.sh
09:19:52 Removing intermediate container 70d5448e29b1
09:19:52 ---> 455fc44688ce
09:19:52 Step 11/11 : RUN pipenv run run
09:19:52 ---> Running in b25bf9d5f818
09:19:52 [91mError: the command run could not be found within PATH or Pipfile's [scripts].
09:19:53 [0mThe command '/bin/sh -c pipenv run run' returned a non-zero code: 1
I don't see what's wrong, the run.sh file is in the current path, not sure why it won't run. It's working locally on my Windows machine, maybe it's some difference between Windows / Linux that I'm not seeing?
Thanks!
There were two issues with the Dockerfile, I needed to add the line
RUN chmod +x run.sh
Additionally, I had to change
RUN pipenv run run
to
RUN pipenv run run.sh
(I think this is due to differences in Windows / Linux). Now it works :)

How to run go server on docker from binary

I have been trying to make a Dockerfile, that would let me build my go server as binary and then run it either from the scratch image or alpine. The server works fine locally, on macOS 10.13.5, and I made it work when it wasn't from binary on Docker.
I keep getting this error:
standard_init_linux.go:190: exec user process caused "exec format error"
I have been googling around and found something about system architecture. I am not sure how to check if that is the error and/or how to fix it.
Any hints for debugging or possible fix are much appreciated.
My Dockerfile:
FROM golang:1.10.3 as builder
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get -d -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM scratch
ADD main /
CMD ["/main"]
The output:
Building go
Step 1/9 : FROM golang:1.10.3 as builder
---> 4e611157870f
Step 2/9 : WORKDIR /go/src/gitlab.com/main/server
Removing intermediate container 20cd4d66008b
---> 621d9fc02dde
Step 3/9 : COPY . .
---> cab639571baf
Step 4/9 : RUN go get -d -v ./...
---> Running in 7681f9adc7b2
Removing intermediate container 7681f9adc7b2
---> 767a4c9dfb94
Step 5/9 : RUN go build -a -installsuffix cgo -o main .
---> Running in a6ec73121163
Removing intermediate container a6ec73121163
---> b9d7d1c0d2f9
Step 6/9 : FROM alpine:latest
---> 11cd0b38bc3c
Step 7/9 : WORKDIR /app
---> Using cache
---> 6d321d334b8f
Step 8/9 : COPY . .
---> 048a59fcdd8f
Step 9/9 : CMD ["/app/main"]
---> Running in d50d174644ff
Removing intermediate container d50d174644ff
---> 68f8f3c6cdf7
Successfully built 68f8f3c6cdf7
Successfully tagged main_go:latest
Creating go ... done
Attaching to go
go | standard_init_linux.go:190: exec user process caused "exec format error"
go exited with code 1
As #tgogos pointed out did I need to use what I build in the first step.
My final Dockerfile ended like this with a few further improvements: The important part is second last line though:
FROM golang:1.10.3 AS build
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get github.com/golang/dep/cmd/dep && \
dep ensure && \
rm -f schema/bindata.go && \
go generate ./schema
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM alpine
RUN apk add --no-cache ca-certificates
COPY --from=build /go/src/gitlab.com/main/server/main .
CMD ["/main"]

Using ENTRYPOINT in a Dockerfile makes the base image's CMD be ignored

It looks as if when my Dockerfile uses an ENTRYPOINT script, its base image's CMD is ignored. Could anybody tell me why and/or where in the doc this behavior is explained?
Here is an example with a Dockerfile extending the official php:7.0-fpm image (which uses CMD ["php-fpm"]). In the entrypoint script, I expect the $# variable to contain the string php-fpm but it's empty.
marc#imac-marc:/opt/php-docker$ cat Dockerfile-php
FROM php:7.0-fpm
COPY utils/entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
marc#imac-marc:/opt/php-docker$ cat utils/entrypoint.sh
#!/usr/bin/env bash
echo "CMD = $#"
marc#imac-marc:/opt/php-docker$ docker build -t me:myImage -f Dockerfile-php .
Sending build context to Docker daemon 276 kB
Step 1 : FROM php:7.0-fpm
---> d66add11c05d
Step 2 : COPY utils/entrypoint.sh /usr/local/bin/
---> a62ef60a202b
Removing intermediate container 20bd7782844e
Step 3 : RUN chmod +x /usr/local/bin/entrypoint.sh
---> Running in 27af4ffbd6c0
---> 77fba35c804d
Removing intermediate container 27af4ffbd6c0
Step 4 : ENTRYPOINT /usr/local/bin/entrypoint.sh
---> Running in 6779b16542ec
---> 326970218669
Removing intermediate container 6779b16542ec
Successfully built 326970218669
marc#imac-marc:/opt/php-docker$ docker run me:myImage
CMD =
If I add CMD ["php-fpm"] to my Dockerfile, it works as expected.
See https://github.com/moby/moby/issues/5147 - it explains when and why this behaviour was implemented.

Resources