Docker how to reduce size of specific image - java

I am totally newbie to Docker. My task is to create such environment to be able to work Spring Boot app on in and what's more I want to have also Heroku CLI installed also (I perform operations between databases on two different accounts using heroku:pg copy). What's more its size should be as small as it is possible and reach max 500MB (would be best if even less) that is quota for Heroku account
Here is my Dockerfile:
FROM ubuntu
# Install OpenJDK-8
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;
# Fix certificate issues
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Install sudo
RUN apt-get update && \
apt-get -y install sudo
# Add user and switch to it
RUN useradd -m login && echo "login:password" | chpasswd && adduser login sudo
USER login
# Install curl and use curl
RUN echo "password" | sudo -S apt --assume-yes install curl
RUN echo "password" | sudo -S curl https://cli-assets.heroku.com/install.sh -o ~/heroku.sh
# Run Heroku script
RUN echo "password" | sudo -S sh ~/heroku.sh
# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
# Add an app and listen on port
ADD target/project-server-1.0-SNAPSHOT.jar .
COPY project-91878452-firebase-adminsdk-dxv9z-0db48a2d01.json /
EXPOSE 8000
CMD java -jar project-server-1.0-SNAPSHOT.jar
This code works, I tested that locally but on Heroku there is an error on start saying that size has been considerably exceeded.
So what should I change in Dockerfile to make image size based on this Dockerfile as small as it is possibke and meet the requirements mentioned at the beginning of the post?
Kind regards!

Related

How to install java in an airflow container using docker-compose.yaml

I am using this docker-compose.yaml file to run airflow on docker container.
https://airflow.apache.org/docs/apache-airflow/2.0.2/docker-compose.yaml
I need to install JRE in one of the containers. How do I add instruction to add java to the docker-compose.yaml file?
Try the following:
Create the following Dockerfile in the directory where you also have the docker-compose.yml:
FROM apache/airflow:2.0.2
USER root
# Install OpenJDK-11
RUN apt update && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y ant && \
apt-get clean;
# Set JAVA_HOME
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/
RUN export JAVA_HOME
USER airflow
WORKDIR /app
COPY requirements.txt /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
In order to install packages via apt, you have to switch to root user. Later, we switch back to user airflow. The specification of the working directory and the installation of python packages through requirements.txt might not apply, depending on your case.
Then, in your docker-compose.yml, add build: . after &airflow-common.
Finally, build your pipeline using docker-compose up -d --build.
For more information, look here: https://airflow.apache.org/docs/docker-stack/build.html#building-the-image
You have to build your own airflow image.
In your Dockerfile you have to apt install the airflow and export JAVA_HOME.
Dockerfile example:
FROM apache/airflow:2.5.1
USER root
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
openjdk-11-jre-headless \
&& apt-get autoremove -yqq --purge \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
USER airflow
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
RUN pip install --no-cache-dir apache-airflow-providers-apache-spark==2.1.3
Build command example:
docker build -t airflow-with-java .
After successfully image building edit your docker-compose by replacing airflow image:
x-airflow-common:
&airflow-common
image: ${AIRFLOW_IMAGE_NAME:-airflow-with-java}
More information you can look here: https://airflow.apache.org/docs/docker-stack/build.html

Docker phusion/baseimage install oracle_db

