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

时间:2018-08-28 07:42:21

标签: docker docker-compose dockerfile ip-address docker-networking

我的申请是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:

2 个答案:

答案 0 :(得分:1)

您应该尝试将容器与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也很有趣。

答案 1 :(得分:0)

将端口从docker容器发布到主机,然后在sms应用程序中配置ec2IP:port。