Docker - Execute script on container - java

I am starting with docker. I need to create an image with an configured installation of WildFly server that i have on my local storage, this wildfly has a java app that runs over 80 port.
I want to create a container that runs the standalone.sh file and i can access it at via locahost url on my local machine.
When i try to access bin directory i suposed that it is ok, but when execute the standalone.sh file, get this error:
/bin/sh: ./standalone.sh: No such file or directory
How can i create the image and runs the container?
This is the image structure:
/opt
├── DEJ_Wildlfy
├── wildfly-11.0.0.Final
├── appclient
├── bin
├── standalone.bat
├── standalone.conf
├── standalone.sh <--
├── ......
├── .....
My DockerFile
FROM centos
#INSTALL JAVA
RUN yum -y install java-11-openjdk
RUN java -version
RUN mkdir /opt/DEJ_Wildfly/
#SET OPT AS WORK DIRECTORY
WORKDIR /opt/DEJ_Wildfly/
#COPY WILDFLY SERVER
COPY wildfly-11.0.0.Final .
RUN chmod 777 -R /opt/DEJ_Wildfly/
RUN chmod +X -R /opt/DEJ_Wildfly/
#ENV JBOSS_HOME /opt/DEJ_Wildfly/wildfly-11.0.0.Final
WORKDIR /opt/DEJ_Wildfly/wildfly-11.0.0.Final/bin/
RUN pwd
RUN ls
CMD standalone.sh -b=0.0.0.0
RUN ./standalone.sh -b 0.0.0.0
CMD tail -f /var/log/syslog
#EXPOSE 80
The log of image creation docker build --tag wildfly_dej_website:2.0 .:
Sending build context to Docker daemon 597.4MB
Step 1/14 : FROM centos
---> 300e315adb2f
Step 2/14 : RUN yum -y install java-11-openjdk
---> Using cache
---> f333f6149e02
Step 3/14 : RUN java -version
---> Using cache
---> 0110143899c7
Step 4/14 : RUN mkdir /opt/DEJ_Wildfly/
---> Running in 6ac37e57c9c0
Removing intermediate container 6ac37e57c9c0
---> 70331f8da178
Step 5/14 : WORKDIR /opt/DEJ_Wildfly/
---> Running in 9d6169492b25
Removing intermediate container 9d6169492b25
---> b597e72f443e
Step 6/14 : COPY wildfly-11.0.0.Final .
---> 44db2164e32f
Step 7/14 : RUN chmod 777 -R /opt/DEJ_Wildfly/
---> Running in f56c31767282
Removing intermediate container f56c31767282
---> 2a2e074ac50f
Step 8/14 : RUN chmod +X -R /opt/DEJ_Wildfly/
---> Running in 8fbfbade9abf
Removing intermediate container 8fbfbade9abf
---> dea87a4dc31c
Step 9/14 : WORKDIR /opt/DEJ_Wildfly/wildfly-11.0.0.Final/bin/
---> Running in 20dc329acd81
Removing intermediate container 20dc329acd81
---> 3fe555e41d7d
Step 10/14 : RUN pwd
---> Running in 1af150bd74bb
/opt/DEJ_Wildfly/wildfly-11.0.0.Final/bin
Removing intermediate container 1af150bd74bb
---> 998b9c9876a3
Step 11/14 : RUN ls
---> Running in d2ca989a5b0a
Removing intermediate container d2ca989a5b0a
---> a0dd9c8263ae
Step 12/14 : CMD standalone.sh -b=0.0.0.0
---> Running in 560a62f26227
Removing intermediate container 560a62f26227
---> ca1aed8ec311
Step 13/14 : RUN ./standalone.sh -b 0.0.0.0
---> Running in d9820e7fb967
/bin/sh: ./standalone.sh: No such file or directory
The command '/bin/sh -c ./standalone.sh -b 0.0.0.0' returned a non-zero code: 127

When you run docker build you are creating the image, you don't want to run wildfly during image creation - the CMD command specifies the default command to run when starting a container with that image.
Any reason not to use the docker hub image that already exists for wildfly? https://hub.docker.com/r/jboss/wildfly
You can see their Dockerfile here: https://github.com/jboss-dockerfiles/wildfly/blob/master/Dockerfile

Related