Can someone help me install oracle_db client on my existing docker image. I tried so hard to get around a fix for this issue. Looks impossible to install oracle_db with phusion/baseimage.
My dockerfile is this:
FROM phusion/baseimage
MAINTAINER bugsbunny
RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe" > /etc/apt/sources.list
RUN add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe"
RUN apt-get -y update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -q python-software-properties software-properties-common
ENV JAVA_VER 8
ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
RUN echo 'deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' >> /etc/apt/sources.list && \
echo 'deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main' >> /etc/apt/sources.list && \
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C2518248EEA14886 && \
apt-get update && \
echo oracle-java${JAVA_VER}-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections && \
apt-get install -y --force-yes --no-install-recommends oracle-java${JAVA_VER}-installer oracle-java${JAVA_VER}-set-default && \
apt-get clean && \
rm -rf /var/cache/oracle-jdk${JAVA_VER}-installer
RUN update-java-alternatives -s java-8-oracle
RUN echo "export JAVA_HOME=/usr/lib/jvm/java-8-oracle" >> ~/.bashrc
RUN apt-get install nano
RUN apt-get install -y ksh
RUN echo "deb http://cz.archive.ubuntu.com/ubuntu trusty main" > /etc/apt/sources.list
RUN apt-get update
RUN cd /home/ && wget http://launchpadlibrarian.net/333072908/libaio1_0.3.110-4_amd64.deb && dpkg -i libaio1_0.3.110-4_amd64.deb
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ADD mtf-release /home/mtf-release
ADD instantclient_12_2 /opt/oracle/instantclient_12_2
RUN sh -c "echo /opt/oracle/instantclient_12_2 > /etc/ld.so.conf.d/oracle-instantclient.conf" && ldconfig
RUN export LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2:$LD_LIBRARY_PATH
RUN mkdir -p /opt/oracle/instantclient_12_2/network/admin
RUN export PATH=/opt/oracle/instantclient_12_2:$PATH
#ENTRYPOINT ["/usr/bin/python"]
As you can see, I need java:8 version and oracle_db client, sqlplus to make my docker work. Is there any proper docker image which has java + oracledb or anyway to merge two docker images into one so that I have both installed working fine.? Thank you.
Can someone help me with using two FROMs and what all images i need?
So you want a Docker image that container both oracle client and java.
Oracle provides a Docker image for the instant client and the source code for the Docker file can be found here.
For Java there are many Docker images available such as openjdk.
You can merge the two images using Docker multi-stage builds. Before that make sure to login into docker store, go to oracle instantclient image, and accept the license and pull the image docker pull store/oracle/database-instantclient:12.2.0.1
FROM store/oracle/database-instantclient:12.2.0.1 as oracle
FROM openjdk:8-jdk
COPY --from=oracle /usr/lib/oracle /usr/lib/oracle
ENV PATH=$PATH:/usr/lib/oracle/12.2/client64/bin
Once you build the above dockerfile you will have a docker image containing java and oracle instantclient.

Check if Onion URL is live

I am making an Android application in Java. There is an onion Url that I want to check is live or not. Basically, Onion Url is mapped to an IP-address, so is there any Java library or API that can assist in determining this check?
Any help in this regard will be appreciated.
Thanks.
You need to have a server on which the tor will work and already give from it either the desired status or the page that you need. For the site tor-sites.link we did the proxying of the tor through the tor launched into the container and the express.js server. You can see how it works at previewer. Schematically, it looks like this:
HTTP request
express.js server
request to tor
returning a response to the client
This way you can get data from the onino site.
Dockerfile:
FROM node:slim
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
RUN apt-get update && apt-get install gnupg wget -y && \
wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install google-chrome-stable -y --no-install-recommends && \
apt-get install tor -y --no-install-recommends && \
apt-get install torsocks -y --no-install-recommends && \
apt-get install curl gnupg && \
rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
RUN mkdir src
RUN mkdir tmp
COPY ./src ./src
COPY ./package.json \
./ecosystem.config.js \
./tsconfig.json \
./
RUN npm i -g pm2 npm#9.2.0
RUN yarn && yarn build
RUN rm -rf src
#HEALTHCHECK --start-period=150s --interval=300s --retries=99999 --timeout=120s CMD curl --fail http://localhost:3000/healthcheck || kill 1
EXPOSE 3000
CMD ["pm2-runtime", "start", "ecosystem.config.js"]

docker - installing java 8 with curl

