I have a simple hello world java app and I want to build Docker image which runs it.
Dockerfile:
FROM java:8
ADD dist/JavaApplication1.jar /JavaApplication1.jar
RUN chmod +x /JavaApplication1.jar
CMD ["/usr/bin/java -jar /JavaApplication1.jar"] # or CMD ["java -jar /JavaApplication1.jar"]
I build it by:
docker build -t myapp .
And run it by:
docker run -tdi myapp
but it throws:
Error response from daemon: Cannot start container 105f043e565e465639e15d78e92dc74e64562faa510fae5d6ff48da3a58c0125: [8] System error: exec: "java -jar /JavaApplication1.jar": stat java -jar /JavaApplication1.jar: no such file or directory
When run it by:
docker run -ti myapp bash
and do ls, the file is there and when I run "/usr/bin/java -jar /JavaApplication1.jar" then everything is ok. Where is problem?
My docker version: 1.9.0.
On docker 1.9.1 either of your approaches works for me:
exec: "/usr/bin/java -jar /JavaApplication1.jar": stat /usr/bin/java -jar /JavaApplication1.jar: no such file or directory
And that is consistent with documentation for CMD
Basically you can use:
CMD ["executable","param1","param2"]
CMD ["param1","param2"]
CMD command param1 param2
That would suggest first arg is a binary not a whole cmd, and that's the reason of your error. If you split up cmd as on example above that it will work just fine.
FROM java:8
ADD dist/JavaApplication1.jar /JavaApplication1.jar
CMD ["java", "-jar", "/JavaApplication1.jar"]
Bonus: chmod +x is not needed
Related
I have the following Dockerfile
FROM golang as builder
ARG CADDY_HASH=4b4e99bdb2e327d553a5f773f827f624181714af
WORKDIR /root/caddy
RUN wget -qO- github.com/caddyserver/caddy/archive/"$CADDY_HASH".tar.gz | tar zx --strip-components=1
RUN set -e; cd cmd/caddy && CGO_ENABLED=0 go build
FROM scratch
COPY --from=builder /root/caddy/cmd/caddy/caddy /
ARG PORT=8000
ENV PORT $PORT
EXPOSE $PORT
CMD /caddy file-server --browse --listen :$PORT
I build and run with this command
DOCKER_BUILDKIT=0 docker build -t caddy-static-docker:latest . && docker run -e PORT=8000 -p 8000:8000 caddy-static-docker:latest
Why this won't work and I receive this error?
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh": stat /bin/sh: no such file or directory: unknown.
Use an entrypoint instead of CMD
ENTRYPOINT ["/caddy"]
CMD ["file-server", "--browse", "--listen", "8080"]
Also note the json syntax (exec form), which leads to the things not run in a subshell.
Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.
source: https://docs.docker.com/engine/reference/builder/#cmd
The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c, which does not pass signals. This means that the executable will not be the container’s PID 1 - and will not receive Unix signals - so your executable will not receive a SIGTERM from docker stop .
Source: https://docs.docker.com/engine/reference/builder/#entrypoint
Your PORT will still cause issues. Consider hard coding it.
Use a alpine image which is a lightweight image to run shell commands and it has /bin/sh loaded in it.
The SCRATCH image is basically empty with nothing inside / folder, thus no executables to execute anything that is given as part of CMD.
I am writing this Dockerfile to generate the docker image.
-> Dockerfile
FROM centos:7
MAINTAINER Glen
ENV TZ "Asia/Shanghai"
ADD jdk1.8.0_151 jenkins.war /usr/local/
RUN ln -sv /usr/local/jdk1.8.0_151/bin/java* /bin/
EXPOSE 80
CMD cd /usr/local/jdk1.8.0_151 && nohup java -jar jenkins.war >> jenkins.log &
I thought the CMD line in the Dockerfile should start Jenkins already, but when I build the image and execute it, nothing happens. I have to type the following commands to start Jenkins:
docker exec -ti 65aeca0b6e3ed9f572e87379e181f6941178ec30d8a38f5c4b5ccd0fee97e92e bash
[root#65aeca0b6e3e /]# cd usr/local
[root#65aeca0b6e3e local]# ll
[root#65aeca0b6e3e local]# ps -ef| grep java
[root#65aeca0b6e3e local]# java -jar jenkins.war >> jenkins.log
Is there anyway to change my Dockerfile so that Jenkins and the log can start by itself?
try to use this Dockerfile:
FROM centos:7
MAINTAINER Glen
ENV TZ "Asia/Shanghai"
ADD jdk1.8.0_151 jenkins.war /usr/local/
RUN ln -sv /usr/local/jdk1.8.0_151/bin/java* /bin/
EXPOSE 80
WORKDIR /usr/local/jdk1.8.0_151
CMD ["java", "-jar", "jenkins.war", ">>", "jenkins.log"]
by the way , why not to use the official Docker jenkins
I am building Scigraph database on my local machine and trying to move this entire folder to docker and run it, when I run the shell script on my local machine it runs without error when I add the same folder inside docker and try to run it fails
Am I doing this right way, here's my DOckerfile
FROM goyalzz/ubuntu-java-8-maven-docker-image
ADD ./SciGraph /usr/share/SciGraph
WORKDIR /usr/share/SciGraph/SciGraph-services
RUN pwd
EXPOSE 9000
CMD ['./run.sh']
when I try to run it I'm getting this error
docker run -p9005:9000 test
/bin/sh: 1: [./run.sh]: not found
if I run it using below command it works
docker run -p9005:9000 test -c "cd /usr/share/SciGraph/SciGraph-services && sh run.sh"
as I already marked the directory as WORKDIR and running the script inside docker using CMD it throws error
For scigraph as provided in their ReadMe, you can to run mvn install before you run their services. You can set your shell to bash and use a docker compose to run the docker image as shown below
Dockerfile
FROM goyalzz/ubuntu-java-8-maven-docker-image
ADD ./SciGraph /usr/share/SciGraph
SHELL ["/bin/bash", "-c"]
WORKDIR /usr/share/SciGraph
RUN mvn -DskipTests -DskipITs -Dlicense.skip=true install
RUN cd /usr/share/SciGraph/SciGraph-services && chmod a+x run.sh
EXPOSE 9000
build the scigraph docker image by running
docker build . -t scigraph_test
docker-compose.yml
version: '2'
services:
scigraph-server:
image: scigraph_test
working_dir: /usr/share/SciGraph/SciGraph-services
command: bash run.sh
ports:
- 9000:9000
give / after SciGraph-services and change it to "sh run.sh" ................ and look into run.sh file permissions also
It is likely that your run.sh doesn't have the #!/bin/bash header, so it cannot be executed only by running ./run.sh. Nevertheless, always prefer to run scripts as /bin/bash foo.sh or /bin/sh foo.sh when in docker, especially because you don't know what changes files have been sourced in images downloaded from public repositories.
So, your CMD statement would be:
CMD /bin/bash -c "/bin/bash run.sh"
You have to add the shell and the executable to the CMD array ...
CMD ["/bin/sh", "./run.sh"]
I have an image that is built using this dockerfile.
# vi Dockerfile
FROM openjdk:8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
I can login to container in interactive mode and type this command that works as expected.
java -jar /usr/src/myapp/dist/some.jar
But if I add this line to Dockerfile, I get an error:
CMD ["/usr/src/myapp/dist/some.jar", "java"]
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \" -jar /usr/src/myapp/dist/some.jar\": stat -jar /usr/src/myapp/dist/some.jar: no such file or directory".
How do I add the java command to dockerfile?
You are using thus wrongly. It should be
CMD ["java", "-jar", "/usr/src/myapp/dist/some.jar"]
or
CMD java -jar /usr/src/myapp/dist/some.jar
Why don't you use the same command as you would type in?
CMD ["java", "-jar", "/usr/src/myapp/dist/some.jar"]
I tried to run the CMD "java -jar /tmp/migration.jar update_schema atlas " to create the Keyspace in the cassandra. But its not creating any keyspaces in cassandra. But if i run same command in the command line its creating any idea whats the issue?
My dockerfile is as follows
'FROM tomcat:8-jre8
ENV LANG en_US.UTF-8
ENV COMMAND="update"
ENV ARGS="--logLevel=debug"
WORKDIR /usr/local/tomcat/
ADD /migration.jar /tmp
ADD atlas_migration.sh /usr/local/bin/atlas_migration.sh
CMD ["/bin/sh", "/usr/local/bin/atlas_migration.sh"]
CMD ENTRYPOINT ["java","-jar","/tmp/migration.jar","update_schema", "atlas"]
CMD java -jar /tmp/migration.jar update_schema atlas
ENV CATALINA_OPTS "-Xmx256m -Xms192m"
EXPOSE 8085
CMD ./bin/catalina.sh start && tail -f ./logs/catalina.out'
CMD is for specifying the command the container should run when it starts. If you want to run a command during the build, so the state after execution is persisted in the image, you need to use RUN.
COPY is also preferable to ADD, so the relevant instructions should be:
COPY /migration.jar /tmp
COPY atlas_migration.sh /usr/local/bin/atlas_migration.sh
RUN /usr/local/bin/atlas_migration.sh
RUN ["java","-jar","/tmp/migration.jar","update_schema", "atlas"]