How to send HTTP requests to my server running in

2020-03-04 03:35发布

I've set up a java application which acts as a web server and handles http requests. I've tested that it works outside the container (which it does), but when in the container my requests don't seem to get to it.

The server listens on port 3971, and the Dockerfile looks like this:

FROM java:8
ADD VaultServer /
EXPOSE 3971
EXPOSE 3972
ENTRYPOINT ["java", "-jar", "VaultServer.jar"]

Calling to the root address should return something (normally I'd send a GET to http://localhost:3971/).

I've tried replacing 'localhost' with the the ip address of docker-machine, and also with the ip address I get when inspecting the running container for my server, but neither seem to respond. When I call to the ip address of docker-machine, I get ERR_CONNECTION_REFUSED. Is there something else I need to enable?

4条回答
女痞
2楼-- · 2020-03-04 04:03

Are you using Docker Toolbox???

In my case, the normal docker installation didnt work on my Windows 10 so I had to install Docker toolbox; docker toolbox adds a virtual machine where the docker runs all their containers.

For example, to access my dockerized apps I have to use: http://192.168.99.100:8080/app.

Here, 192.168.99.100 is the virtual machine ip.

Hope it helps you!!!!

查看更多
欢心
3楼-- · 2020-03-04 04:05

you are doing EXPOSE 3972 which exposes the port to other linked containers but NOT to the host machine

To expose the port to the host machine you do ...

docker run -p 3972:3972 ....... etc

you can also do ...

docker run -P

which exposes all ports exposed by EXPOSE to the host (this is not the default - you must use the -P flag here)

查看更多
疯言疯语
4楼-- · 2020-03-04 04:14

Use the "action="http://192.168.99.100:8080/rest/data/ingest" in your html to refer to your Docker Container's Rest Path. See the bottom HTML Code, line 4.

http://192.168.99.100:8080/rest/data/ingest Breakdown:

1) 192.168.99.100:8080= Docker's IP Address & Port #.

2) /rest/ = In your web.xml file

3) data/= @Path("/data")

4) /ingest= @Path("/ingest")

HTML CODE:

<html>
    <body>
        <h1>Data Ingest: Web Uploader</h1>     
        <form action="http://192.168.99.100:8080/rest/data/ingest" method="post" enctype="multipart/form-data">

           <p>Select files
: <input type="file" name="file" size="10000000" multiple/> </p>
            <input type="submit" value="Upload Files"/>
        </form>
    </body>
</html>

JAVA CODE:

@Path("/data")
public class YourClass {

@POST
@Path("/ingest")
public Response yourMethod (...){
}
查看更多
时光不老,我们不散
5楼-- · 2020-03-04 04:19

I can show you how I compose dotnet core project with other containers (MSSQL and Elasticsearch), maybe it will help you.

To docker-compose.yml add dependencies for another container:

services:

  prime.web:
    image: prime.web
    build:
      context: ./Prime.Web
      dockerfile: Dockerfile
    links:
      - sql.data
      - elasticsearch

  sql.data:
    image: microsoft/mssql-server-linux  

  elasticsearch:
    image: elasticsearch

And write in docker-compose.override.yml, where you will override your appsetting file, and will get access to another container:

services:  

  prime.web:
    environment:
      - 'ConnectionStrings:primekinetics=Server=tcp:sql.data,1433;InitialCatalog=prime;User ID=sa;Password=Pass@word;Connection Timeout=30;'      
      - 'ElasticSearchConnection=http://elasticsearch:9200/'
      - ASPNETCORE_ENVIRONMENT=Development      
    ports:
      - "5105:80"

  sql.data:
    environment:      
      - SA_PASSWORD=Pass@word
      - ACCEPT_EULA=Y
    ports:
      - "5433:1433"

  elasticsearch:    
    ports:
      - "9200:9200"

I hope it will be helpful.

查看更多
登录 后发表回答