mfrachet
mfrachet

Reputation: 8922

Docker run, no way to access application

I m building an Express (nodejs) application that runs on the port 3000, with a simple hello world, and hoted to a public github repository.

Actually, it works great and the code simply looks like this :

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

I m studying docker and I want to make this little code work inside of a container, so I created this dockerfile :

FROM phusion/baseimage:0.9.17

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

# Install corresponding packages
# Git, pm2, curl
RUN apt-get update && apt-get install -y \
  git \
  curl

# Downloading NodeJs v0.12
RUN curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
RUN apt-get install -y nodejs

# Cloning repository
RUN git clone https://github.com/User/hello-world.git

# Install global dependencies
RUN npm install -g pm2

# Move inside of the project
RUN cd hello-world && npm install  && pm2 start app.js


# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Then I run these suit of command :

docker build -t hello-world
docker run -p 3000:3000

After this (and all is successful), when I try to access http://localhost:3000/ , The page is not found.

I need some help to understand my mistakes.

Thanks for all

Upvotes: 1

Views: 96

Answers (2)

user1803291
user1803291

Reputation: 355

Depending on your docker installation, it might not be available on localhost. My installation is for instance not available on 127.0.0.1, but on 192.168.99.100.

The following hostsentry:

192.168.99.100 dockerhost

would allow me to access your app on http://dockerhost:3000/

Hope that helps :-)

Upvotes: 0

riser101
riser101

Reputation: 650

You should log-in to your running container:

docker exec -it <container-id> /bin/bash

After getting inside the container, you can run your app there and debug directly inside the container.

something like:

cd your-directory/hello-world

Then:

node app.js

The console will spit out errors like your local development environment.

Upvotes: 1

Related Questions