gpt4 book ai didi

docker - 使用与主机发送请求相同的网络从Docker容器发送HTTP请求

转载 作者:行者123 更新时间:2023-12-02 19:30:12 27 4
gpt4 key购买 nike

我的应用程序是dockerized。它的python/django应用程序。我们正在使用基于IP的本地短信发送API。所以我给了他们我的EC2 IP地址。我正在这个EC2机器上运行我的docker容器。但是我的python应用无法将请求发送到该计算机。因为此Docker容器具有不同的IP。

我该如何解决这个问题?

Dockerfile

# ToDo use alpine image
FROM python:3.6
# Build Arguments with defaults
ARG envior
ARG build_date
ARG build_version
ARG maintainer_name='Name'
ARG maintainaer_email='email@email.com'
# Adding Labels
LABEL com.example.service="Service Name" \
com.example.maintainer.name="$maintainer_name" \
com.example.maintainer.email="$maintainaer_email" \
com.example.build.enviornment="$envior" \
com.example.build.version="$build_version" \
com.example.build.release-date="$build_date"
# Create app directory
RUN mkdir -p /home/example/app
# Install Libre Office for pdf conversion
RUN apt-get update -qq \
&& apt-get install -y -q libreoffice \
&& apt-get remove -q -y libreoffice-gnome
# Cleanup after apt-get commands
RUN apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
/var/cache/apt/archives/*.deb /var/cache/apt/*cache.bin
# Activate WORKING DIR
WORKDIR /home/example/app
# Copying requirements
COPY requirements/${envior}.txt /tmp/requirements.txt
# Install the app dependencies
# ToDo Refactor requirements
RUN pip install -r /tmp/requirements.txt
# Envs
ENV DJANGO_SETTINGS_MODULE app.settings.${envior}
ENV ENVIORNMENT ${envior}
# ADD the source code and entry point into the container
ADD . /home/example/app
ADD entrypoint.sh /home/example/app/entrypoint.sh
# Making entry point executable
RUN chmod +x entrypoint.sh
# Exposing port
EXPOSE 8000
# Entry point and CMD
ENTRYPOINT ["/home/example/app/entrypoint.sh"]

docker-compose.yml
version: '3'
services:
postgres:
image: onjin/alpine-postgres:9.5
restart: unless-stopped
ports:
- "5432:5432"
environment:
LC_ALL: C.UTF-8
POSTGRES_USER: django
POSTGRES_PASSWORD: django
POSTGRES_DB: web
volumes:
- postgres_data:/var/lib/postgresql/data/

web:
build:
context: .
args:
environ: local
command: gunicorn app.wsgi:application -b 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- postgres
environment:
DATABASE_URL: 'postgres://django:django@postgres/web'
DJANGO_MANAGEPY_MIGRATE: 'on'
DJANGO_MANAGEPY_COLLECTSTATIC: 'on'
DJANGO_LOADDATA: 'off'
DOMAIN: '0.0.0.0'

volumes:
postgres_data:

最佳答案

您应该尝试将容器与EC2实例放置在同一网络中。这意味着通过host驱动程序使用网络。

建议的 docker-compose 文件

version: '3'
services:
postgres:
[...]
networks:
- host
volumes:
- postgres_data:/var/lib/postgresql/data/

web:
[...]
networks:
- host

volumes:
postgres_data:

networks:
host:

万一它不起作用,您可以通过以下方式定义自己的网络:
networks:
appnet:
driver: host

并连接到该网络表单服务:
postgres:
[..]
networks:
- appnet

进一步阅读有关网络 official ref
official networking tutorial也很有趣。

关于docker - 使用与主机发送请求相同的网络从Docker容器发送HTTP请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52052564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com