#Docker port mapping

1 messages · Page 1 of 1 (latest)

junior trail
#

I have a node.js webapp thing being built into an image, but when I run the container, the port mapping isn't working.

I'm using -p 127.0.0.1:3000:8080 when I run the container, I'm not sure why the site isn't showing up there

grim gyro
#

i think the right command is -p 3000:8080

#

no need to include localhost

red wedge
#

@junior trail remove the ENV PORT line and see what happens

novel carbon
#

Looks correct to me. The Docker docs say that the EXPOSE line in a Dockerfile is just for documentation, so the consumer of the image knows what ports are exposed. So the key thing here is that -p 127.0.0.1:3000:8080, which would map localhost:3000 to :8080 in the container. What's your Express code look like, @junior trail?

junior trail
# novel carbon Looks correct to me. The Docker docs say that the `EXPOSE` line in a Dockerfile ...
//Imports
const express = require('express');
const app = express();
const port = 3000;
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const db = mongoose.connection;
const bodyParser = require('body-parser');


const messageSchema = {
    name: {
        type: String,
        required: true
    },   
    email: {
        type: String,
        required: true
    },
    message: {
        type: String,
        required: true
    } 
}

const Message = mongoose.model("Message", messageSchema);

dotenv.config()

//Static files
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
// app.use('/css', express.static(__dirname + 'public/css'));
// app.use('/img', express.static(__dirname + 'public/img'));
// app.use('/js', express.static(__dirname + 'public/js'));

//Views
app.set('views', './views');
app.set('view engine', 'html');


//GET INDEX.HTML
app.get('', (req, res) => {
    res.sendFile(__dirname + '/views/index.html');
});

//POST METHOD
app.post('/submit', (req, res) => {

    let newMessage = new Message({
        name: req.body.name,
        email: req.body.email,
        message: req.body.message
    })

    newMessage.save();

    // db.collection('messages').insertOne(data, (err, collection) => {
    //     if (err) throw err;
    //     console.log("Record inserted Successfully");
    // });
    res.redirect("localhost:3000/");
});

mongoose.connect(process.env.DB_CONNECT);

//Listen
app.listen(port, () => {
    console.log(`listening on port ${port}`)
});

here's my app.js

sorry for the late reply, got caught up with school

novel carbon
#

Hmm... also looks correct to me, or at least I can't spot what's wrong right off the bat. Can you verify that it works when you run npm run start outside the docker image? You might have to comment out the Mongoose parts if you don't have a DB installed locally.

#

cc @junior trail

junior trail
#

just verified it works outside the docker image @novel carbon

#

my DB is atlas from mongoDB, so it just connects that way

#

the DB connection failed bc i'm not at a whitelisted IP but the page is working otherwise locally

#

I can send the actual file so it's easier to look at if that would help @novel carbon

novel carbon
#

is it on a git repo by any chance? I could clone the whole thing

junior trail
#

I believe so, let me double check it's the most up to date version

#

the one on github might not have the dockerfile

junior trail
#

also if you're knowledgable about frontend, my website looks terrible on all browsers besides chrome, I have a webkit but I guess I messed something up there @novel carbon

#

but one problem at a time lol

novel carbon
#

Alright I'll take a look once I get home from uni

#

Thanks

junior trail
#

@novel carbon Did you get a chance to look at it yet? No worries if not