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/
Related
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
I have a docker container which runs a springboot java application. Dockerfile:
# Create container with java preinstalled
FROM openjdk:8-jdk-alpine
# Create app directory
VOLUME /tmp
# Handle Arguments
ARG JAR_FILE
ARG ENV_NAME
ENV SPRING_PROFILES_ACTIVE=${ENV_NAME}
RUN echo ${ENV_NAME}
# Bundle app source
COPY ${JAR_FILE} app.jar
COPY application.yml application.yml
# Run the server
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.config.location=application.yml","-jar","app.jar"]
Now, I have a custom library I need to install in that container. I'll need to copy the installation, extract it, run the install script and answer prompts (Y/n)
I understood the easiest way to do this is to connect to the container, install the package and commit the changes.
First - I start the container using:
docker run --name local-jdk8 -d openjdk:8-jdk-alpine
The next step is to copy the data and run the install script, but the container keeps on exiting since the run command is empty ("/bin/sh") which means I can't run
docker exec -it local-jdk8 bash
Any ideas on how I can modify such a container?
Solved it using expect library
My dockerfile :
# Create container with java preinstalled
FROM openjdk:8
# Create app directory
VOLUME /tmp
# Handle Arguments
ARG JAR_FILE
ARG ENV_NAME
ARG DRIVER_FILE
# Environment
ENV SPRING_PROFILES_ACTIVE=${ENV_NAME}
RUN echo ${ENV_NAME}
# Fingerprint Driver
RUN apt-get update -y
RUN apt-get install -y expect
COPY ${DRIVER_FILE} driver.tar.gz
COPY driver-install.exp driver-install.exp
RUN tar -xzf driver.tar.gz
RUN /driver-install.exp
# Copy app source
COPY ${JAR_FILE} app.jar
COPY application.yml application.yml
# Run the server
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.config.location=application.yml","-jar","app.jar"]
driver-install.exp is the expect scripts that automatically interacts with the package installation
For what it's worth, here is a little trick that allows you to keep your container running to modify and commit it:
docker run --name local-jdk8 -d openjdk:8-jdk-alpine tail -f /dev/null
Furthermore, there is no bash installed on the container so sh will have to do:
docker exec -it local-jdk8 sh
Nevertheless, modifying Dockerfile is the better approach, since your change is persisted in code, rather than done on an potentially ephemeral container.
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 am new to docker and I have tried following some tutorials and documentation on USING DOCKER TO EFFICIENTLY CREATE MULTIPLE TOMCAT INSTANCES, but am having trouble getting the service to run via the docker run command.
I have a docker file with the following coding below
FROM ubuntu:precise
MAINTAINER Quinten Krijger < qkrijger [at] gmail {dot} com>
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update && apt-get -y install python-software-properties
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update && apt-get -y upgrade
RUN echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-get -y install oracle-java7-installer && apt-get clean
RUN update-alternatives --display java
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle" >> /etc/environment
When I build using the command
docker build -t quintenk/jdk7-oracle .
it shows the build is successful.
In the another dockerfile, I try to create create other images based on it
I have another docker in a different path with the following commands
FROM quintenk/jdk7-oracle
MAINTAINER Quinten Krijger "qkrijger#gmail.com"
RUN apt-get -y install tomcat7
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle" >> /etc/default/tomcat7
EXPOSE 8080
CMD service tomcat7 start && tail -f /var/lib/tomcat7/logs/catalina.out
I use the following command to run the docker file
docker run -d quintenk/tomcat7
But it shows as tomcat instance fail
* Starting Tomcat servlet engine tomcat7
...fail!
If I give the command
docker ps
also there is no instance running on my machine
I'm obviously doing something wrong and I'm getting the behaviour on my OSX
For testing you can create a Dockerfile with CMD ["bash"].
start the container then login to the container using the command
docker -it exec container_name bash
then start tomcat and checkout out the tomcat logs
Try running the image with following command
docker run -dt --cap-add SYS_PTRACE quintenk/tomcat7
I have faced this issue. Finally, I am able to run the tomcat.Follow below steps.
Step-1: open terminal Ctrl+Alt+t
Step-2: then on terminal go inside the running container, type commands
sudo docker exec -it containerID /bin/bash
Now to check tomcat is running or not, type your container IP address followed by colon(:) 8080 port on the browser. like 172.17.0.2:8080.
Hope it helps!!
** 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).