9

in this example, I copy wait-for-it.sh inside /app/wait-for-it.sh But, I don't want to save wait-for-it.sh in my local directory. I want to download it using curl and then copy into /app/wait-for-it.sh

FROM prismagraphql/prisma:1.34.8
COPY ./wait-for-it.sh /app/wait-for-it.sh
RUN chmod +x /app/wait-for-it.sh
ENTRYPOINT ["/bin/sh","-c","/app/wait-for-it.sh mysql:3306 --timeout=0 -- /app/start.sh"]

What I have tried is this, but how can I get the wait-for-it.sh after downloading the file using curl command:

FROM prismagraphql/prisma:1.34.8

FROM node:11-slim

RUN apt-get update && apt-get install -yq build-essential dumb-init

RUN curl -LJO https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh

COPY wait-for-it.sh /app/wait-for-it.sh

RUN chmod +x /wait-for-it.sh

ENTRYPOINT ["/bin/sh","-c","/wait-for-it.sh mysql:3306 --timeout=0 -- /app/start.sh"]
Ashik
  • 2,888
  • 8
  • 28
  • 53

3 Answers3

12

Based on your comments below, you may try this one:

FROM prismagraphql/prisma:1.34.8

RUN apk update && apk add build-base dumb-init curl

RUN curl -LJO https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh

RUN cp wait-for-it.sh /app/

RUN chmod +x /wait-for-it.sh

ENTRYPOINT ["/bin/sh","-c","/wait-for-it.sh mysql:3306 --timeout=0 -- /app/start.sh"]

Note: You need to use cp command as you want to copy the script from one location to another within your container's filesystem.

You can also confirm the presence of your script and other files/dirs in the /app folder by running the command:

$ docker run --rm --entrypoint ls waitforit -l /app/
total 36
drwxr-xr-x    1 root     root          4096 Aug 29  2019 bin
drwxr-xr-x    2 root     root         16384 Aug 29  2019 lib
-rwxr-xr-x    1 root     root           462 Aug 29  2019 prerun_hook.sh
-rwxr-xr-x    1 root     root            61 Aug 29  2019 start.sh
-rw-r--r--    1 root     root          5224 Apr 22 13:46 wait-for-it.sh
Technext
  • 7,887
  • 9
  • 48
  • 76
  • after running your solution, I am getting this /wait-for-it.sh: line 179: /app/start.sh: No such file or directory – Ashik Apr 22 '20 at 12:28
  • @UnbearableLightness: `-O` (capital) automatically creates file with the name that's present in the URL after stripping the path. Had i been using `-o`, i would have specified the file name as well. – Technext Apr 22 '20 at 12:37
  • @Ashik: Are you sure you're using the above Dockerfile content while building the image? I'm able to see the files in both the `src` and `destination`. Check my updated answer. Nothing has been changed in the Dockerfile code i posted. Just try the command i've added and kindly paste the result. – Technext Apr 22 '20 at 12:42
  • Also, if i run `docker run --rm -it waitforit`, i get output: `wait-for-it.sh: waiting for mysql:3306 without a timeout` – Technext Apr 22 '20 at 12:43
  • I think problem is in the entrypoint line. It didn't find /app/start.sh but, you can see my first snippet I didn't create any folder called `app` using RUN command. I copied the `wait-for-it.sh` inside `/app/wait-for-it.sh` and it worked. but now I want to get the ./wait-for-it.sh by running curl command (I don't want to save wait-for-it.sh locally). – Ashik Apr 22 '20 at 12:57
  • maybe I find the problem. I am using two images here, one is node and one is prisma-graphql. so, prisma containers /app directory is messed up with node containers app directory. but, I want to use Curl. thats why I have to import from node image. is there any other way to get curl without using node image? – Ashik Apr 22 '20 at 13:09
  • 2
    With `-o`, you can put the downloaded file where you want it, no need to move it after fetching it. If you use `--create-dirs` it will create any missing directories in the path you specify. – Richard Whitehead Jul 28 '21 at 15:41
  • 1
    I suggest to use `curl -LJOf` (adding option `-f` or `--fail`) to make `curl` fail the build if the file could not be downloaded. Makes it much easier to track down problems. – not2savvy Mar 10 '22 at 11:59
5

Not related but an easier way to handle downloads during build time is to use Docker's ADD directive without curl or wget.

ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /tmp
COPY /tmp/wait-for-it.sh /app/wait-for-it.sh

Right now, we recommend using Docker's ADD directive instead of running wget or curl in a RUN directive - Docker is able to handle the https URL when you use ADD, whereas your base image might not be able to use https, or might not even have wget or curl installed at all.

https://github.com/just-containers/s6-overlay#usage

John Murphy
  • 57
  • 2
  • 9
parse
  • 1,706
  • 17
  • 27
0

Just download the file via wget or curl and copy the file in a directory that it's not temporary. If you want to create a folder for this specific file, please create it before to download it.

If you download the file and create the directory in the same moment Docker doesn't recognize the directory as permanent , so it'll delete it after run this command.