78

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"]
Markus
  • 2,071
  • 4
  • 22
  • 44
user3356164
  • 783
  • 1
  • 5
  • 6

3 Answers3

163

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.

Community
  • 1
  • 1
Imran
  • 5,542
  • 3
  • 23
  • 46
11

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.

Markus
  • 2,071
  • 4
  • 22
  • 44
  • 2
    Markus you are not right. With docker 17.05 there is a major change. Please adjust your answer. [Ref](https://docs.docker.com/develop/develop-images/multistage-build/) – trust512 Jul 05 '18 at 13:43
  • Thank you for the advice. I was not aware of that feature. :) – Markus Jul 05 '18 at 13:48
  • 2
    Sure, no problem, and the answer is correct now - `curl` will not be available as its wiped out. – trust512 Jul 05 '18 at 13:50
  • Curl is not unavailable because it has been "wiped out", curl just is not part of Alpine by default. – Captain Man Jun 21 '19 at 19:10
  • @CaptainMan: But it is going to be installed in the Dockerfile and will be wiped after the second FROM statement. – Markus Jun 22 '19 at 19:04
  • @Markus The build's first stage using Ubuntu should still produce an image that has curl installed though. – Captain Man Jun 24 '19 at 17:55
  • Yes, but it is wiped after the second FROM switching to Alpine. See [reference link](https://docs.docker.com/develop/develop-images/multistage-build/) in first comment. – Markus Jun 24 '19 at 21:11
  • Unfortunately the laptop I am on right now can't run Docker (Windows 7) so I cannot test, but I could've sworn running `docker images` would show the intermediate stages as unnamed images. Maybe this is just an issue of word usage though. When I see "wiped out" I think deleted, but as I'm saying I don't think it is. That said, the *state* of the build is wiped, but it seems like a moot point to mention it when talking about installed packages as opposed to things like `WORKDIR` or `ADD` -- especially since Ubuntu and Alpine don't use the same package manager so the commands would be different. – Captain Man Jun 26 '19 at 15:39
-8

Most languages have readily available HTTP clients these days; you should almost never be calling out to curl from a program in a language more sophisticated than a shell script. java.net.URLConnection has been a part of Java since Java 1.0 and (without knowing why you're trying to shell out for this) it's almost definitely the right tool here.

Assuming you control the executeCoreAPI method from your backtrace, you should change it to use the built-in Java HTTP client, and just delete all of the Dockerfile parts that try to install curl.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • 1
    Sometimes you don't want to build a product to see what response the webserver is returning. The question is about installing curl, this answer isn't one. – boatcoder Feb 20 '20 at 18:16
  • 1
    i.e. curl is usefull to implement a quick healthcheck for swarm. – Xavier D Jun 30 '20 at 22:07