1

I have two docker containers. One is java server, running on 8080 port with rest API /drivers. Another container is simple nodejs server with index.html page where ajax call is being performed to save new driver. URL in js file is: const URL = "http://storage:8080/drivers";

When I run them using just docker and created for them network, communication between them works fine. But when I run both containers using docker-compose, then I get status "(failed) net::ERR_NAME_NOT_RESOLVED"

When I open bash of any of this containers and run 'ping storage', I normally receive packets.

What am I missing?

DockerFile for java server:

FROM java:8

VOLUME /tmp
ADD target/docker-project-1.0-SNAPSHOT.jar app.jar

EXPOSE 8080

RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

DockerFile for nodejs server:

FROM node:argon

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN npm install connect serve-static

COPY . /usr/src/app

EXPOSE 8081

CMD ["node", "server.js"]

docker-compose.yml file:

version: '2.1'
services:
  client:
    image: glasierr/js-client
    networks:
       default:
          aliases:
             - "client"
    links:
       - "storage"
    ports:
       - "8081:8081"
  storage:
    image: glasierr/drivers-storage
    networks:
       default:
          aliases:
            - "storage"
    ports:
       - "8080:8080"
    expose:
       - "8080"

JS script:

 const URL = "http://storage:8080/drivers";
    $.ajax({
        headers: {
            'Content-Type': 'application/json'
        },
        type: "POST",
        url: URL,
        data: JSON.stringify({
            licenceId: licenceId,
            name: name,
            surname: surname,
            email: email
        }),
        dataType: "json"
    });

1 Answer 1

1

I cannot see any mistake and tried it and it worked with compose version 2, maybe it is not working with experimental.

But you can simplify your compose file to that

version: '2'
services:
  client:
    image: glasierr/js-client
    ports:
       - 8081:8081
  storage:
    image: glasierr/drivers-storage
    ports:
       - 8080:8080

All containers are automatically in the default overlay network and are reachable with their service name (and your alias is the same), so you can remove networks. links does the same as the default network and depends_on and is not needed in this example. You also only need expose if you don't publish 8080.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried your simplified compose file and result was the same. Tried cleaning local images and pulling from remote, but nothing changed. I am using ubuntu 16. Maybe it may have problems? Btw, how did you check that it worked? I am going at 172.19.0.2:8081/index.html (or 172.19.0.3) and press 'submit' there.
Sorry i only tryed connecting from inside the client container to the storage. And if only the container port is specified a random port will be choosen. But now it also works from outside the container
Btw, I can use localhost:8081 from outside, but i dont know how your setup looks like
For setup I just run "docker-compose up" with your compose file. It all works if i set URL in js script to localhost:8080/drivers. But it refuses to work on storage:8080/drivers. From inside of client container (bash) I can ping storage, but when I run my webpage from browser, ajax POST fails.
ok, this is a diffrent problem. You can not use storage from the browser. It is only avalible inside the container. If you try docker exec -it desktop_client_1 curl storage:8080/drivers it will return an empty array. The best way would be to proxy the request so you dont have to access storage from outside
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.