zoukankan      html  css  js  c++  java
  • docker container 相互调用 ——link

    原文:

    https://michaeljolley.com/blog/communication-between-containers-using-docker-compose-in-windows/

    services:
      iotapp:
        image: ${DOCKER_REGISTRY-}iotapp
        build:
          context: .
          dockerfile: *path to your app Dockerfile here*
        ports:
          - {external port}:{internal port}
        links:
          - api
      api:
        image: ${DOCKER_REGISTRY-}api
        build:
          context: .
          dockerfile: *path to your api Dockerfile here*
        ports:
           - {external port}:{internal port}
    

      

    Communication between containers using docker compose in Windows

    February 16, 2019    |   3 min read

    This definitely has to be filed under "remember this in the future."

    I've been working on a project lately that includes an ASPNETCore SPA using Angular. It's being deployed to multiple Raspberry Pi's using a popular deployment tool. However, as the number of devices used by the client grows the cost of the deployment tool is becoming prohibitive. While exploring other options we landed on the Azure IoT Hub.

    In production, the application on the Pi communicates with a Restful API that lives at the clients main office. However, while debugging we need to run them side-by-side. So, docker-compose to the rescue (I think.)

    Setting up the Dockerfiles

    Most of our developers are using VS 2017 or 2019 so we used the built in functionality to add pre-built Dockerfile to the IoT application.

    Once we started debugging we found our docker based IoT application couldn't communicate with the localhost:port that the API was loaded to. This made complete sense because the docker container thinks that it is localhost, not the host machine. So we decided to load our API into a container. Same steps, using Visual Studio to add the Dockerfile.

    The fun begins (or doesn't)

    With the two Dockerfiles in place we setup a docker-compose that would launch each of them. After reviewing multiple sites explaining varying ways to allow the two containers to communicate we finally found the correct solution. So, for our future reminder, and possibly a chance to help others:

    How to setup a docker-compose file to allow communication between two or more containers on Windows

    While the wind-up for this post has been huge, actually getting the docker-compose right is fairly straight forward.

    Firstly, add your two services in the docker compose like so:

    version 3.4
    
    services:
      iotapp:
        image: ${DOCKER_REGISTRY-}iotapp
        build:
          context: .
          dockerfile: *path to your app Dockerfile here*
        ports:
          - {external port}:{internal port}
        links:
          - api
      api:
        image: ${DOCKER_REGISTRY-}api
        build:
          context: .
          dockerfile: *path to your api Dockerfile here*
        ports:
           - {external port}:{internal port}
    
    

    Notice the links flag in the iotapp service. This lets Docker know that iotapp will need to be able to communicate with the api project.

    Pretty simple right?

    The missing piece of the puzzle for us was how to communicate between the two.

    For instance, in our iotapp code, it is hardcoded to communicate with http://localhost:{port}/api. However, this won't be okay because the api & iotapp containers each have their own "localhost". After much experimenting and several incorrect StackOverflow & Google responses, we found you must call the name of the service specified in the docker-compose. So any call in our iotapp to http://localhost:{port}/api needed to be changed to http://api:{port}/api.

    Seeing what's required and the "how-to", it makes complete sense, but this one stumped me for several hours. Hopefully if you're reading this Google has sent you and it saves you that time.

    Anything I missed or that we could do better? Leave me a comment below.

     

     

    https://stackoverflow.com/questions/44275794/how-can-one-docker-container-call-another-docker-container

     

     

    I have two Docker containers

    1. A Web API
    2. A Console Application that calls Web API

    Now, on my local web api is local host and Console application has no problem calling the API.However, I have no idea when these two things are Dockerized, how can I possibly make the Url of Dockerized API available to Dockerized Console application?

    i don't think i need a Docker Compose because I am passing the Url of API as an argument of the API so its just the matter of making sure that the Dockerized API's url is accessible by Dockerized Console

    Any ideas?

    his is the best way I have found to connect multiple containers in a local machine / single cluster.

    Given: data-provider-service, data-consumer-service

    • Option 1: Using Network
    docker network create data-network
    docker run --name=data-provider-service --net=data-network -p 8081:8081 data-provider-image
    docker run --name=data-consumer-service --net=data-network -p 8080:8080 data-consumer-image
    
    

    Make sure to use URIs like: http://data-provider-service:8081/ inside your data-provider-service.

    • Option 2: Using Docker Compose

    You can define both the services in a docker-compose.yml file and use depends_on property in data-provider-service. e.g.

    data-consumer-service:
      depends_on:
        - data-provider-service
         
    

    You can see more details here on my Medium post: https://saggu.medium.com/how-to-connect-nultiple-docker-conatiners-17f7ca72e67f

    he idea is not to pass the url, but the hostname of the other container you want to call.
    See Networking in Compose

    By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

    This is what replace the deprecated --link option.

    And if your containers are not running on a single Docker server node, Docker Swarm Mode would enable that discoverability across multiple nodes.

    https://brianchildress.co/call-localhost-on-host-from-docker-container/

    There are times when we need to call “localhost” on a host from within a Docker container. For example, when developing locally using Docker I need to call another service that is running on my host machine and not within the context of Docker, so I can’t use Docker bridge networking.

    Recently I need to call a localhost service from a container running the AWS CLI. If I tried to call http://localhost:1234 from within the AWS CLI Docker container I would be making a request to the localhost that the container is thinking about NOT the localhost on my machine (AKA the host).

    To be able to communicate from within a container to a host service we can use the --net="host" flag as part of the docker run command. More on Docker Host Networking

    The result would look like this:

    1
    docker run --rm -it --net="host" -v ~/.aws:/root/.aws amazon/aws-cli <command>
  • 相关阅读:
    TLPI读书笔记第15章-文件属性2
    TLPI读书笔记第15章-文件属性1
    Java异常及错误
    10055
    4月。
    JavaScript三种方法获取地址栏参数的方法
    页面预加载loading动画,再载入内容
    什么是可串行化MVCC
    简化版扫雷详细解
    论unity中UI工具与GUI函数
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/15311958.html
Copyright © 2011-2022 走看看