0

I'm new to Docker and following a tutorial on Udemy.com. The turtorial has me setting and running a very simple container.

Docker File

# Specify a base image
FROM node:alpine

# Putting files into a specific directory
WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm", "start"]

Index.js

const express = require('express');

const app = express();

app.get('/', (req, res) => {
    res.send('Hi there');
});

app.listen(5000, () => {
    console.log('Listening on port 5000');
});

package.json

{
    "dependencies": {
        "express": "*"
    },
    "scripts": {
        "start": "node index.js"
    }
}

I'm working on a laptop running Windows 10 Home. Using the Docker Quickstart Terminal and PowerShell.

I've edited my HOSTS file as to point localhost to 192.168.99.100, which is the ip the container gets.

I've tried hitting the server by both 192.168.99.100:5000 and localhost:5000. No matter what, when I try to access via browser(I.E. and Chrome) I get the following message:

This site can’t be reached

192.168.99.100 refused to connect.


curl http://192.168.99.100:5000

returns

curl: (7) Failed to connect to 192.168.99.100 port 5000: Connection refused


However, If I run 'docker-machine ls', I get:

tcp://192.168.99.100:2376

Why port 2376? I cannot figure out where/how this port is being assigned.

Trying the browser on that port I get a different message:

This page isn’t working

192.168.99.100 sent an invalid response.

ERR_INVALID_HTTP_RESPONSE


This would suggest(at least to me) that the server is there on that port, but not doing something right.

curl http://192.168.99.100:2376 --output -

Gives me some garbled output in the terminal, and writing it to a file gives garbled output as well. Not much, just a few garbage characters.

I suspect this is the "Hi There" message that is not being formatted correctly.

So my questions would be, am I doing something wrong in my dockerfile? Why/how is the container getting this port 2376 when i'm specifically mapping to 5000?

Any insight would be appreciated.

1
  • You need to expose the ports either by adding them in your dockerfile or add arguments while starting container. Look at deosha's answer.
    – KatariaA
    Mar 26, 2019 at 13:38

2 Answers 2

1

May be you are using bridge network mode. In that case, you need to bind your host port to container port to use same port. So when you execute docker run, you may need to add -p 5000:5000 as a flag to docker run command. Also I don't see any expose in Dockerfile. So you may want to add EXPOSE 5000 in Dockerfile just above cmd.

1
0

Containers have to expose ports

A container will not listen to outside traffic unless the Dockerfile has an EXPOSE 5000 command. This tells the container to listen for external traffic on that port.

That being said, the Docker engine will only listen to external ports that you tell it to. By default that is none. Setting EXPOSE 5000 means that the container will listen to external traffic that the docker engine routes to it. By default this means only internal docker engine traffic will be able to access that port.

Expose the port to the host

Depending on how you are starting the container (either cli or docker-compose), you will need to expose the port to the host. From CLI, this will be done with the -p flag in the format -p hostPort:containerPort. You can then map the port as you wish, like -p 50000:5000 with 5000 being the same port in your EXPOSE command in the Docker file and 50000 being the localhost port you want to use.

Putting it together

Update your docker file to something like this:

# Specify a base image
FROM node:alpine
...

EXPOSE 5000

# Default command
CMD ["npm", "start"]

Then run it with:

docker run --rm imageName -p 5000:5000

From there you should be able to curl http://localhost:5000 and get the expected response.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.