#Docker port mapping
1 messages · Page 1 of 1 (latest)
@junior trail remove the ENV PORT line and see what happens
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?
//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
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
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
is it on a git repo by any chance? I could clone the whole thing
I believe so, let me double check it's the most up to date version
the one on github might not have the dockerfile
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 Did you get a chance to look at it yet? No worries if not