Some of the problems that we have faced in the industry of exposing our code, getting it deployed, is it works great in one environment and in another environment it doesn't work at all, or someone updated the server and it changed everything.
One of the solutions for distribution not being equal is Docker.
Docker gives you an image and it goes to that image (which is kind of like a virtual machine or container) and deploys from that container and exposes the URL through that container
Can build a Docker container for any server environment
- mkdir docker-explore
- cd docker-explore
- npm init
- npm install --save express body-parser
- touch Dockerfile
- touch server.js
const express = require('express');
const bodyParser = require('body-parser');
const server = express();
server.use(bodyParser.json());
server.get('/', (req, res) => {
res.json({ success: 'Hello world!' });
});
server.listen(3000, () => {
if (err) console.log(err);
console.log('server is running on 3000');
})
/* image you want to build off of (baseImage)
8.7 is the version of Node being used
Sometimes Docker can't work with latest version
Environment you'll build on */
FROM node:8.7
/* set working directory for any subsequent
add, copy, command -- entrypoint
working directory is the root
Where you'll start building this to */
WORKDIR /
/* at the root, copy package.json and package-lock.json files.
lock allows you to stay synchronized in your teams
copied to the root of the project
will be copied from the project to the docker container
Copy package.json/package-lock.json into root
COPY package.json package-lock.json ./ */
RUN npm install
/* wildcard of two dots
take everything given to you and copy it over to the container
Copy modules over into the image */
COPY . .
/* expose the port you're running from */
EXPOSE 3000
/* run npm start */
CMD("npm", "start")
"scripts": {
"start": "node server.js"
}
Make sure it's up and running
/* to build image */
docker build -t docker-explorer .
/* tell docker which port to expose */
docker run -p 49161:3000 -d docker-explorer
/* returns hash that you can use to run the docker exec command that allows you to look inside container */
docker exec -it ${hash}
ls
- Dockerfile
- bin
- boot
- dev
- etc
- home
- lib
- lib64
- home
- media
- mnt
- node_modules
- opt
- package-lock.json
- package.json
- proc
- root
- run
- sbin
- server.js
- srv
- sys
- tmp
- usr
- var
curl -X GET http://localhost:49161
Project on localhost:49161, but localhost:3000 not running
Docker is taking port 3000 and running its own port. Right now running locally, not online
With the use of Docker, you can safely say what it looks like on the local host, it will look like on the world wide web.