PrimuS
PrimuS

Reputation: 2683

Access endpoint in one docker container from another

I have a docker-compose file with two services: app and httpd

app

app:
    image: primus852/machinelearning:latest
    ports:
        - 5001:5000
    expose:
        - "5001"
    restart: always
    networks:
        - default
    volumes:
        - ./api:/app
    environment:
        - FLASK_APP=app/source/__init__.py
        - FLASK_ENV=development

httpd

httpd:
  image: primus852/mitswiki:latest
  ports:
    - 80:80
  restart: always
  networks:
    - default
  volumes:
    - ./project:/var/www/html

Flask app

The app container has an endpoint like this:

@app.route('/predict', methods=['GET'])
def predict():
    ...DO STH....

I can open http://localhost:5001/predict in my browser, works... I can curl from my cmd: curl localhost:5001/predict, works...

But when I am inside my httpd container this does not work from the console: curl localhost:5001/predict

curl: (7) Failed to connect to localhost port 5001: Connection refused

So I thought I address the app container as I address my mysql from inside my httpd container: curl app:5001/predict but it has the same result.

Can anyone see what I am doing wrong?

Upvotes: 0

Views: 2299

Answers (2)

Baklap4
Baklap4

Reputation: 4202

Inside the httpd container localhost refers to just that httpd container. It cannot access other containers by default.

Another thing which might be occuring is that your app is not open for 'remote' access. A connection from one container to another one is a remote connection.

Within your docker-compose files you can link containers to eachother

While the containers are linked you can then use curl to get the /predict page by using curl app:5001/predict

Upvotes: 0

grapes
grapes

Reputation: 8636

According to your yaml:

ports:
    - 5001:5000

Inside container you have to use port 5000

Upvotes: 2

Related Questions