Docker HTTPS请求在容器内部失败,在容器外部成功

时间:2020-09-08 06:36:19

标签: python docker flask dockerfile

我有一个Flask应用,正在使用该应用从远程端点提取数据。当我在开发机器上运行应用程序时,我可以从服务器返回结果而不会出现问题。当我在Docker容器中运行应用程序时,该完全相同的请求失败。

Dockerfile

FROM python:3

ENV PYTHONDONTWRITEBYTECODE 1
ENV FLASK_APP "run.py"
ENV FLASK_ENV "development"
ENV FLASK_DEBUG True

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN python3 -m pip install -r requirements.txt


COPY . /app

ADD . /app

EXPOSE 5000


ENTRYPOINT [ "python3" ]

CMD [ "run.py" ]

docker-compose.yml

version: '2'

services:
    app:
        build: ./
        ports:
            - "5000:5000"
        network_mode: host

实际代码:

def crs_customer_login(self):
        
        conn = http.client.HTTPSConnection('secret-url.com',443)
        headers = {'Content-type': 'application/json'}
        data = {
            "secret": SECRET,
            "apikey": API_KEY
        }
        json_data = json.dumps(data)
        
        conn.request(
            'POST', '/{}/direct/login'.format(BASE_URL), json_data, headers)
        res = conn.getresponse()
        decoded_data = json.loads(res.read().decode('utf-8'))
        res_object = {}
        res_object['direct_token'] = decoded_data['token']
        res_object['expires'] = decoded_data['expires']
        res_object['direct_refresh_token'] = decoded_data['refresh']
       
        return res_object

构建图像时,我使用以下命令docker run --env-file .env --name flask-api-container -p 5000:5000 flask-api

创建容器

我想强调的是,当我在本地运行应用程序时,我可以毫无问题地访问URL。将其放入Docker容器后,一无所有。我同时使用了requestshttp.client

0 个答案:

没有答案