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
Related
I am new to docker, and I am trying to setup docker for spring boot project.
Here is my Dockerfile
FROM maven:3.6.3-jdk-11-slim AS build
WORKDIR usr/src/springboot
COPY . ./
RUN mvn install
RUN mvn clean package
#
# Package stage
#
FROM openjdk:11-jre-slim
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
COPY --from=build /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
CMD ["java","-jar", "./springboot.jar"]
Which works completely fine and I can access hello world from localhost:8080
But my confusion is how to make any changes in java file reflect in the docker container? how do I recompile the .jar file.
I tried something like docker exec -it strange_shaw "mvn clean package"
But it throws error exec: "mvn clean package": executable file not found in $PATH: unknown
when you are using double FROM instruction inside your Dockerfile , Docker will keep only the latest FROM and use the previous FROM to build the next one.
So it is simple to find this error because FROM maven:3.6.3-jdk-11-slim AS build only used to build the next step FROM openjdk:11-jre-slim and will be removed from the final image(this strategy used to minimize the docker image size).
I hope that give you a clear idea about Dockerfile with multiple stage.
Build your jar outside after your Dockerfile will be like that:
FROM openjdk:11-jre-slim
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
COPY /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
CMD ["java","-jar", "./springboot.jar"]
The issue was that maven was not installed within the container. So I changed my Dockerfile to
FROM openjdk:11
ARG JAR_NAME="springboot-0.0.1-SNAPSHOT"
WORKDIR /usr/src/springboot
EXPOSE 8080
#COPY --from=build /usr/src/springboot/target/${JAR_NAME}.jar ./springboot.jar
#CMD ["java","-jar", "./springboot.jar"]
RUN apt-get update; apt-get install curl -y
ARG MAVEN_VERSION=3.6.3
# 2- Define a constant with the working directory
ARG USER_HOME_DIR="/root"
# 4- Define the URL where maven can be downloaded from
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries
# 5- Create the directories, download maven, validate the download, install it, remove downloaded file and set links
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& echo "Downloading maven" \
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
\
&& echo "Unziping maven" \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
\
&& echo "Cleaning and setting links" \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
This will install maven inside the container and set $PATH
docker run -it -v "$(pwd)":/usr/src/springboot -p 8080:8080 spring-boot-app
Run above command to start container and map your project folder to container folder and local port to container port. So both are in sync if you do any changes in project.
docker exec -it <container_name> mvn verify
Above command will create the .jar file. Restart the container.
docker exec -it <container_name> java -jar target/<file_name>.jar
Will execute the jar file in port 8080 and will be accessible in your localhost:8080
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 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/
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 am trying make a docker image of a java project. I first created a directory and in that I created a docker.txt file. The files contains this
FROM java:8
# Install maven
RUN apt-get update
RUN apt-get install -y maven
WORKDIR /home/mmt/CouchBaseClient/CB-RestAPI/CouchBaseThinClient
# Prepare by downloading dependencies
ADD pom.xml /home/mmt/CouchBaseClient/CB-RestAPI/CouchBaseThinClient/pom.xml
RUN ["mvn", "dependency:resolve"]
RUN ["mvn", "verify"]
# Adding source, compile and package into a fat jar
ADD src /home/mmt/CouchBaseClient/CB-RestAPI/CouchBaseThinClient/src
RUN ["mvn", "package"]
EXPOSE 4567
CMD ["/usr/lib/jvm/java-8-openjdk-amd64/bin/java", "-jar", "target/sparkexample-jar-with-dependencies.jar"]
and then I run in terminal the following command
docker build -t API .
I get the following error
invalid value "API" for flag -t: Error parsing reference: "API" is not a valid repository/tag
See 'docker build --help'.
Docker is complaining about "API" in the sense that it's not allowed to have a tag name with one or more character in uppercase:
$ docker build -t FOO .
repository name component must match "[a-z0-9](?:-*[a-z0-9])*(?:[._][a-z0-9](?:-*[a-z0-9])*)*"
Usually "recipes" to build Docker images are written in a file named Dockerfile, anyway you can continue to use docker.txt using the -f option:
docker build -f docker.txt -t api .