I will skip "What/Why Docker?" part to make it straight to the point! 🤝
Let's see this in action!
I already have installed -
- NodeJS
- Docker
We will create a simple Node Server. We will use http
module from node and make a simple http server. server.js
file contains the code of our server-
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Node & Docker Running...");
});
server.listen(3333);
console.log("Node HTTP Server started at http://localhost:3333/");
If we run node server.js
on our terminal, it will start the server at 3333 port on localhost.
We can open a browser and hit http://localhost:3333/
and we can see server is sending the text Node & Docker Running....
Awesome 👏
Now, we want to create a docker image for our Node Server. To do this, we need to create a file named Dockerfile
in the root directory with the below commands-
FROM mhart/alpine-node
COPY server.js .
EXPOSE 3333
CMD node server.js
Here, I have used mhart/alpine-node
(Minimal Node.js Docker Image) for having NodeJS environment.
EXPOSE 3333
means - 3333
port is intended to be published.
Now that we have our Dockerfile
ready, we will build a Docker image from this file. We can run on the terminal-
docker build -t docknode .
*Here, docknode
is the image name. We can use any name.
After running, we can see some steps on our terminal.Like-
Here ^ 4 Steps are basically representing the 4 lines of the Dockerfile, which makes sense! 👌
To verify that, our image has been created successfully we can run the below command to get all the docker images-
docker images
Awesome, we can see our newly created image docknode
there 😎
I will run the docker image now which will make a container for us-
docker run -p 3333:3333 docknode
We will see Our Node server is running-
Now, if we open any Browser and hit http://localhost:3333
, we can see the server is running 🎉
If you haven't used Docker yet, I think, you should Try it in 2020!
I will write more on Docker in my future articles!
Till then,
Cheers!
👋
Top comments (4)
So that docker command line
docker run -p 3333:3333 docknode
is essentially saying map/expose the docker containers port 3333 to the local computers 3333?The left one is the localhost one.
We can give any unused port there like
4444:3333
Please read the docs: docs.docker.com/engine/reference/b...
expose does not expose the port for the outside world to reach
Thanks, 👍 I changed the wording.