Docker - Run Wildfly standalone

I have a Ubuntu server with a WildFly standalone installation configured, here i have a Java app that is accesible on port 80, i want to dockerize it. When create the container, i'm copying the entire WildFly directory from my local storage to the container, when i run the command docker run -d --name dej_website -p 80:80 wildfly_dej_website:2.0 the container start, but when try to access in my local machine localhost doesn't works.
What am i doing wrong? How can i do to access to this container, with the configured server?
This is my dockerfile
FROM centos
#INSTALL JAVA
RUN yum -y install java-11-openjdk
RUN java -version
RUN mkdir /opt/DEJ_Wildfly/
#SET OPT AS WORK DIRECTORY
WORKDIR /opt/DEJ_Wildfly/
#COPY WILDFLY SERVER FILES
COPY wildfly-11.0.0.Final .
#DEFINE ENVIRONMENT VARIABLE
ENV JBOSS_HOME /opt/DEJ_Wildfly/wildfly-11.0.0.Final/
#EXECUTE SH FILE
CMD JBOSS_HOME/bin/standalone.sh -b=0.0.0.0
CMD tail -f /var/log/lastlog
#EXPOSE 80
This is the log of docker build --tag wildfly_dej_website:2.0 . command:
Sending build context to Docker daemon 597.4MB
Step 1/9 : FROM centos
---> 300e315adb2f
Step 2/9 : RUN yum -y install java-11-openjdk
---> Using cache
---> f333f6149e02
Step 3/9 : RUN java -version
---> Using cache
---> 0110143899c7
Step 4/9 : RUN mkdir /opt/DEJ_Wildfly/
---> Running in 88bc6f0632c1
Removing intermediate container 88bc6f0632c1
---> c0ab7cc8a364
Step 5/9 : WORKDIR /opt/DEJ_Wildfly/
---> Running in 84705355ac2c
Removing intermediate container 84705355ac2c
---> 6986f8229cb0
Step 6/9 : COPY wildfly-11.0.0.Final .
---> 3caf8ec0e5d0
Step 7/9 : ENV JBOSS_HOME /opt/DEJ_Wildfly/wildfly-11.0.0.Final/
---> Running in b5f649e4e2e0
Removing intermediate container b5f649e4e2e0
---> 27d775c3d0cb
Step 8/9 : CMD JBOSS_HOME/bin/standalone.sh -b=0.0.0.0
---> Running in c7ffddb4bcdf
Removing intermediate container c7ffddb4bcdf
---> 533d3836f94d
Step 9/9 : CMD tail -f /var/log/lastlog
---> Running in f92aca5a63ba
Removing intermediate container f92aca5a63ba
---> 974e6ec6c415
Successfully built 974e6ec6c415
Successfully tagged wildfly_dej_website:2.0
This is the list of containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95ee0139f42c wildfly_dej_website:2.0 "/bin/sh -c 'tail -f…" 4 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp dej_website
The problem here is in your dockerfile.
You need to add a CMD at the end of your dockerfile to run the command you want and keep it running for the docker file to stay alive and provide you with logs through docker logs command.
Add a line like this to your dockerfile:
RUN ./standalone.sh -b 0.0.0.0
CMD tail -f /var/log/syslog
And to make the port accessible on the local machine you'll need to use -p option when issuing the docker run command like this:
docker run -d -p 127.0.0.1:80:8080 <image>
you can find out more here:
https://docs.docker.com/engine/reference/commandline/run/

Committing container changes of files generated in /workspace (even using makefile) doesn't persist into new instances of the image

