Add file into hidden folder - docker

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

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 use a executable file created in a docker file in the same docker file

What I want: i want to run a cpp file witch use opencv inside a container
What I've done:
installing an image of opencv:
docker pull spmallick/opencv-docker:opencv
create a docker file
FROM spmallick/opencv-docker:opencv
RUN ["g++ a.cpp -o a.out"]
COPY . .
CMD ["./a.out"]
bash command
sudo docker build -t project_opencv
OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "g++ a.cpp -o a.out": executable file not found in $PATH": unknown
first I try this with cmd instead of RUN (how to use cmd inside a Dockerfile to compile a file). It couldn't find a.out although I've done COPY . .
Now it seems that there is a problem to create a.out
When you use the JSON-array form of RUN (or CMD or ENTRYPOINT), you explicitly provide the set of "words" that make up the command. As you've shown it, it is a single word, and Docker tries to run it as such, including the spaces in the command name it's looking up. You need to split it into its constituent words yourself.
RUN ["g++", "a.cpp", "-o", "a.out"]
The reverse side of this is that there is no splitting, expansion, or interpolation that happens in this command:
# Preserves both the literal $ and the spaces
RUN ["cp", "dollars-word-$word.txt", "file name with spaces.txt"]
Especially for RUN it's common to use the shell form. This wraps the command in sh -c so it works like an ordinary command. (There are technical reasons you might not want this for CMD and especially ENTRYPOINT.)
RUN g++ a.cpp -o a.out
RUN cp dollars-word-\$word.txt, 'file name with spaces.txt'
RUN tail env-var-$word.txt
First copy your a.cpp into docker image then your RUN command will work , I am not sure about your RUN ["g++ a.cpp -o a.out"] command this will work or not but try this:
FROM spmallick/opencv-docker:opencv
COPY . .
RUN ["g++ a.cpp -o a.out"]
CMD ["./a.out"]
I think you need to check the permissions for the files you have copied, from your host machine into the container.
You could also check the directory you are in, and if you need to change the directory, then you can use the WORKDIR command in your Dockerfile.
It would also be useful to start a bash session in the container, and run the commands manually. For each successful command, you can enter the command into the Dockerfile, or if you encounter an error, you can also troubleshoot more easily, rather than change the Dockerfile each time and run, and if you need to debug which is time consuming.

Docker /bin/sh: COPY: command not found

I tried to run this below command :
RUN if [ "$someargs" = "AAA" ]; then COPY from/ /usr/local/; fi
I got this error: This command returned a non-zero code: 127
You can't do, inside a
RUN
a Dockerfile
COPY
You need to find another way, you may have a script that creates different Dockerfile based on your test.
As the error states, COPY is not a shell command, it's a docker instruction which is supposed start with a newline.
Syntax -
INSTRUCTION arguments
You can use cp command in Dockerfile. However, you need to COPY the complete directory structure in your image to perform a cp operation. I haven't yet tried this yet but logically it should work.
You can apply the shell logic at your host & use docker cp to actually copy contents from/to host to/from container.
Ref - https://docs.docker.com/engine/reference/builder/

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.

docker run complains file not found

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

Resources