简体   繁体   English

无法在我的 Docker 容器中运行 Curl 命令

[英]Can't run Curl command inside my Docker Container

I created a docker container from my OS X VM Docker host.我从我的 OS X VM Docker 主机创建了一个 docker 容器。 I created it using the run command and created the container based off the ubuntu:xenial image off docker hub.我使用 run 命令创建了它,并基于 docker 集线器的ubuntu:xenial图像创建了容器。

I'm now connected to my container after it's created and logged in as root and at the command prompt inside my container.在我的容器创建并以 root 身份登录并在我的容器内的命令提示符下,我现在连接到我的容器。

I tried to install homebrew and for some reason, I can't run the command to install Homebrew:我尝试安装 Homebrew,但出于某种原因,我无法运行命令来安装 Homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

when I run that I get a bash:当我运行时,我得到一个 bash:

curl: command not found curl:找不到命令

Not sure why I'm not able to use curl here inside my container.不知道为什么我不能在我的容器中使用 curl。

curl: command not found curl:找不到命令

is a big hint, you have to install it with :是一个很大的提示,您必须使用以下命令安装它:

apt-get update; apt-get install curl

Ran into this same issue while using the CURL command inside my Dockerfile.在我的 Dockerfile 中使用 CURL 命令时遇到了同样的问题。 As Gilles pointed out, we have to install curl first.正如 Gilles 所指出的,我们必须先安装 curl。 These are the commands to be added in the 'Dockerfile'.这些是要添加到“Doc​​kerfile”中的命令。

FROM ubuntu:16.04

# Install prerequisites
RUN apt-get update && apt-get install -y \
curl
CMD /bin/bash

So I added curl AFTER my docker container was running.所以我在我的 docker 容器运行后添加了 curl。

(This was for debugging the container...I did not need a permanent addition) (这是为了调试容器......我不需要永久添加)

I ran my image我跑了我的形象

docker run -d -p 8899:8080 my-image:latest

(the above makes my "app" available on my machine on port 8899) (not important to this question) (以上使我的“应用程序”在我的机器上的端口 8899 上可用)(对这个问题不重要)

Then I listed and created terminal into the running container.然后我在正在运行的容器中列出并创建终端。

docker ps
docker exec -it my-container-id-here /bin/sh

If the exec command above does not work, check this SOF article:如果上面的exec命令不起作用,请查看这篇 SOF 文章:

Error: Cannot Start Container: stat /bin/sh: no such file or directory" 错误:无法启动容器:stat /bin/sh:没有这样的文件或目录”

then I ran:然后我跑了:

apk

just to prove it existed in the running container, then i ran:只是为了证明它存在于正在运行的容器中,然后我跑了:

apk add curl

and got the below:并得到以下内容:

apk add curl apk 添加卷曲

fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz获取http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz

fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz获取http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz

(1/5) Installing ca-certificates (20171114-r3) (1/5) 安装 ca 证书 (20171114-r3)

(2/5) Installing nghttp2-libs (1.32.0-r0) (2/5) 安装 nghttp2-libs (1.32.0-r0)

(3/5) Installing libssh2 (1.8.0-r3) (3/5) 安装 libssh2 (1.8.0-r3)

(4/5) Installing libcurl (7.61.1-r1) (4/5) 安装 libcurl (7.61.1-r1)

(5/5) Installing curl (7.61.1-r1) (5/5) 安装 curl (7.61.1-r1)

Executing busybox-1.28.4-r2.trigger执行busybox-1.28.4-r2.trigger

Executing ca-certificates-20171114-r3.trigger执行 ca-certificates-20171114-r3.trigger

OK: 18 MiB in 35 packages OK:35 个包中的 18 MiB

then i ran curl:然后我跑了卷曲:

/ # curl
curl: try 'curl --help' or 'curl --manual' for more information
/ # 

