Hello Docker
Let’s have a simple NodeJS "Hello World" web service that listens on port 8088 and greets you on GET request.
Example NodeJS app⌗
I have a NodeJS application located in <workspace>/hello-docker.
The NodeJS package is described by a following package.json
created by calling npm init
and then installing the express
dependent library:
There is also a corresponding package-lock.json
file created.
Now, we would like to run this service in a Docker container. What do we do? What do we need?
Creating a Dockerfile⌗
Docker container is described by Dockerfile. It is basically a cookbook for containerizing the project. We typically specify:
- Which container is used as base (NodeJS in our case).
- What files should be copied from the local machine and where to in the container.
- What commands should be run inside the container (preparation, installation, ...).
- What TCP/UDP ports should be exposed outside the container.
- And many more - see the Dockerfile Reference
Here is the Dockerfile for the hello-docker project
Building and Running the Container⌗
To build the container, go to the directory where the Dockerfile is located and run
This will build a container with tag korimsoft/hello-docker
.
And now, finally, we can run the container with
To prove that the application works, either open the web browser on localhost:8080
or call curl localhost:8080
from the command line.
You should get your Hello.
Docker run, again, has a lot of parameters that could (and should) be tinkered around with. See its documentation