Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

Docker | access host file in python within docker container

I am trying to access certbot ssl certificates on my host machine within a docker container. My certifications are located in /etc/letsencrypt/live/domain.com I am loading my container using the args:

docker run -v /etc/letsencrypt/live/domain.com:/certs -it --rm -p 80:80 kaws/kaws_app:latest

My python script within the docker container says that the files or directories are not found. I'm trying to load the certificates that exist in /etc/letsencrypt/live/domain.com/

The error I'm receiving says:

ssl_context.load_cert_chain('/certs/fullchain.pem', '/certs/privkey.pem')
FileNotFoundError: [Errno 2] No such file or directory

How do I access files on my host machine from my python script within my docker container?

Dockerfile

# Use the official Python 3 image.
# https://hub.docker.com/_/python
FROM python:3-alpine

RUN 
  apk add --no-cache python3 py3-pip 
  && apk upgrade --update 
  && apk add git 
  && apk add --update alpine-sdk 
  && apk add libffi-dev openssl-dev 
  && apk --no-cache --update add build-base

COPY . /app

# Create and change to the app directory.
WORKDIR /app

RUN apk add --no-cache --virtual .build-deps 
  gcc musl-dev

RUN pip install --no-cache-dir -r requirements.txt

# Cleanup dev dependencies
RUN apk del -f .build-deps

RUN chmod 444 app.py
RUN chmod 444 requirements.txt

# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 80
ENV PORT 443

# Run the web service on container startup.
CMD [ "python", "app.py" ]

Update:
I ran the following code within my python script to see if it could see the certs and its able to see them, but it still gives an error.

for root, dirs, files in os.walk("/certs"):
    for filename in files:
        print(filename)

Output:

docker run kaws/kaws_app:latest
chain.pem
fullchain.pem
privkey.pem
cert.pem
README
Traceback (most recent call last):
  File "/app/app_pusher.py", line 83, in <module>
    ssl_context.load_cert_chain('/certs/fullchain.pem', '/certs/privkey.pem')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looking at https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag, you need to drop the : after /certs.

Your command is then

docker run -v /etc/letsencrypt/live/domain.com:/certs -it --rm -p 80:80 kaws/kaws_app:latest

You may need to enable sharing on your OS as well (you do on macOS).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...