How to install package manager in java based docker image - java

I am developing an application using SpringBoot and Java with Docker.
I want to install Netcat in my application so that I can check if a specific container is up before my spring application starts.
I have tried
RUN apt-get update && apt-get -y install netcat && apt-get clean
But it says apt-get is not found.
I am not sure how can I install any package manager in my java based application.
Or is there any other I can get net-cat installed.
Below is my Dockerfile
FROM openjdk:8
RUN apt-get update && apt-get -y install netcat && apt-get clean
ADD target/student.jar student.jar
EXPOSE 2018
ENTRYPOINT ["java","-jar","student.jar"]

apt-get is package manager for Debian-based Linux distros (including Ubuntu and others).
Try apk add instead.
Also, if you are using package managers in your Dockerfile, it is considered a good practice to explicitly specify Linux distro. In your case you can use openjdk:8-jdk-stretch variant or any other, where distro is specified.

I can get a successful docker build with this Dockerfile
FROM openjdk:8
RUN apt-get update && apt-get -y install netcat && apt-get clean
ENTRYPOINT ["/bin/bash"]
and apt-get works just fine. As far as I can tell openjdk:8 is Ubuntu (or at least Debian based). Even if I run
docker build . -t stackexample
docker run -it stackexample
and get inside the container, running netcat opens the netcat cmd.

Related

How to install java for tabula inside docker container

I cant find anything related to my question
I tried below docker file
RUN apt-get update && apt-get install -y \
software-properties-common
RUN apt-get update && \
apt-cache search openjdk && \
apt-get install openjdk-8-jdk && \
apt-get clean;
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
#tabula.errors.JavaNotFoundError: `java` command is not found from this Python
#process.Please ensure Java is installed and PATH is set for `java`
when i use import tabula im getting tabula.errors.JavaNotFoundError. can someone please help what to do to get rid of this error in docker ?
UPDATE:
Im using flask and mongodb. in flask there is a code responsible to read pdf files which is tabula and it needs Java as it says in its error. for other python package i installed with pipfile and pipfile.lock
RUN pip install pipenv
COPY Pipfile . #<---- contains tabula package
COPY Pipfile.lock . #<---- contains tabula package
RUN PIPENV_VENV_IN_PROJECT=1 pipenv install --deploy
##But i have no idea how to install java for tabula dependecy.
**************** FINAL UPDATE *****************
I replaced tabula with pdfplumber. now working good, thanks for all who tried to help me.
Generally one should refrain from using a container image with more than one main process, such as python and java, and I would personally advise finding a replacement to tabula-py that doesn't require a java enviroment for that is the best practice when using containers as specified here as so:
It is generally recommended that you separate areas of concern by using one service per container.
With that in mind, because I don't know if you can do those things I'm gonna provide an alternative as well.
this docker image packs multiple runnable environments into one such as java and python, and its dockerfile is listed here.
Because it encompasses more environments than you need you can slim it down to your needs.
there is also this project though it wasn't updated for awhile or this article describing a consise homebrewed python and java dockerfile
In case anyone is trying to achieve that and doesn't want to switch to another library, here is a way of making it work with Tabula.
By the way, here I'm using Tabula in jupyter notebook... but you just have to change the image from Jupyter to python to achieve what you want with Flask.
Your docker-compose will be like this:
jupyter:
container_name: jupyter_lab
build: .
ports:
- "8888:8888"
environment:
- JUPYTER_ENABLE_LAB=yes
volumes:
- ./work:/home/jovyan/work
This is your Dockerfile:
# Image base-notebook
FROM jupyter/minimal-notebook #getting the basic one.
# Change to root user to install java 8
USER root
# Install java 8
RUN apt-get update \
&& echo "Updated apt-get" \
&& apt-get install -y openjdk-8-jre \
&& echo "Installed openjdk 8"
# Install requirements
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
RUN rm -rf requirements.txt
# Change to "$NB_USER" command so the image runs as a non root user by default
USER $NB_UID
Your requirements.txt
tabula.py
Create the folder "work" (that is the folder that is going to be synced with the container).
Now open the terminal and type docker-compose up --build
This command will build and start your container. With these steps, you should be ready to go.
Test with this line of code in the notebook:
from tabula import read_pdf
pdf = read_pdf("path_to_your_pdf.pdf", pages='all')

Installing rJava package with Docker

I am trying to install the 'rJava' package in my RStudio docker image using my Dockerfile:
FROM rocker/tidyverse:3.6.1
RUN mkdir -p /rstudio
RUN mkdir -p /rscripts
RUN apt-get update && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y liblzma-dev && \
apt-get install -y libbz2-dev
RUN R -e "install.packages(c('rJava','mailR'))"
Following this SO post I added the above part with the apt-get commands but still I get the same error:
java libs :
'-L/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server -ljvm'
checking whether Java run-time works... ./configure: line 3766:
/usr/bin/java: No such file or directory no configure: error: Java
interpreter '/usr/bin/java' does not work ERROR: configuration failed
for package ‘rJava’
So there is a missing file or directory but I don't know what changes I should make.
[EDIT 1]:
So, following Dirk's suggestion, I entered in the rstudio container and ran apt-get install r-cran-rjava which seems to work.
But when I install rJava package I get a new error:
error: Cannot compile a simple JNI program. Make sure you have Java
Development Kit installed and correctly registered in R. If in doubt,
re-run "R CMD javareconf" as root.
I tried to enter again in the container and run R CMD javareconf but that did not change the error. I also tried the following commands found on this article:
sudo apt-get install default-jre
sudo apt-get install default-jdk
But I still get:
Cannot compile a simple JNI program.
I found a github repo suggesting to add these steps in the Dockerfile before installing R package rJava and it worked:
RUN apt-get -y update && apt-get install -y \
default-jdk \
r-cran-rjava \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/
This is a duplicate of similar answers I have already given in the Debian / Ubuntu context. The fact that you are using Docker does not matter: you should still simply install the binary!
edd#rob:~$ docker run --rm -ti r-base bash
root#ef4bb9726a21:/# apt update -qq
73 packages can be upgraded. Run 'apt list --upgradable' to see them.
root#ef4bb9726a21:/# apt install --no-install-recommends -y r-cran-rjava
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ca-certificates-java default-jre default-jre-headless java-common libasound2 libasound2-data libavahi-client3 libavahi-common-data libavahi-common3 libcups2 libdbus-1-3 libdrm-amdgpu1 libdrm-common
libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libdrm2 libedit2 libgif7 libgl1 libgl1-mesa-dri libglapi-mesa libglvnd0 libglx-mesa0 libglx0 liblcms2-2 libllvm10 libnspr4 libnss3 libpciaccess0 libpcsclite1
libsensors-config libsensors5 libsqlite3-0 libvulkan1 libx11-xcb1 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-sync1 libxdamage1 libxfixes3 libxi6 libxshmfence1 libxtst6 libxxf86vm1 libz3-4
openjdk-11-jre openjdk-11-jre-headless
[.... many lines skipped ....]
Running hooks in /etc/ca-certificates/update.d...
done.
done.
root#ef4bb9726a21:/# R
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
[...some lines skipped...]
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(rJava)
>
The reason why Dirk's answer doesn't work for you is because you're using rocker/tidyverse as a base image instead of the r-base that Dirk is using.
In the rocker/tidyverse documentation on Docker Hub they are discouraging the use of apt install --no-install-recommends -y r-cran-rjava:
do not use apt-get install r-cran-* to install R packages on this stack. The requested R version and all R packages are installed from source in the version-stable stack. Installing R packages from apt (e.g. the r-cran-* packages) will install the version of R and versions of the packages that were built for the stable debian release (e.g. debian:stretch), giving you a second version of R and different packages. Please install R packages from source using the install.packages() R function (or the install2.r script), and use apt only to install necessary system libraries (e.g. libxml2). If you would prefer to install only the latest verions of packages from pre-built binaries using apt-get, consider using the r-base stack instead.
Since rJava is supposed to be installed with apt install r-cran-rjava, but the rocker/... base images explicitly tell you not to do that, it looks like you will not be able to use rJava with any of the rocker/... base images..

How to run kurento java web server on docker

I want to run this kurento example: https://doc-kurento.readthedocs.io/en/stable/tutorials/java/tutorial-one2one.html
But I have some issues with application server when I building it on docker.
When I run kms docker - it run's successfully. I setted port of kurento-media-server as 8889:8888 and IP in docker container when I checked was 172.17.0.2. Also, I setted port of web app as 8081:8080.
When i tried to open a web-page - it was not responded.
My url was: https://192.168.0.2:8443, where 192.168.0.2 is IP of my server where I run docker. Also I tried to connect to docker container IP directly with https://172.17.0.3:8443.
Here's my app dockerfile.
FROM ubuntu:16.04
MAINTAINER USER1 "USER1#infinte.com"
RUN apt-get update
RUN apt-get install git -y
RUN apt-get install curl -y
RUN apt install apt-utils -y
RUN apt install maven -y
RUN apt install openjdk-8-jdk openjdk-8-jre -y
RUN apt-get install software-properties-common -y
RUN git clone https://github.com/Kurento/kurento-tutorial-java.git
WORKDIR kurento-tutorial-java/kurento-one2one-call-advanced/
EXPOSE 8080
ENTRYPOINT mvn -U clean spring-boot:run -Dkms.url=ws://172.17.0.2:8888/kurento
Here's my kms run command:
docker run -t --name kms -p 8889:8888 kurento/kurento-media-server
Here's my app server run command:
docker run -d --name apps -p 8081:8080 --link kms apps
What mistakes I have made here? May'be I need to change IP in ENTRYPOINT?
I spent many weeks to find out the reason why it's not working.
To make everything work, I needed to add --network=host line in docker command.
Correct docker command is
docker run --network=host -t --name apps -p 8081:8080 apps

How do I put Java onto a Ubuntu docker container?

I currently have a docker container with an Ubuntu(17.10) image installed with other packages included. However, I'm currently having difficulty trying to install Java onto this container in addition to the current image.
Current Dockerfile :
FROM cityofzion/neo-privatenet
ADD files/ files/
ENTRYPOINT [ "/bin/bash" ]
When trying to find information on how to do this and testing inside of the container most suggest using this command: apt-get install -y oracle-java9-installer
However this results in:E: Unable to locate package oracle-java9-installer
I have also tried this suggested command wget http://download.java.net/java/GA/jdk9/9/binaries/jdk-9+181_linux-x64_bin.tar.gz
Which produces this result HTTP request sent, awaiting response...
404 Not Found - ERROR 404: Not Found.
I have only tried running these commands in the container, since that is how they would be run and they seem to be failing.
Can anyone suggest what I can include into my Dockerfile that install java onto my image?
Thanks in advance.
You can also directly pull any of the open-jdk images mentioned at (https://hub.docker.com/_/openjdk/) and use it. There is no need to install Ubuntu in docker image and then install Java on top of it. These images already use Ubuntu (with bare-minimum file system).
add to to your docker file
RUN \
apt-get update && \
apt-get install -y software-properties-common && \
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | debconf-set-selections && \
add-apt-repository -y ppa:webupd8team/java && \
apt-get update && \
apt-get install -y oracle-java9-installer && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /var/cache/oracle-jdk8-installer
ENV JAVA_HOME /usr/lib/jvm/java-9-oracle
Following from here
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
Oracle JDK version 7
sudo apt-get install oracle-java7-installer
Oracle JDK version 8
sudo apt-get install oracle-java8-installer

Install MySQL connector/J in Docker container

I'm deploying an application to a pre-configured GlassFish Docker container on Elastic Beanstalk. I need to install the MySQL connector/J library so it's accessible to GlassFish. According to the AWS documentation, "With preconfigured Docker platforms you cannot use a configuration file to customize and configure the software that your application depends on." But it does state: "you can add a Dockerfile to your application's root folder."
They provide an example (with no explanation!) on installing a PostgreSQL library for Python but I'm using Java and MySQL. As I am completely unfamiliar with Docker (and really just need to configure this one thing), I can't figure out how to do it. Using Amazon's example Dockerfile I was able to come up with this:
FROM amazon/aws-eb-glassfish:4.1-jdk8-onbuild-3.5.1
# Exposes port 8080
EXPOSE 8080 3306
# Install MySQL dependencies
RUN curl -L -o /tmp/mysql-connector-java-5.1.34.jar https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar && \
apt-get update && \
apt-get install -y /tmp/mysql-connector-java-5.1.34.jar libpq-dev && \
rm -rf /var/lib/apt/lists/*
However, on application deploy, I'm getting the following error:
t update && apt-get install -y /tmp/mysql-connector-java-5.1.34.jar libpq-dev && rm -rf /var/lib/apt/lists/*] returned a non-zero code: 100"
And obviously, my library is not installed. How can I install the MySQL Connector/J library with a Dockerfile?
apt-get install -y /tmp/mysql-connector-java-5.1.34.jar
This is not how apt-get works. It installs debian packages.
There is a package called libmysql-java that may be what you want.
You might also try something like
RUN curl -L -o /mysql-connector-java-5.1.34.jar https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar
ENV CLASSPATH=/mysql-connector-java-5.1.34.jar:${CLASSPATH}

Categories