Dockerfile:如何使用 curl 下载文件并将其复制到容器中
Dockerfile: how to Download a file using curl and copy into the container
在这个例子中,我将 wait-for-it.sh 复制到 /app/wait-for-it.sh
但是,我不想在我的本地目录中保存 wait-for-it.sh 。我想使用 curl 下载它,然后复制到 /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"]
我试过的是这个,但是如何在使用curl命令下载文件后得到wait-for-it.sh
:
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"]
根据您在下面的评论,您可以试试这个:
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"]
注意:您需要使用 cp
命令,因为您想要将脚本从一个位置复制到另一个 容器文件系统中的另一个位置。
您还可以通过 运行 命令确认 /app
文件夹中存在您的脚本和其他 files/dirs:
$ 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
不相关但在构建期间处理下载的更简单方法是使用 Docker 的 ADD
指令而不使用 curl 或 wget。
RUN 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.
在这个例子中,我将 wait-for-it.sh 复制到 /app/wait-for-it.sh 但是,我不想在我的本地目录中保存 wait-for-it.sh 。我想使用 curl 下载它,然后复制到 /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"]
我试过的是这个,但是如何在使用curl命令下载文件后得到wait-for-it.sh
:
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"]
根据您在下面的评论,您可以试试这个:
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"]
注意:您需要使用 cp
命令,因为您想要将脚本从一个位置复制到另一个 容器文件系统中的另一个位置。
您还可以通过 运行 命令确认 /app
文件夹中存在您的脚本和其他 files/dirs:
$ 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
不相关但在构建期间处理下载的更简单方法是使用 Docker 的 ADD
指令而不使用 curl 或 wget。
RUN 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.