I am using a java program which generates a lot of files (i.e ANTLR to generate parsers using a grammar). When I perform:
java -cp "/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH" org.antlr.v4.Tool Java8.g4 && javac *.java
in the interactive shell using a docker image, certain java files and their classes are generated in the current directory.
But when I perform this in the Dockerfile using:
RUN java -cp "/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH" org.antlr.v4.Tool Java8.g4 && javac *.java
the files don't seem to be generated.
Any reason why this is happening and how I could perhaps correct this to generate the files using the RUN instruction in the Dockerfile?
UPDATE 1:
More info, my Dockerfile looks like this:
FROM blah/blah_java
MAINTAINER blabla
RUN apt-get install -y make wget
RUN mkdir -p /usr/java && wget -O java.tar.gz http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-x64.tar.gz --header "Cookie: oraclelicense=accept-securebackup-cookie" && tar xfz java.tar.gz -C /usr/java
ENV JAVA_HOME /usr/java/jdk1.8.0_40
ENV PATH $PATH:$JAVA_HOME/bin
.
.
.
RUN mkdir -p /usr/local/lib && cd /usr/local/lib && wget http://www.antlr.org/download/antlr-4.5-complete.jar
ENV CLASSPATH=".:/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH"
.
.
.
RUN mkdir -p /workspace/antlr-test/
ADD Java8.g4 /workspace/antlr-test/
WORKDIR /workspace/antlr-test/
RUN java -cp "/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH" org.antlr.v4.Tool Java8.g4 && javac *.java
UPDATE 2:
Outputs something like this on building:
Step 1 : FROM blah/blah_java
---> ff083dedeeac
Step 2 : MAINTAINER blabla
---> Using cache
---> b2cc75d73e93
Step 3 : RUN apt-get install -y make wget
---> Using cache
---> e8f8162ca496
Step 4 : RUN mkdir -p /usr/java && wget -O java.tar.gz http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-x64.tar.gz --header "Cookie: oraclelicense=accept-securebackup-cookie" && tar xfz java.tar.gz -C /usr/java
---> Using cache
---> 1587950d483a
Step 5 : ENV JAVA_HOME /usr/java/jdk1.8.0_40
---> Using cache
---> 1787ea5c62c4
Step 6 : ENV PATH $PATH:$JAVA_HOME/bin
---> Using cache
---> 5f9082023d2c
.
.
.
Step 10 : RUN mkdir -p /usr/local/lib && cd /usr/local/lib && wget http://www.antlr.org/download/antlr-4.5-complete.jar
---> Using cache
---> 7a67f6c4c572
Step 11 : ENV CLASSPATH ".:/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH"
---> Using cache
---> c64a24b809f0
.
.
.
Step 14 : RUN mkdir -p /workspace/antlr-test/
---> Using cache
---> 27b18aab274f
Step 15 : ADD Java8.g4 /workspace/antlr-test/
---> Using cache
---> 87ace495d90b
Step 16 : WORKDIR /workspace/antlr-test/
---> Using cache
---> 31089935532b
Step 17 : RUN java -cp "/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH" org.antlr.v4.Tool Java8.g4 && javac *.java
---> Using cache
---> 773c302ecf5e
Successfully built 773c302ecf5e
UPDATE 3:
On having 'ls' in the last RUN instruction as:
RUN java -cp "/usr/local/lib/antlr-4.5-complete.jar:$CLASSPATH" org.antlr.v4.Tool Java8.g4 && javac *.java && ls -la
I find that the files generated are listed correctly. It is in committing the changes that this is not working.
UPDATE 4:
I also find that in the interactive shell of the image, the files are generated. But when I commit the changes, they are not visible the next time I run them. On performing a `docker diff , before committing the changes, I find that the files generated are not listen here.
UPDATE 5:
Looks like the files generated are not persisted across image builds/commits if they are in one of the subdirectories in /workspace directory. If they are made to generate outside workspace, they seem to persist through builds/commits.
Docker keeps old build steps as a optimization (this is part of dockers strength).
If you change a Dockerfile and build, the new image will use cache for everything BEFORE the change and rebuild everything after it.
Restoring the changed line in the Dockerfile will re-activate the cached build.
You can invalidate the cache by flags to docker build or you can add lines to your Dockerfile to remind you when you invalidated everything below that part last. For example adding/editing the following line will invalidate the cache below it.
ENV FORCED_DOCKER_REBUILD=2018-01-01

Files not getting added in docker workspace

I have docker file
FROM java:8
# Install maven
RUN apt-get update
RUN apt-get install -y maven
WORKDIR /code/
# Prepare by downloading dependencies
#ADD pom.xml /mmt/CouchBaseClient/CB-RestAPI/CacheService/pom.xml
#RUN ["mvn", "dependency:resolve"]
#RUN ["mvn", "verify"]
ADD cacheService-0.0.1-SNAPSHOT.jar /code/cacheService-0.0.1-SNAPSHOT.jar
ADD couchclient-0.0.1-SNAPSHOT.jar /code/couchclient-0.0.1-SNAPSHOT.jar
EXPOSE 4567
CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml" ]
When I build this file I get the following output
Sending build context to Docker daemon 35.46 MB
Step 1 : FROM java:8
---> 736600fd4ae5
Step 2 : RUN apt-get update
---> Using cache
---> a3466698c29d
Step 3 : RUN apt-get install -y maven
---> Using cache
---> d0fb8e77f89a
Step 4 : WORKDIR /code/
---> Using cache
---> 197735d2da02
Step 5 : ADD cacheService-0.0.1-SNAPSHOT.jar /code/cacheService-0.0.1-SNAPSHOT.jar
---> 9ba30f5a2144
Removing intermediate container bd3c072ebbc6
Step 6 : ADD couchclient-0.0.1-SNAPSHOT.jar /code/couchclient-0.0.1-SNAPSHOT.jar
---> ef59315ed7fe
Removing intermediate container 0da1a69bdb51
Step 7 : EXPOSE 4567
---> Running in a2b32799dd6c
---> 3fb2b534d7c5
Removing intermediate container a2b32799dd6c
Step 8 : CMD /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml
---> Running in efb44e2bcdb3
---> 56637dfacc0d
Removing intermediate container efb44e2bcdb3
Successfully built 56637dfacc0d
But no directory named code is being made and so no files are being added even though it is giving no compilation error
Used method suggested by #VonC
ENTRYPOINT ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar" ]
and then used this command to run the image
docker run <image> -d <arguments>
First, don't forget that ADD <src>... <dest> can invalidate the cache for all following instructions from the Dockerfile if the contents of <src> have changed. See "Best practices" and use COPY instead of ADD.
In both cases (ADD or COPY), if <dest> doesn’t exist, it is created along with all missing directories in its path.
So no mkdir necessary.
COPY cacheService-0.0.1-SNAPSHOT.jar /code/
COPY couchclient-0.0.1-SNAPSHOT.jar /code/
Otherwise, the file cacheService-0.0.1-SNAPSHOT.jar in the folder /code/cacheService-0.0.1-SNAPSHOT.jar/!
Finally, to be sure that the files are where they should be, open a bash:
docker run --rm -it <yourImage> bash
Or, if you have a running container:
docker exec -it <yourContainer> bash
And check what ls /code returns.
Also:
docker run --rm -it --entrypoint /bin/sh <yourImage> -c "ls -alrt"
The OP Legendary Hunter confirms in the comments the files are there.
The issue comes from the CMD which is not fully in exec form.
Each parameter should be in its own quotes:
CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar", "server", "cacheService.yml" ]
If the last parameters are grouped together, CMD tries to access the jar file named "couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml", which obvioulsy does not exist.
Hence the error message:
"Error: Unable to access jarfile couchclient-0.0.1-SNAPSHOT.jar server cacheService.yml"
Instead of using CMD, use ENTRYPOINT (with the sa me exec form, each arg in its own double-quotes), and leave CMD undefined.
That way, the arguments you will add to your docker run command will be passed to the ENTRYPOINT (which runs java -jar ...)
Since "server", "cacheService.yml" are the two arguments to be passed to the running container:
ENTRYPOINT ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "couchclient-0.0.1-SNAPSHOT.jar" ]
Build and then:
docker run --rm -it <image> server cacheService.yml
Once you know it is working, launch it in detached mode:
docker run -d <image> server cacheService.yml
Try this before WORKDIR line:
RUN mkdir /code

I have built the commands by using docker and the output is successsful and I am unable to open the localhost 7604

** These are the commands which i have used**
1.$ sudo docker build -t="prabakar/java7" .
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:saucy
---> 195eb90b5349
Step 1 : RUN apt-get update && apt-get -y upgrade
---> Using cache
---> 4f1ffd94ad85
Step 2 : RUN apt-get -y install software-properties-common
---> Using cache
---> 1d972cf31614
Step 3 : RUN add-apt-repository ppa:webupd8team/java
---> Using cache
---> a479b931080e
Step 4 : RUN apt-get -y update
---> Using cache
---> 4953b8e1e1b2
Step 5 : RUN echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 boolean true" | debconf-set-selections
---> Using cache
---> 074e0bcc47f7
Step 6 : RUN apt-get -y install oracle-java7-installer
---> Using cache
---> 5de174a53567
Step 7 : RUN apt-get -y install tomcat7
---> Using cache
---> fbaf2c3b594b
Step 8 : RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle" >> /etc/default/tomcat7
---> Using cache
---> d75cc6e4d41f
Step 9 : EXPOSE 7604
---> Using cache
---> e28931a42d90
Step 10 : RUN mkdir /var/lib/tomcat-7.0-doc
---> Running in a07cc29cd8bb
---> 029a57ec552a
Removing intermediate container a07cc29cd8bb
Step 11 : RUN wget http://tomcat.apache.org/ -P /var/lib/tomcat-7.0-doc
---> Running in b5b9a1c271c4
--2014-12-22 07:26:40-- http://tomcat.apache.org/
Resolving tomcat.apache.org (tomcat.apache.org)... 54.172.167.43, 192.87.106.229, 2001:610:1:80bc:192:87:106:229
Connecting to tomcat.apache.org (tomcat.apache.org)|54.172.167.43|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11540 (11K) [text/html]
Saving to: '/var/lib/tomcat-7.0-doc/index.html'
0K .......... . 100% 140M=0s
2014-12-22 07:26:41 (140 MB/s) - '/var/lib/tomcat-7.0-doc/index.html' saved [11540/11540]
---> c5edb92f2f31
Removing intermediate container b5b9a1c271c4
Successfully built c5edb92f2f31
$ sudo docker run -i -t prabakar/java7 /bin/bash
root#6b635fc414c5:/# exit
$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b635fc414c5 prabakar/java7 "/bin/bash" 25 seconds ago Exited
You are running bin/bash in the docker. When that runs, you type 'exit', so bash terminates, and then the docker terminates, as can be seen in the docker ps command. Try leaving that window open after you run
sudo docker run -i -t prabakar/java7 /bin/bash
and then connect to port 7604.
A better solution would be to make your docker run tomcat as its main command. Try:
sudo docker run -i -t prabakar/java7 "tomcat/bin/catalina.sh run"
You may need to disable tomcat autorun on startup for this to work (apt-get probably starts the service automatically).

How to run the COPY and RUN command in same container?

I am trying to install java from the rpm file to docker centos image.
> Step 0 : FROM centos:latest
---> **34943839435d**
Step 1 : COPY . jdk-6u45-linux-x64-rpm.bin
---> **2055e5db6ae9**
Removing intermediate container 7ae13aaa4424
Step 2 : RUN chmod +x jdk-6u45-linux-x64-rpm.bin && sh jdk-6u45-linux-x64-rpm.bin
---> Running in **c4d6b63576bc**
jdk-6u45-linux-x64-rpm.bin: jdk-6u45-linux-x64-rpm.bin: is a directory
2014/12/16 06:03:34 The command [/bin/sh -c chmod +x jdk-6u45-linux-x64-rpm.bin && sh jdk-6u45-linux-x64-rpm.bin] returned a non-zero code: 126
The error it gives seems to be because of the different containers. How to run command on same container?
Docker file is as below
FROM centos:latest
# Install Java.
COPY . jdk-6u45-linux-x64-rpm.bin
RUN chmod +x jdk-6u45-linux-x64-rpm.bin && \
sh jdk-6u45-linux-x64-rpm.bin
Syntax of COPY is as follows:
COPY <src>... <dest>
So COPY . jdk-6u45-linux-x64-rpm.bin mean copy current directory as jdk-6u45-linux-x64-rpm.bin. Hence the error.
This should give you what you want:
COPY jdk-6u45-linux-x64-rpm.bin jdk-6u45-linux-x64-rpm.bin
Your RUN command is actually running in the same container, the command itself simply failed, probably because you did not set the path correctly.
Here is an example for a very similar docker file that builds and runs correctly (By the way, there is no need for /bin/sh -c, as this is exactly what the run command is doing)
FROM centos:latest
COPY test.bin /root/
RUN /bin/sh -c "chmod +x /root/test.bin"
For more info about the run command see https://docs.docker.com/reference/builder/#run

Categories

Resources