我正在尝试为我的Python Flask API创建一个Docker镜像。
我需要使用git安装依赖项,而且我已经在Docker中多次安装了git。但是在这里,我不明白我做错了什么。
使用Docker:
FROM python:3.6-slim
ARG ssh_prv_key
ARG ssh_pub_key
RUN apt-get update && \
apt-get install -y openssh-server &&\
apt-get install -y git
# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
chmod 0700 /root/.ssh && \
ssh-keyscan github.com > /root/.ssh/known_hosts
# Add the keys and set permissions
RUN echo "$ssh_prv_key" > /root/.ssh/id_rsa && \
echo "$ssh_pub_key" > /root/.ssh/id_rsa.pub && \
chmod 600 /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa.pub && \
echo "StrictHostKeyChecking no " > /root/.ssh/config
RUN eval "$(ssh-agent -s)"
RUN ssh-add /root/.ssh/id_rsa
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt
# Remove SSH keys
RUN rm -rf /root/.ssh/
COPY ./my_api /usr/src/app
# Expose the Flask port
EXPOSE 5000
CMD [ "python", "./app.py" ]
我执行了以下命令:
docker build --build-arg ssh_prv_key=.keys/id_rsa --build-arg ssh_pub_key=.keys/id_rsa.pub -t my-api -f Dockerfile .
这导致我出现以下错误:
Step 7/16 : RUN eval "$(ssh-agent -s)"
---> Running in be450cc39533
Agent pid 9
Removing intermediate container be450cc39533
---> fb101226dc5f
Step 8/16 : RUN ssh-add /root/.ssh/id_rsa
---> Running in 4288e93db584
Could not open a connection to your authentication agent.
The command '/bin/sh -c ssh-add /root/.ssh/id_rsa' returned a non-zero code: 2
一个PID是通过eval函数从ssh-agent中检索出来的,但我无法连接到它。
已解决
最后我找到了问题所在。首先,我的构建参数不正确。正确的docker构建命令如下:
docker build --build-arg ssh_prv_key="$(cat .keys/id_rsa)" --build-arg ssh_pub_key="$(cat .keys/id_rsa.pub)" -t my-api -f Dockerfile .
此外,我不知道为什么,git在没有使用ssh keys的情况下正确处理它。
RUN eval "$(ssh-agent -s)"
RUN ssh-add /root/.ssh/id_rsa
执行上述命令会出现"无法连接到您的代理"错误。
正确的文件是
FROM python:3.6-slim
ARG ssh_prv_key
ARG ssh_pub_key
RUN apt-get update && \
apt-get install -y git
# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
chmod 0700 /root/.ssh && \
ssh-keyscan github.com > /root/.ssh/known_hosts
# Add the keys and set permissions
RUN echo "$ssh_prv_key" > /root/.ssh/id_rsa && \
echo "$ssh_pub_key" > /root/.ssh/id_rsa.pub && \
chmod 600 /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa.pub
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt /usr/src/app/
RUN pip3 install --no-cache-dir -r requirements.txt
# Remove SSH keys
RUN rm -rf /root/.ssh/
COPY ./my_api /usr/src/app
# Expose the Flask port
EXPOSE 5000
CMD [ "python", "./app.py" ]
RUN systemctl restart sshd
命令时,我遇到了以下错误:无法连接到总线:没有这样的文件或目录。 - Laurent GRENIER