0

I'm testing to see if two containers (tomcat and busybox) in a same network 'myNetwork' are able to communicate with each other.

I followed the steps:

  1. Create a network, 'myNetwork'

    docker network create -d bridge myNetwork

  2. Run a 'myTomcat' container in 'myNetwork'

    docker run -it --name myTomcat --net=myNetwork tomcat

  3. Run a 'busybox' container in the same network as 'myTomcat' network. i.e 'myNetwork'

    docker run -it --net=container:myTomcat busybox

  4. Test if tomcat container is accessible from busybox container using wget command

    docker container exec -it {busybox_container_id}

    /# wget localhost:8080

Here I I got an error wget: server returned error: HTTP/1.1 404

I wonder why I can't connect to tomcat container from busybox container even though I configured both containers in the same network. So I inspected by doing:

    docker network inspect myNetwork
[
  {
    "Name": "myNetwork",
...
    "Containers": {
      "43ba4d7ae27753f8085f5697cf6afc4eb872dbdbd2cf18138e3c6e3f90d54d15":{
        "Name": "myTomcat",
        "EndpointID":"03a98...",
...
        "IPv4Address": "172.20.0.2/16",
        "IPv6Address": ""
    }
 }
]
 
    docker inspect {tomcat_container_id}
[
...
  "Networks": {
    "myNetwork": {
      "NetworkID": "54bf...",
      ...
      "Gateway": "172.20.0.1",
      "IPAddress": "172.20.0.2"
      ...
    }
...
]
    docker inspect {busybox_container_id}
[
...
  "NetworkSettings": {
    "Bridge": "",
    ...
    "Networks": {}
  }
...
]

It seems tomcat is in 'myNetwork', but inspecting busybox container doesn't seem to show any network information. I'd appreciate for any help

lophih
  • 1
  • How do you connect from `busybox` to `tomcat`? Via name or IP? – Saeed Oct 25 '21 at 08:25
  • If you're getting an HTTP 404 error, you've connected successfully, and possibly the problem is that you haven't loaded any `.war` files into Tomcat. This having been said, the `--net=container:...` construction is really unusual; why this particular setup? – David Maze Oct 25 '21 at 10:37
  • @DavidMaze You're right. After I created index.html inside tomcat container's $CATALINA_HOME/webapps/ROOT/index.html, I was able to 'wget localhost:8080' from busybox container and download index.html file. I'm trying `--net=container:...` command just for learning purposes, while I'm learning some basic networking command in docker. Thank you. – lophih Oct 25 '21 at 14:11
  • @Saeed My test case assumes that both `busybox` and `tomcat` containers are in the same network. So you might be able to connect to a running `tomcat` server by simply `localhost:8080`, or `myTomcat:8080` as AymDev answered. – lophih Oct 25 '21 at 14:28
  • Also Default tomcat webapps directory has been moved to webapps.dist starting with Tomcat version 7, preventing the tomcat default page loading up as soon as the tomcat instance started. https://stackoverflow.com/questions/61972819/cannot-access-tomcat-manager-or-any-other-default-tomcat-app-when-running-tomcat – lophih Oct 31 '21 at 01:51

1 Answers1

1

Using localhost always point to the current machine. Using it in a Docker container targets the current container.

If you need your containers to communicate in a Docker network you can make use of the embedded DNS server by using container names as host names. As you named your Tomcat container myTomcat you should be able to do:

wget myTomcat:8080
AymDev
  • 6,626
  • 4
  • 29
  • 52
  • Thank you for your answer. I found that my `HTTP 404 error` was not because I couldn't access the tomcat server, but because the running tomcat server simply didn't have a default index.html page configured in the `$CATALINA_HOME/webapps/ROOT` directory. After creating index.html inside the tomcat container's directory, I was able to `wget myTomcat:8080` from `busybox` container. And `wget localhost:8080` also worked fine, because both containers are in the same network, I think. – lophih Oct 25 '21 at 14:20
  • @lophih Oh maybe that's because of the way you defined the network of the busybox container. I never used `--net=container:...`, not even `--net` alone. That's not very common. I guess that this syntax acts like host networking between containers because there is no way that `localhost` points to another container except if it inherited its network interfaces. If you want to get a more *"standard"* setup you could add the busybox container to the network as you did for the Tomcat one, then I think you'll be able to test that `localhost:8080` won't work but `myTomcat:8080` will. – AymDev Oct 25 '21 at 14:26