简体   繁体   中英

How can I connect docker container use localhost?

I created a docker-container based on nodejs. However, it seems to run fine, but both localhost:8080 and localhost:8443 and localhost can't connect. Also, the connection using curl produces the following message:

$ curl -vvv localhost:8080
*   Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.67.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
$ curl -vvv localhost:8443
*   Trying ::1:8443...
* TCP_NODELAY set
* Connected to localhost (::1) port 8443 (#0)
> GET / HTTP/1.1
> Host: localhost:8443
> User-Agent: curl/7.67.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

This is docker-compose.yml

version: "2"
services:
  app:
    container_name: app_test
    restart: always
    build: .
    ports:
      - "8080:8080"
      - "8443:8443"

This is Dockerfile

FROM node:10.12.0

WORKDIR /usr/src/app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 80 # I try 3000, 8080 instead 80

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

And This is result of docker ps

docker ps
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS              PORTS                                                    NAMES
735fba0a6ff5        IMAGE_NAME                  "node app.js"            46 seconds ago      Up 45 seconds       0.0.0.0:8080->8080/tcp, 80/tcp, 0.0.0.0:8443->8443/tcp   app_test

Neither curl nor browser runs. What should I do?

(I'm awkward because I'm not good at English. Please understand.)

version: "2"
services:
  app:
    container_name: app_test
    restart: always
    build: .
    ports:
      - "8080:3000"
      - "8443:8443"

If you want access your app on localhost:8080, this should work. A node app serves on port 3000 as standard.

So basically:

{port host machine} : {port it actually runs on inside your container}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM