Search code examples
dockerdocker-composedockerfileip-addressdocker-network

Sending http requests from docker container using same network as sending requests from host machine


My application is dockerized. Its python/django application. We are using a local sms sending api that is restricted on IP based. So I have given them my EC2 ip address. And I am running my docker container in this EC2 machine. But my python app is not able to send requests to that machine. Because this docker container has different IP.

How do I solve this problem ?

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:

Solution

  • You should try putting the container in the same network as your EC2 instance. It means using networks with host driver.

    suggested docker-compose file

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

    In case it wouldn't work, you might define your own network by:

    networks:
      appnet:
        driver: host
    

    and connect to that network form services:

    postgres:
        [..]  
        networks:
          - appnet
    

    Further reading about networks official ref. An interesting read too from official networking tutorial.