Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make curl available in Docker image based java:8-jdk-alpine and keep the image clean?

We are having java code that runs curl command to fetch the some result.

We have built a jar file and the jar file executes fine

Now, when we try to dokerize the java program (using jar) and run the application in docker we get this error:

errorjava.io.IOException: Cannot run program "curl": error=2, No such file or directory     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)     at com.ps.api.common.CoreAPI_Spec.executeCoreAPI(CoreAPI_Spec.java:295)     at com.ps.api.common.CoreAPI_Spec.getAccessTokens(CoreAPI_Spec.java:319) 

Dockerfile used :

FROM ubuntu:16.04 MAINTAINER niro;  # Install prerequisites RUN apt-get update && apt-get install -y \ curl  FROM java:8-jdk-alpine # Set the working directory to /app WORKDIR /Users/******/Desktop/CoreAPI_Jar  # Copy the current directory contents into the container at /app ADD *******_Automation-0.0.1-SNAPSHOT-jar-with-dependencies.jar ******_Automation-0.0.1-SNAPSHOT-jar-with-dependencies.jar  # Run app.py when the container launches CMD ["java", "-jar", "******-0.0.1-SNAPSHOT-jar-with-dependencies.jar"] 
like image 570
user3356164 Avatar asked Jul 05 '18 13:07

user3356164


People also ask

Does Docker alpine have curl?

Docker Alpine Curl. Like it says, it's a docker image built on alpine with curl installed. Size 5.93 MB.

Does alpine image have curl?

A minimal Docker image based on Alpine Linux has only 5 MB in size, but a lot of tools common for Linux distributions (e.g. curl ) are not installed by default. In this short note i will show how to install curl in Alpine container from the command line.

Does alpine have curl by default?

Curl is not unavailable because it has been "wiped out", curl just is not part of Alpine by default. @CaptainMan: But it is going to be installed in the Dockerfile and will be wiped after the second FROM statement.


2 Answers

The Java base image you are using is Alpine Linux one and curl package also needs to be downloaded from there. Here is Dockerfile I have used for Production deployments.

FROM openjdk:8-jre-alpine  RUN apk add --update \     curl \     && rm -rf /var/cache/apk/* 

Update 05/2019

As of Alpine Linux 3.3 there exists a new --no-cache option for apk. It allows users to install packages with an index that is updated and used on-the-fly and not cached locally:

FROM openjdk:8-jre-alpine      RUN apk --no-cache add curl 

This avoids the need to use --update and remove /var/cache/apk/* when done installing packages.

Reference - https://github.com/gliderlabs/docker-alpine/blob/master/docs/usage.md and Thank you @Daniel for the comment.

like image 100
Imran Avatar answered Sep 19 '22 15:09

Imran


Your example dockerfile contains multiple FROM statements. This is valid but as the documentation says each FROM clears the state from previous instructions. And so the fresh installed curl is wiped after the second FROM.

like image 36
Markus Avatar answered Sep 17 '22 15:09

Markus