Title: How to send from one container to another container using docker-compose?
Tags: docker-compose
Question: I have two containers. When I call the first container using fastapi, then the container sends an image to the second container using requests and the second container receive the image and store it in a  volume. I'm getting error.
Files of first container:
main.py
```import base64
import io
import json
import logging
import os
from io import BytesIO

import requests
import uvicorn
from fastapi import FastAPI, File, Form, UploadFile
from fastapi.responses import FileResponse
from PIL import Image

app = FastAPI()


@app.get(&quot;/&quot;)
def read_root():
    img = Image.new('RGB', (200, 50), color = (73,195,150))
    img.save('newfile.jpg')
    print(&quot;image saved&quot;)
    img.show()
    ##send the image
    api = 'http://localhost:81/test'

    filename ='newfile.jpg'
    up = {'image':(filename, open(filename, 'rb'))}
    #json = {'first': &quot;Hello&quot;, 'second': &quot;World&quot;}
    request = requests.post(api, files=up)
    print(request.text)
    return {&quot;image&quot;:&quot;sent successfully:&quot;, &quot;statuscode&quot;:request.status_code}
```
Dockerfile:
```FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app /app
WORKDIR /app
RUN pip install Pillow requests python-multipart
CMD [&quot;uvicorn&quot;, &quot;app.main:app&quot;, &quot;--host&quot;, &quot;5086201276&quot;, &quot;--port&quot;, &quot;80&quot;]
COPY . /app
```
Files of the second container:
main.py:
```from fastapi import FastAPI,UploadFile,File,Form
from PIL import Image
import uvicorn
import io
import json                    
import base64                  
import logging             
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 
import shutil
app = FastAPI()

def read_imagefile(file) -&gt; Image.Image:
    image = Image.open(BytesIO(file))
    return image
@app.post(&quot;/test&quot;)
async def predict_api(file: UploadFile = File(...)):
    extension = file.filename.split(&quot;.&quot;)[-1] in (&quot;jpg&quot;, &quot;jpeg&quot;, &quot;png&quot;)
    if not extension:
        return &quot;Image must be jpg or png format!&quot;
    img = read_imagefile(await file.read())
    
    #img = Image.open(myfile) 
    draw = ImageDraw.Draw(img)
    # font = ImageFont.truetype(<font-file&gt;, <font-size&gt;)
    font = ImageFont.truetype(&quot;sans-serif.ttf&quot;, 16)
    # draw.text((x, y),&quot;Sample Text&quot;,(r,g,b))
    draw.text((0, 0),&quot;Manipulated&quot;,(255,255,255),font=font)
    img.save('sample-out.jpg')
    return {&quot;image&quot;: &quot;saved in vol&quot;}
```
Dockerfile:
```FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
COPY ./app /app
WORKDIR /app
RUN pip install Pillow python-multipart
CMD [&quot;uvicorn&quot;, &quot;app.main:app&quot;, &quot;--host&quot;, &quot;5086201276&quot;, &quot;--port&quot;, &quot;81&quot;]
COPY . /app
```
Docker-compose file:
```version: '3.8'

services:
  app1:
    build: ./app1/
    ports:
      - 80:80
    networks:
      - my-proxy-net
  app2:
    build: ./app2/
    volumes:
      - myapp:/app
    ports:
      - 81:81

networks:
  my-proxy-net:
    external: true
volumes:
  myapp:
```

Comment: you should not use localhost but the names of the containers in the same network, or services if you use compose. So in this case use app1 and app2 as hostnames respectivly.


Here is another answer: If your services are in same network, they can reach each other by using their respective container_name as hostname:
In your case for example:
```http://app1
```
Use
```docker container ps -a
```
and check &quot;NAMES&quot; column in order to fight the right name to call

Comment for this answer: updated answer to find the right container name
Comment for this answer: In this case this isn't the container name though. The container name would be something like `compose-project_app1_1`. app1 is the name of the *service* compose creates, although its not a real service, since only a swarm manager can create real docker services.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:12/11/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<docker-compose>', 'Title': 'How to send from one container to another container using docker-compose?', 'ViewCount': 485, 'id': 70315539, 'user_AboutMe': '', 'user_AccountId': 19897981.0, 'user_DisplayName': 'shamiul islam shifat', 'user_Reputation': 9.0, 'user_Views': 3.0, 'user_id': 14578676.0}}