Note, to get "out" of the drilled-in-terminal-window, I had to open a new terminal window and stop the running container:请注意,要“退出”终端窗口,我必须打开一个新的终端窗口并停止正在运行的容器:

docker ps
docker stop my-container-id-here

APPEND:附加:

If you don't have "apk" (which depends on which base image you are using), then try to use "another" installer.如果您没有“apk”(这取决于您使用的基础映像),请尝试使用“另一个”安装程序。 From other answers here, you can try:从这里的其他答案,您可以尝试:

apt-get -qq update

apt-get -qq -y install curl

This is happening because there is no package cache in the image, you need to run:发生这种情况是因为图像中没有包缓存,您需要运行:

apt-get -qq update

before installing packages, and if your command is in a Dockerfile, you'll then need:在安装软件包之前,如果您的命令在 Dockerfile 中,那么您将需要:

apt-get -qq -y install curl

After that install ZSH and GIT Core:之后安装 ZSH 和 GIT Core:

apt-get install zsh
apt-get install git-core

Getting zsh to work in ubuntu is weird since sh does not understand the source command.zsh在 ubuntu 中工作很奇怪,因为sh不理解 source 命令。 So, you do this to install zsh :所以,你这样做是为了安装zsh

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh

and then you change your shell to zsh:然后将 shell 更改为 zsh:

chsh -s `which zsh`

and then restart:然后重新启动:

sudo shutdown -r 0

This problem is explained in depth in this issue .这个问题在本期有深入的解释。

If you are using an Alpine based image, you have to如果您使用的是基于Alpine的图像,则必须

RUN
... \
apk add --no-cache curl \
curl ...
...

You don't need to install curl to download the file into Docker container, use ADD command , eg您不需要安装curl将文件下载到 Docker 容器中,使用ADD命令,例如

ADD https://raw.githubusercontent.com/Homebrew/install/master/install /tmp
RUN ruby -e /tmp/install

Note: Add above lines to your Dockerfile file.注意:将以上Dockerfile行添加到您的Dockerfile文件中。


Another example which installs Azure CLI :安装Azure CLI 的另一个示例:

ADD https://aka.ms/InstallAzureCLIDeb /tmp
RUN bash /tmp/InstallAzureCLIDeb

what worked successfully for me was:对我来说成功的是:

FROM ubuntu:20.0.4

RUN apt-get update &&\
   apt-get install -y curl
...

If you don't need many other commands, you could just begin your Dockerfile like this:如果您不需要许多其他命令,您可以像这样开始您的 Dockerfile:

FROM alpine/curl

You could base your image on one that already has curl .您可以将图像建立在已经具有curl的图像上。

A minimalistic one:一个简约的:

FROM alpine/curl

or a full-fledged one:或成熟的:

FROM rockylinux:9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker for mac - 无法卷曲我的容器 - Docker for mac - can't curl my container 不能在之前安装过的 docker 容器中使用 curl - Can't use curl inside a docker container that has installed it before 为什么我不能在我的 Jenkins Docker 容器中运行任何 docker 命令? - Why I can't run any docker commands inside my Jenkins Docker container? 为什么我不能在 Dockerfile 中运行命令,但我可以在我的 Docker 容器中运行命令? - Why can't I run command in Dockerfile but I can from within the my Docker container? Docker:运行echo命令在我的窗口容器上不起作用 - Docker: Run echo command don't work on my window container 詹金斯:无法在Docker容器中运行命令 - Jenkins: Can't run commands inside Docker container 使用 Ansible 在 Docker 容器内运行命令 - Run Command Inside of Docker Container Using Ansible 如何在 Docker 容器内运行命令 - How to run command inside Docker container 无法在我的VPS(virtuozzo 7容器)中运行Docker - Can't run Docker in my VPS ( virtuozzo 7 container ) 在 docker 容器内运行的主机命令无法解析容器主机名,但 ping 可以 - host command run inside docker container doesn't resolve container hostname, but ping does
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM