admin管理员组

文章数量:1125723

I'm trying to connect a Mage.ai pipeline to an endpoint of my development server (a microservice of my app). However, I barely have any knowledge on the topic and it's getting rough.

My app uses Vue and Flask and these are the steps I've followed until now:

  1. I checked that the endpoint is valid.

    curl -X POST http://localhost:4999/user/login -H "Content-Type: application/json" -d '{"username": "USERNAME", "password": "PASSWORD"}'

    Returns me an OK and a valid token.

  2. Setted the app to run on 0.0.0.0, on both the microservice and the login endpoint. These are the lines of my login endpoint, for example:

    if __name__ == '__main__':
        app.run(
            host='0.0.0.0',  
            # host='127.0.0.1',
            port='4999',
            debug = True
        )
    
      WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: on
     * Running on :4999/ (Press CTRL+C to quit)
     * Restarting with stat
    

    This is the Vue message:

      App running at:
      - Local:   http://localhost:9528/ 
      - Network: http://172.22.15.213:9528/
    
  3. Did a ping to establish a connection with that network address:

    root@fcf10b9466d3:/home/src/pipelines/actualizarfirmas_test# ping 172.22.15.213
    
    PING 172.22.15.213 (172.22.15.213) 56(84) bytes of data.
    
    ^C
    
    --- 172.22.15.213 ping statistics ---
    
    52 packets transmitted, 0 received, 100% packet loss, time 52210ms
    

And that's it because I do not know what else to do.

This is my microservice important flask lines:

@app.route('/receive_results', methods=['POST'])
def receive_results():
    data = request.json
    print("Received results:", data)

    status = data.get("status_received")
    message = data.get("message_received")

    return jsonify({
        "status_received": status,
        "message_received": message,
        "details": "Conexión exitosa con la API."
    })

if __name__ == '__main__':

    app.run(
        host='0.0.0.0',  
        # host='127.0.0.1',  
        port='5009',
        # debug = True,
        # threaded=False
    )

This is the way I intended to connect with my microservice (I just wanted to send back a response of the status of the pipeline, fail, success and why):

import requests

def enviar_mensaje_amic(custom_args, res, msg, token): 
    custom_args['status_received'] = res
    custom_args['message_received'] = msg 
    print(res)
    print(token)
    print(msg)
    try:
        result = {
            "status_received": custom_args['status_received'],
            "message_received": custom_args['message_received'],
            "execution_details": "Custom results."
        }
        headers = {
            "Authorization": f"Bearer {token}",
            "Content-Type": "application/json"
        }
        response = requests.post("http://172.22.15.213:5009/receive_results", json=result, headers=headers)
        print(response.status_code)
        print(response.json()) 
        print("Datos recibidos:", response.status_code)
    except Exception as e:
        print(f"Error al enviar datos: {e}")
    return 

def amic_login(custom_args, config_data):
    base_url = "http://172.22.15.213:4999"
    login_endpoint = f"{base_url}/user/login"

    userdata = custom_args.get('userdata', [])
    user = userdata[0]
    pwd = userdata[1]

    payload = {
        "username": user,
        "password": pwd
    }
    try:
        response = requests.post(login_endpoint, json=payload)
        response_data = response.json()
        if response.status_code != 200 or "token" not in response_data:
            raise Exception(f"Autentificación fallida: {response_data.get('message', 'Incógnita...')}")
            # MIRAR CUÁL ES LA ESTRUCTURA DEL JSON QUE SE DEVOLVERÍA (PRODUCIDO POR AMIC, ESTO ES UNA BASE GUÍA!)
        return response_data['token']
    except Exception as e:
        raise Exception(f"Error inesperado durante la autentificación: {str(e)}")

I'm trying to connect a Mage.ai pipeline to an endpoint of my development server (a microservice of my app). However, I barely have any knowledge on the topic and it's getting rough.

My app uses Vue and Flask and these are the steps I've followed until now:

  1. I checked that the endpoint is valid.

    curl -X POST http://localhost:4999/user/login -H "Content-Type: application/json" -d '{"username": "USERNAME", "password": "PASSWORD"}'

    Returns me an OK and a valid token.

  2. Setted the app to run on 0.0.0.0, on both the microservice and the login endpoint. These are the lines of my login endpoint, for example:

    if __name__ == '__main__':
        app.run(
            host='0.0.0.0',  
            # host='127.0.0.1',
            port='4999',
            debug = True
        )
    
      WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: on
     * Running on http://0.0.0.0:4999/ (Press CTRL+C to quit)
     * Restarting with stat
    

    This is the Vue message:

      App running at:
      - Local:   http://localhost:9528/ 
      - Network: http://172.22.15.213:9528/
    
  3. Did a ping to establish a connection with that network address:

    root@fcf10b9466d3:/home/src/pipelines/actualizarfirmas_test# ping 172.22.15.213
    
    PING 172.22.15.213 (172.22.15.213) 56(84) bytes of data.
    
    ^C
    
    --- 172.22.15.213 ping statistics ---
    
    52 packets transmitted, 0 received, 100% packet loss, time 52210ms
    

And that's it because I do not know what else to do.

This is my microservice important flask lines:

@app.route('/receive_results', methods=['POST'])
def receive_results():
    data = request.json
    print("Received results:", data)

    status = data.get("status_received")
    message = data.get("message_received")

    return jsonify({
        "status_received": status,
        "message_received": message,
        "details": "Conexión exitosa con la API."
    })

if __name__ == '__main__':

    app.run(
        host='0.0.0.0',  
        # host='127.0.0.1',  
        port='5009',
        # debug = True,
        # threaded=False
    )

This is the way I intended to connect with my microservice (I just wanted to send back a response of the status of the pipeline, fail, success and why):

import requests

def enviar_mensaje_amic(custom_args, res, msg, token): 
    custom_args['status_received'] = res
    custom_args['message_received'] = msg 
    print(res)
    print(token)
    print(msg)
    try:
        result = {
            "status_received": custom_args['status_received'],
            "message_received": custom_args['message_received'],
            "execution_details": "Custom results."
        }
        headers = {
            "Authorization": f"Bearer {token}",
            "Content-Type": "application/json"
        }
        response = requests.post("http://172.22.15.213:5009/receive_results", json=result, headers=headers)
        print(response.status_code)
        print(response.json()) 
        print("Datos recibidos:", response.status_code)
    except Exception as e:
        print(f"Error al enviar datos: {e}")
    return 

def amic_login(custom_args, config_data):
    base_url = "http://172.22.15.213:4999"
    login_endpoint = f"{base_url}/user/login"

    userdata = custom_args.get('userdata', [])
    user = userdata[0]
    pwd = userdata[1]

    payload = {
        "username": user,
        "password": pwd
    }
    try:
        response = requests.post(login_endpoint, json=payload)
        response_data = response.json()
        if response.status_code != 200 or "token" not in response_data:
            raise Exception(f"Autentificación fallida: {response_data.get('message', 'Incógnita...')}")
            # MIRAR CUÁL ES LA ESTRUCTURA DEL JSON QUE SE DEVOLVERÍA (PRODUCIDO POR AMIC, ESTO ES UNA BASE GUÍA!)
        return response_data['token']
    except Exception as e:
        raise Exception(f"Error inesperado durante la autentificación: {str(e)}")
Share Improve this question edited 2 days ago Brian Tompsett - 汤莱恩 5,87572 gold badges61 silver badges133 bronze badges asked 2 days ago user27827714user27827714 212 bronze badges 2
  • So you have a server on the host and you want the app running in a docker to connect to it? – Michael Wiles Commented 2 days ago
  • @MichaelWiles I have an app running locally on my machine. My superiors' has a Docker where the Mage.ai is running. The pipeline that must connect to my app is on Mage.ai. I do not know how to proceed in order to connect these two things, since when I do a curl from within the Docker container (from Mage.ai website terminal, since that Docker is not running on my machine, I'm just on the network) to my localhost endpoint (login) it does this: PING 172.22.15.213 (172.22.15.213) 56(84) bytes of data and then keeps on thinking endlessly. I just never did this before XD – user27827714 Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Since your app is running locally on your computer, you can achieve a workaround by setting the network_mode of your container to host, which will give the container "raw access to the host's network interface" (ref), meaning you can access the service via localhost:port as you would from outside the container.

Or, if you're running on Docker Desktop, you could try referencing the host.docker.internal host, which should work as a sort of alias to localhost outside of the container. So where you would usually reference localhost:port you would use host.docker.internal:port instead.

本文标签: flaskHow to establish Docker Connection to Localhost Development ServerStack Overflow