首页 > 解决方案 > 在 Dockerfile 中的 Docker 镜像中复制 gitlab 项目

问题描述

我正在尝试将 gitlab 项目复制到我的 docker 映像中,以便可以在我的 python 脚本中使用它。我的 DockerFile 一直运行到这一行:

RUN \
### ssh configuration, see https://docs.gitlab.com/ee/ci/ssh_keys/#ssh-keys-when-using-the-docker-executor
# run ssh-agent inside build environment
eval $(ssh-agent -s); \
# add ssh-key from GitLab variable to ssh agent store incl. fixing line endings
echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add -; \
# create the SSH directory and give it the right permissions
mkdir -p ~/.ssh; \
# Allow access to gitlab.your.lan
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config; \
chmod 700 ~/.ssh; \
\
git clone ${BACKEND_PATH}

错误代码:

+ echo 
+ ssh-add -+ 
tr -d \r
Could not open a connection to your authentication agent.
+ mkdir -p /root/.ssh
+ echo -e Host *\n\tStrictHostKeyChecking no\n\n
+ chmod 700 /root/.ssh
+ git clone /backend
fatal: repository '/backend' does not exist
+ docker build . --force-rm --build-arg SSH_PRIVATE_KEY=config/backend_key.pem
/bin/sh: 1: docker: not found
ERROR: Service 'app' failed to build: The command '/bin/sh -c set -x     eval $(ssh-agent -s);     echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add -;     mkdir -p ~/.ssh;     echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config;     chmod 700 ~/.ssh;     git clone ${BACKEND_PATH};     docker build . --force-rm --build-arg SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY_BACKEND};' returned a non-zero code: 127

完整的 Docker 文件:

FROM python:3.7-stretch
ARG CONFIG_ENV="development"
ARG BACKEND_PATH="git@gitlab.company.com:IT/mycompany-next-gen/backend.git"
ARG SSH_PRIVATE_KEY
ARG SSH_PRIVATE_KEY_BACKEND=config/backend_key.pem
RUN echo $SSH_PRIVATE_KEY
RUN if [ -z $CONFIG_ENV ]; then >&2 echo "Please pass the 'CONFIG_ENV' as a build argument"; exit 1; fi

# Copy the store handler code into the image
ARG AGGREGATION_PATH=/store-handler-project
WORKDIR ${AGGREGATION_PATH}
COPY store-handler store-handler
COPY store-handler .

# Copy the backend code into the image
ARG BACKEND_PATH=/backend
WORKDIR backend
RUN set -x \
    ### ssh configuration, see https://docs.gitlab.com/ee/ci/ssh_keys/#ssh-keys-when-using-the-docker-executor
    # run ssh-agent inside build environment
    eval $(ssh-agent -s); \
    echo "${SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add -; \
    # create the SSH directory and give it the right permissions
    mkdir -p ~/.ssh; \
    # Allow access to gitlab.your.lan
    echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config; \
    chmod 700 ~/.ssh; \
    git clone ${BACKEND_PATH}; \

    docker build . --force-rm --build-arg SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY_BACKEND};


# Copy the config file and mongo certificate to the image
WORKDIR /etc/store-handler/
COPY config/${CONFIG_ENV}.pem mongo.pem
COPY requirements.txt .
COPY config/${CONFIG_ENV}.ini config.ini
COPY config/logging.conf .

# Install all of the python requirements in the image
RUN pip install -r requirements.txt

# Install systemd
RUN apt-get update; apt-get -y install systemd systemd-sysv

# Install docker-systemctl-replacement
# docker systemctl replacement - allows to deploy to systemd-controlled containers without starting an actual systemd daemon
RUN wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -O /usr/local/bin/systemctl
RUN chmod +x /usr/local/bin/systemctl

# Set Python path correctly
RUN export PYTHONPATH=/store-handler-project

# Clean up the /tmp/
WORKDIR /root
RUN rm -rf /tmp/store-handler/

标签: pythongitdocker

解决方案


推荐阅读