To insall java 8 on docker container, I used this command in dockerfile
RUN curl -LO 'http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.rpm'
-H 'Cookie: oraclelicense=accept-securebackup-cookie' RUN rpm -i jdk-8u111-linux-x64.rpm RUN rm jdk-8u111-linux-x64.rpm
It worked for awhile, but now this link is not no longer available. Is there replaceable url for this? or easy way to insall java 8 on docker?
It's up to your using OS, so I'd like you to show your OS. For example, Ubuntu users prepares their ppa repository for Oracle Java.
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get update
RUN apt-get install oracle-java8-installer
It seems that Oracle has changed the authorization for downloading, the actual link that the browser uses is http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm?AuthParam=1496223138_c808095f7637d83147c37d69d3a87e7a, but this cannot be used with curl.
I have no solution for the problem with downloading from Oracle, but I use the official OpenJDK image as base and have found no problems with that.
If openjdk is OK for you, you can use
RUN apt-get install -y openjdk-8-jdk
openjdk, in contrast to oracle, does not require user's confirmation.
If you only need java runtime, consider openjdk-8-jre.
Adaption of my Dockerfile from https://hub.docker.com/r/sftech/java
FROM ubuntu
ENV JAVA_VERSION=8
RUN echo oracle-java${JAVA_VERSION}-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-get update \
&& apt-get install -y software-properties-common \
&& apt-add-repository ppa:webupd8team/java \
&& apt-get update \
&& apt-get install -y oracle-java${JAVA_VERSION}-installer \
&& update-java-alternatives -s java-${JAVA_VERSION}-oracle \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
Why would you install java in an image / container, when you can get a java:8 image from the Docker store for free. The license requirements are exactly the same as the java / jdk download install from the oracle.com website.

Create Docker container with both Java and Node.js

I am not sure why I expected this to work:
# Dockerfile
FROM node:6
FROM java:8
but it doesn't really work - looks like the first command is ignored, and second command works.
Is there a straightforward way to install both Node.js and Java in a Docker container?
Ultimately the problem I am trying to solve is that I am getting an ENOENT error when running Selenium Webdriver -
[20:38:50] W/start - Selenium Standalone server encountered an error: Error: spawn java ENOENT
And right now I assume it's because Java is not installed in the container.
The best way for you is to take java (which is officially deprecated and it suggests you use openjdk image) and install node in it.
So, start with
FROM openjdk:latest
This will use the latest openjdk image, which is 8u151 at this time. Then install node and other dependencies you might need:
RUN apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_9.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh
You might want to install things like grunt afterwards, so this might come in handy as well.
RUN npm install -g grunt grunt-cli
In total you will get the following Dockerfile:
FROM openjdk:latest
RUN apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_9.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh \
RUN npm install -g grunt grunt-cli
You may clone the Dockerfile from my gitlab repo here
You can use single FROM per generated image.
Try to use node as a base image and install java to it.
Dockerfile
FROM node:latest
RUN apt-get -y install default-jre
You can choose the version you need:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
You can also use the node image and then install the default-jre:
# Dockerfile
FROM node:latest
RUN apt-get -y install default-jre
You can choose the version you need:
apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
Perhaps, you might want to try this https://hub.docker.com/r/timbru31/java-node one to create the docker file.
This docker image comes with both Java and Node pre-installed. It comes in handy when both are required as a dependency.
Something like,
FROM timbru31/java-node:<tag>
The FROM inside your dockerfile simply tells docker from which image it should start the configuration. You can't simply concatenate multiple images together. There are already multiple container images available which offer preinstalled Java 8 and node JS. I don't want to recommend any image specifically but will direct you to docker-hub for you to go search on your own and use the container that suites your needs the best.
With version 14 of node it works perfectly for me:
FROM openjdk:latest
RUN apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_14.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh \
RUN npm install -g grunt grunt-cli
this worked for me:
FROM openjdk:16-slim-buster
RUN apt-get update; apt-get install -y curl \
&& curl -sL https://deb.nodesource.com/setup_14.x | bash - \
&& apt-get install -y nodejs \
&& curl -L https://www.npmjs.com/install.sh | sh
This works for me with node v16.15.0, be careful with the packages version of java, I'm using the latest by default. The packages used for java are:
Java Runtime Environment (jre)
Java Development Kit (jdk)
FROM node:16.15.0
RUN apt-get update \
&& apt-get install default-jre -y \
&& apt-get install default-jdk -y
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm install --global nodemon
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
I hope this works for you all

Categories

Resources