1

So, I'm stuck in an issue related to using files stored in a server and not able to display them in the frontend.

My project is: React + Redux using Docker The React app is full, i.e., there's an API folder for the backend (react/redux), a CLIENT folder for the frontend (react/libraries) and MongoDB as DB.

Docker Compose creates these 3 parts, API, CLIENT and MONGO in just 1 container.

So, in the frontend, the user is able to select an image as an avatar, and then, this image is sent through the layers and saved in a specific folder (NOT BUILD/PUBLIC etc) inside the API docker image. It's possible to remove/delete and re-select it. Everything's working fine!

The issue is the display of this image in the frontend. The avatar component uses an IMAGE SRC to display it, but I can't find a valid URL to use to be able for the frontend TO SEE that image file saved in the API/server.

Since it's inside a container, I tried all possibilities I could find in Docker documentation... I think the solution relays in the NETWORK docker-compose option, but even though couldn't make it.

Docker Compose File:

version: '3.8'

services:
  client:
    build: ./client
    stdin_open: true
    image: my-client
    restart: always
    ports:
        - "3000:3000"
    volumes:
        - ./client:/client
        - /client/node_modules
    depends_on:
        - api
    networks:
        mynetwork:
            ipv4_address: 172.19.0.9

  api:
    build: ./api
    image: my-api
    restart: always
    ports:
        - "3003:3003"
    volumes:
        - ./api:/api
        - logs:/api/logs
        - /api/node_modules
    depends_on:
        - mongo
    networks:
        mynetwork:
            ipv4_address: 172.19.0.10

  mongo:
    image: mongo
    restart: always
    ports:
        - "27017:27017"
    volumes:
        - mongo_data:/data/db
    networks:
        - mynetwork

volumes:
  mongo_data:
  logs:

networks:
  mynetwork:
    driver: bridge
    ipam:
        config:
            - subnet: "172.19.0.0/24"

To summarize, there's a folder in the API side with images/files and I want to reference them as in

<img src="mynetwork:3003/imagefolder/imagefile.png"> or something like that...

I can't believe I have to use this other solution...Another Stackoverflow Reply

Luis Souza
  • 11
  • 3
  • Your backend (`api`) code needs to provide an HTTP-based route to retrieve it. You can't directly access files in containers, and especially not from Web browsers that might be running on different systems. – David Maze Aug 21 '20 at 23:34
  • You mean something like NGINX set to provide this access, saving the files somewhere there but PUBLIC? – Luis Souza Aug 23 '20 at 00:13

0 Answers0