I want to create a Docker image with Java and Docker installed on it. The idea is that the eventual docker container should be able to create Docker images. My Java application executes commands like docker build -t my-image ..
That is why I need Docker installed in my Docker container
In the past I solved the same issue writing the following Dockerfile:
FROM maven:3.6.3-jdk-8
USER root
# Install docker CLI for docker images generation inside the container itself
RUN apt update -y
RUN apt install -y curl
RUN curl https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz | tar xvz -C /tmp/ && mv /tmp/docker/docker /usr/bin/docker
# Customize here your container with your instructions...
Of course, you can change the FROM image as per you needs.
Related
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 a little experience with docker-compose and Laravel, this set goes fine, but how could I make the same with dspace?
I would like to have work directory in my host, not all into container.
I have tried dspace-docker that is in dockerhub, is this one: https://github.com/4Science/dspace-docker, but I had troubles wit him.
Thank you!
The following Docker images can be used to run DSpace locally. There is not yet a published Docker Compose file.
- https://hub.docker.com/r/dspace/dspace-tomcat/
- https://hub.docker.com/r/dspace/dspace-postgres-pgcrypto/
The following page describes how to utilize these images on either Windows or MacOS: https://github.com/DSpace-Labs/DSpace-Docker-Images/blob/master/tutorial.md
Here are the key steps.
Clone DSpace
Configure a local.cfg file that assumes DSpace will run in a container. [dspace-install] will be within the container.
Run the DSpace maven build on your workstation
Run the DSpace ant update in a container to install the code at [dspace-install]
The MacOS setup is described here. See the link above for Windows.
docker network create dspacenet
docker volume create pgdataD6
docker run -it -d --network dspacenet -p 5432:5432 --name dspacedb -v pgdataD6:/pgdata -e PGDATA=/pgdata dspace/dspace-postgres-pgcrypto
docker run -it --rm --network dspacenet -v "$(pwd)"/dspace/target/dspace-installer:/installer -v dspaceD6:/dspace -w /installer dspace/dspace-tomcat ant update clean_backups
docker run -it --network dspacenet -v dspaceD6:/dspace -p 8080:8080 --name dspacetomcat -e DSPACE_INSTALL=/dspace dspace/dspace-tomcat
I'm working on 'dockerizing' a java web application (https://github.com/kermitt2/grobid) which I want to run using jetty.
Here the Dockerfile:
FROM jetty:9.3-jre8
ADD ./grobid-home/target/grobid-home-0.4.1-SNAPSHOT.zip /opt
RUN unzip /opt/grobid-home-0.4.1-SNAPSHOT.zip -d /opt && \
rm /opt/grobid-home-0.4.1-SNAPSHOT.zip && \
apt-get update && apt-get -y --no-install-recommends install libxml2
COPY ./grobid-service/target/grobid-service-0.4.1-SNAPSHOT.war \
/var/lib/jetty/webapps/ROOT.war
The current docker image works perfectly, but it requires the application to be built before (it cannot be built from the fresh git clone).
For example I could not run a build with the docker HUB build system.
What would be the prefereable approach? Build the maven project while building the image or run docker after the build as been successfully finished?
I assume the docker image you are creating is for production.
If you create an image which takes the sources and build the war, you will have to embed :
The JDK
Maven
Your sources
Each of these are completely useless and take a lot of space in your image for absolutely nothing.
So yeah, IMO you only add the war to your docker image, you don't build from within.
I think that you should not build your docker image inside your maven process, it's two separate processes that you can automate with some higher level scripting (or jenkins pipeline)
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!!
I have a java application (jar file) that I want to be able to run from a docker image.
I have created a Dockerfile to create an image using centos as base and install java as such:
Dockerfile
FROM centos
RUN yum install -y java-1.7.0-openjdk
I ran docker build -t me/java7 after to obtain the image me/java7
however I am stuck at some dead ends.
How do I copy the jar file from the host into the image/container
I require 2 parameters. 1 is a file, which needs to be copied into a directory into the container at runtime. The other is a number which needs to be passed to the jar file in the java -jar command automatically when the user runs docker run with the parameters
Extra Notes:
The jar file is a local file. Not hosted anywhere accessible via wget or anything. The closest I have at the moment is a windows share containing it. I could also access the source from a git repository but that would involve compiling everything and installing maven and git on the image so I'd rather avoid that.
any help is much appreciated.
In the Dockerfile, add a local file using ADD, e g
ADD your-local.jar /some-container-location
You could use volumes to put a file in the container in runtime, e g
VOLUME /copy-into-this-dir
And then you run using
docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7
You can use ENTRYPOINT and CMD to pass arguments, e g
ENTRYPOINT ["java", "-jar", "/whatever/your.jar"]
CMD [""]
And then again run using
docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7 --myNumber 42
(Have a look at the Dockerfile documentation.)
Suppose your file structure is as follow :
DockerTest
└── Dockerfile
└── local.jar
Dockerfile content will be :
FROM centos
RUN yum install -y java-1.7.0-openjdk
EXPOSE 8080
ADD /local.jar fatJar.jar
ENTRYPOINT ["java","-jar","fatJar.jar"]
Use following command :
$ cd DockerTest
$ docker build -f Dockerfile -t demo .