Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker curl to other container in same host

Tags:

docker

I have 2 containers, first one running on port 80 and the other is running on port 8022, I try to make a curl request from the container that`s is running on port 8022 to the container on port 80 and i get a empty response.

For the container on port 8022 i run this command:

docker run -d -it --privileged -p 0.0.0.0:8022:80 -v ~/path/to/my/app:/var/www/app --network=bridge --memory 1073741824  my/app:latest

If a make a curl request to other host form example google, I get the response correctly.

Thanks for help

UPDATED Ok I can solve this, creating a network and using this network in both containers, then I add to host file of the 8022 container the IP of the port 80 container.

Thanks for @zero298 for help!!

like image 379
Algoleigol Avatar asked Oct 30 '17 14:10

Algoleigol


2 Answers

This is a very simple example of how to do inter-container communication over a user-defined, bridged network. Here are the 3 files that I have defined to make this possible:

~/Desktop/code/bootstrap.sh

This will kick off the demo by first creating a user defined, isolated network that your containers can talk over named example_nw. It then creates a new container, named "servertest", that will hold the server we will curl to. I'm using a node container because I'm just more familiar with it.

It will also create a volume that binds to your machines ~/Desktop/code/ directory which should contain all the code that we are using, including the node server.js script. The server listens on port 3000 and responds with: "Hello World`".

After creating the server, it kicks off another container, named "curler" that will install curl (debian doesn't come with it installed). After that, it curls to servertest:3000 and gets the correct reply because they are both connected to the same docker, user-defined, network: example_nw.

After completing, it cleans up by killing the server container and removing the example_nw network.

#!/usr/bin/env bash

# Create user-defined network
docker network create example_nw

# Create server to listen to pings on our network
docker run \
--rm \
-d \
-v ~/Desktop/code:/my/stuff \
--name servertest \
--expose 3000 \
--network example_nw \
node node /my/stuff/server

# Create a curler
docker run \
-it \
--rm \
-v ~/Desktop/code:/my/stuff \
--name pingtest \
--network example_nw debian \
/my/stuff/curler.sh

# Clean up
docker container stop servertest
docker network rm example_nw

~/Desktop/code/server.js

This is a really simple node.js script that will create a server that listens on port 3000.

/*jslint node:true, esversion:6*/

"use strict";

const http = require("http"),
    port = 3000;

// Make a server
const server = http.createServer((req, res) => {
    console.log("Got request");
    res.end("Hello World!\n");
});

// Have the server listen
server.listen(port, (err) => {
    if (err) {
        return console.error(err);
    }
    console.log(`Listening on port: ${port}`);
});

~/Desktop/code/curler.sh

This just installs curl in the container and then curls to servertest:3000.

#!/usr/bin/env bash

apt update
apt install -y curl
curl servertest:3000

Running ~/Desktop/code/bootstrap.sh will demonstrate the communication.


I would recommend reading the Docker documentation: Work with network commands because it gives a lot of good examples and use cases.

like image 157
zero298 Avatar answered Sep 17 '22 22:09

zero298


This is an example to use the user-generated network

docker network create mynetwork
docker run --network=mynetwork -d -p 127.0.0.1:8022:80 --name mynginx nginx
docker run -it --network=mynetwork appropriate/curl http://mynginx

Otherwise you can use the older --link option

docker run -d -p 127.0.0.1:8022:80 --name mynginx nginx
docker run -it --link=nginx appropriate/curl http://mynginx
like image 39
Stefano Avatar answered Sep 21 '22 22:09

Stefano