Bài toàn đặt ra là khi mà bạn run docker build thì yêu cần cần clone/pull 1 số repo private
Mình nghĩ ra 1 cách là thực hiện COPY ~/.ssh/id_rsa /root/.ssh/
như bị lỗi như bên dưới.
Step 32/38 : COPY ~/.ssh/id_rsa /root/.ssh/ COPY failed: file not found in build context or excluded by .dockerignore: stat ~/.ssh/id_rsa: file does not exist
Mình tìm thấy chả có quy định j trong .dockerignore
Giờ chúng ta sự dụng argument để làm việc này
https://stackoverflow.com/questions/18136389/using-ssh-keys-inside-docker-container
Build command
docker build -t example --build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)" --build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa.pub)" --squash .
Dockerfile
FROM python:3.6-slim ARG ssh_prv_key ARG ssh_pub_key RUN apt-get update && \ apt-get install -y \ git \ openssh-server \ libmysqlclient-dev # 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 # Avoid cache purge by adding requirements first ADD ./requirements.txt /app/requirements.txt WORKDIR /app/ RUN pip install -r requirements.txt # Remove SSH keys RUN rm -rf /root/.ssh/ # Add the rest of the files ADD . . CMD python manage.py runserver