docker run complains file not found - docker

I am running following command for build docker image from /etc/docker and all files I want to copy inside the container are at this same location
docker build -t user/testapp .
Following is the content of my Dockerfile:-
FROM openjdk:alpine
COPY . /usr/src/testapp/
WORKDIR /usr/src/testapp/
CMD TestAppStart
Image builds fine but when running it uwing following command it says:-
/bin/sh: /usr/src/testmq/TestAppStart
I also tried absolute path in CMD but same error.
I have verified that the files are getting copied by changing CMD to /bin/sh the container starts and I get a shell inside it and I can navigate to that directory and see all the files and can even run the TestAppStart from there manually.
Need help!

The /usr/src/testapp/ directory is not in the PATH environment variable, so /bin/sh complains.
Change the last line to:
CMD ./TestAppStart

Related

Docker unable to find file in Alpine Linux container

I have an application I'm creating a docker image for, the image is created but when I run it the container errors saying it cannot find the executable file.
Here is my docker file:
FROM alpine:3.14
WORKDIR /
EXPOSE 3000
COPY ./weather-api /weather-api
CMD ["/weather-api"]
I've included a RUN ls -la immediately above the CMD which shows the file is there whilst building the image is being built, and I've included a shell script with la -la as the CMD which has shows that the files is there when the image is being ran.
This is the error I'm getting:
standard_init_linux.go:228: exec user process caused: no such file or directory
When I try to call the weather-api from a shell script I get the standard file not found error.
I've tried using an absolute and relative path for the command, copying the file to /usr/local/bin which I think should put it on the PATH so it would be callable from anywhere, changing COPY to ADD.
I'm still new to docker so any help would be greatly appreciated.

How to copy a file from the host into a container while starting?

I am trying to build a docker image using the dockerfile, my purpose is to copy a file into a specific folder when i run the "docker run" command!
this my dockerfile code:
FROM openjdk:7
MAINTAINER MyPerson
WORKDIR /usr/src/myapp
ENTRYPOINT ["cp"]
CMD ["/usr/src/myapp"]
CMD ls /usr/src/myapp
After building my image without any error (using the docker build command), i tried to run my new image:
docker run myjavaimage MainClass.java
i got this error: ** cp: missing destination file operand after ‘MainClass.java’ **
How can i resolve this? thx
I think you want this Dockerfile:
FROM openjdk:7
WORKDIR /usr/src/myapp
COPY MainClass.java .
RUN javac MainClass.java
ENV CLASSPATH=/usr/src/myapp
CMD java MainClass
When you docker build this image, it COPYs your Java source file from your local directory into the image, compiles it, and sets some metadata telling the JVM where to find the resulting .class files. Then when you launch the container, it will run the single application you've packaged there.
It's common enough to use a higher-level build tool like Maven or Gradle to compile multiple files into a single .jar file. Make sure to COPY all of the source files you need in before running the build. In Java it seems to be common to build the .jar file outside of Docker and just COPY that in without needing a JDK, and that's a reasonable path too.
In the Dockerfile you show, Docker combines ENTRYPOINT and CMD into a single command and runs that command as the single main process of the container. If you provide a command of some sort at the docker run command, that overrides CMD but does not override ENTRYPOINT. You only get one ENTRYPOINT and one CMD, and the last one in the Dockerfile wins. So you're trying to run container processes like
# What's in the Dockerfile
cp /bin/sh -c "ls /usr/src/myapp"
# Via your docker run command
cp MainClass.java
As #QuintenScheppermans suggests in their answer you can use a docker run -v option to inject the file at run time, but this will happen after commands like RUN javac have already happened. You don't really want a workflow where the entire application gets rebuilt every time you docker run the container. Build the image during docker build time, or before.
Two things.
You have used CMD twice.
CMD can only be used once, think of it as the purpose of your docker image. Every time a container is run, it will always execute CMD if you want multiple commands, you should use RUN and then lastly, used CMD
FROM openjdk:
MAINTAINER MyPerson
WORKDIR /usr/src/
ENTRYPOINT ["cp"]
RUN /usr/src/myapp
RUN ls /usr/src/myapp
Copying stuff into image
There is a simple command COPY the syntax being COPY <from-here> <to-here>
Seems like you want to run myjavaimage so what you will do is
COPY /path/to/myjavaimage /myjavaimage
CMD myjavaimage MainClass.java
Where you see the arrows, I've just written dummy code. Replace that with the correct code.
Also, your Dockerfile is badly created.
ENTRYPOINT -> not sure why you'd do "cp", but it's an actual entrypoint. Could point to the root dir of your project or to an app that will be run.
Don't understand why you want to do ls /usr/src/myapp but if you do want to do it, use RUN and not CMD
Lastly,
Best way to debug docker containers are in interactive mode. That means ssh'ing in to your container, have a look around, run code, and see what is the problem.
Run this: docker run -it <image-name> /bin/bash and then have a look inside and it's usually the best way to see what causes issues.
This stackoverflow page perfectly answers your question.
COPY foo.txt /data/foo.txt
# where foo.txt is the relative path on host
# and /data/foo.txt is the absolute path in the image
If you need to mount a file when running the command:
docker run --name=foo -d -v ~/foo.txt:/data/foo.txt -p 80:80 image_name

airflow exporter - binary build dockerfile error

When I build binary and package to alpine image for airflow exporter with my Dockerfile I am getting error.
Not sure how to fix this error.
+++++ Error while docker build +++++++++
---> Running in caebfe9a04a0
stat mage.go: no such file or directory
The command '/bin/sh -c cd /go/src/github.com/airflow_exporter/; go run mage.go binary' returned a non-zero code: 1
+++++++++++++++
++++++++++++++++ My Dockerfile +++++++++++++++++
FROM golang:1.11.1 AS builder
RUN mkdir -p /go/src/github.com/airflow_exporter
ADD . /go/src/github.com/airflow_exporter
RUN cd /go/src/github.com/airflow_exporter/;
go run mage.go binary
FROM alpine:3.4
COPY --from=builder /go/src/github.com/airflow_exporter/bin/*/airflow_exporter /airflow_exporter
EXPOSE 9112
ENTRYPOINT [ "/airflow_exporter" ]
+++++++++++++++++++++++++++++++++
The first line in your docker build output shows that the binary file mage.go is not in the expected location (stat mage.go: no such file or directory). Check through the steps that lead up to reading that binary. I would look through the following:
Check that the directory from which your docker file is being run
actually contains the mage.go binary, since you are adding the
contents of your pwd to the airflow_exporter (ADD .
/go/src/github.com/airflow_exporter)
Try adding the full file path to mage.go
If the above fails, try running your own stat commands on the file throughout the process, and continue breaking down the problem from there

How to execute start script in Dockerfile

I want to create new image from jdk, build it, it works; when I run my new imag, it return container id, but can't get the process-info when docker ps,this is my dockerfile:
# specified jdk version
FROM openjdk:7-jre
# env
ENV APP_HOME /usr/src/KOAL-OCSP
ENV PATH $APP_HOME:$PATH
# copy my app in .zip to /usr/src
COPY myapp.zip /usr/src/
# unzip copy file
RUN unzip /usr/src/myapp.zip
WORKDIR $APP_HOME
#port
expose 80
#run the setup script of my app when start container
CMD ["service.sh" "start"]
service.sh is a setup script is my app root-file, I wish the script can auto execuced when run the new self-build image.
I suspect that the container has executed and exited successfully. The container will stay alive as long as the processes that you have started using the services.sh script is still running.
In your case, the services.sh has executed and exited, thus causing the container to exit.
To view all containers, use docker ps -a
Update:
The error /bin/sh: 1: ./service.sh: not found indicates that the services.sh script is not found under $APP_HOME inside the Docker image. Make sure you add it under $APP_HOME using
ADD `service.sh` $APP_HOME
CMD ["service.sh" "start"]
The above is not valid json, it's missing a comma in the the array, so docker will execute it as a string which will fail since ["service.sh" will not be found as a command to run.
If you use docker ps -a you will see a list of all containers, including exited ones. From there, you can use docker logs $(docker ps -lq) to see the logs of the last container you tried to run. And you can docker inspect $(docker ps -lq) to see all the details about the last container it tried to run, including the exit code.
To get past your current error, correct your syntax with the missing comma:
CMD ["service.sh", "start"]
For the next problem you are seeing, a "not found" error can indicate:
The command doesn't exist inside your container (at the expected location). In your scenario, make sure it is included in /usr/src/KOAL-OCSP that you unzip in your image.
The shell script does exist, but calls a binary on the first line that doesn't exist in your image. E.g. if you call #!/bin/bash but only have /bin/sh in your container. This also happens when you edit the files on a Windows system and have ^M linefeeds that become part of the name of the binary that the container is looking for (/bin/sh^M instead of /bin/sh).
For binaries, this can happen if executable you are running has library dependencies that do not exist inside your container. For example, if you build on a glibc environment and run the container with a musl libc environment of Alpine, this same error message will appear.

Add file into hidden folder

I am trying to add a file from the host machine to the container into a hidden folder but it does not seem to be working. This is my Dockerfile:
FROM frekele/gradle:2.14.1-jdk8
MAINTAINER Fran Garcia <myemail#email.com>
ADD gradle.properties /root/.gradle/gradle.properties
# Run
CMD ["ls", "/root/.gradle/gradle.properties"]
But the file is not added. I can add this file to any other folder but not to a hidden one. Does anybody have any idea why this happens or how to fix it?
This is working as designed. Your error is in the way you use the CMD. Here's what's happening:
I created your Dockerfile, and then ran docker build . -t gr-foo
I started the container using docker run -it --rm gr-foo, which produced the following output line (among others):
exec: fatal: unable to exec ls /root/.gradle/gradle.properties: No such file or directory
I guess this is the error that you're seeing as well.
The CMD expects an array of values, where each entry is treated as one part of the command line. You need to change it to:
CMD ["ls", "/root/.gradle/gradle.properties"]
This will treat ls and the path as two different items, resulting in the desired output:
/root/.gradle/gradle.properties
ls exited 0

Resources