Accessing localstack images at localhost for Django ImageField

403 Views Asked by At

I have a django rest app that I am running locally with docker and a docker-compose file.

I have a model with an image field. However, the image field has a URL in the form of http://localstack/... but the image is not accessible there. It is actually accessible at http://localhost/....

The following is a snippet of the important bits of my docker-compose:

version: "3.9"

services:
  app:
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - .:/commas-api
    restart: always
    command: sh -c "/app/start-server-local.sh"
    environment:
      STAGE: 'local'
      DB_NAME: 'api_db'
      DB_USERNAME: 'user'
      DB_PASSWORD: 'password'
      DB_HOST: 'db'
      DB_PORT: 3306
      DJANGO_SUPERUSER_EMAIL: '[email protected]'
      DJANGO_SUPERUSER_PASSWORD: 'pass'
      DJANGO_SECRET_KEY: 'xxxx'
      AWS_ACCESS_KEY_ID: 'dummy'
      AWS_SECRET_ACCESS_KEY: 'dummy'
      AWS_S3_ENDPOINT_URL: 'http://localstack:4566'
      AWS_SES_ACCESS_KEY_ID: 'dummy'
      AWS_SES_SECRET_ACCESS_KEY: 'dummy'
    expose:
      - 8000
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
      localstack:
        condition: service_healthy
    links:
      - localstack

  localstack:
    image: localstack/localstack
    ports:
      - "4566:4566"
    volumes:
      - ./localstack:/etc/localstack/init/ready.d

volumes:
  commas-api:

The Django Rest project is using S3Boto3Storage where you can see the endpoint provided in the environment variables is: http://localstack:4566

However, if I try to change this to http://localhost:4566, then I get connection errors:

botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL: "http://localhost:4566/myBucket/static/admin/css/login.css"

I assume this may be a networking issue between localhost and the two apps where boto is running in the app so the localhost there is different. However, I am not super experienced with docker.

My goal is to have the image field of my model be an endpoint that is accessible on the app/browser when I navigate to it so that my frontend running locally can load the image.

Any help would be greatly appreciated!

0

There are 0 best solutions below