By Hemanta Sundaray on 2022-07-10
In part-1, we successfully packaged our Node.js web application into a Docker image: node-docker.
Now, we will run this image inside of a container using the docker run command.
A container is a normal operating system process except that this process is isolated and has its own file system, its own networking, and its own isolated process tree separated from the host.
The docker run command has the following usage format:
docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
Execute the following command in your terminal:
docker run -d -p 8000:8000 –name rest-server node-docker
We can see that Docker started our container in the background and printed the container ID on the terminal.
We can make sure that the container is working properly using the following curl command:
curl localhost:8000
Alternatively, we can point our browser to localhost:8000 and see the desired output:
Remember that when we start our web server, it logs the text
"server listening on port". Now that our application is running inside a container, the log it produces will also be inside the container. We can fetch these logs using the docker logs command.
docker logs rest-server
To see the list of running containers on our machine, we can use the docker ps command.
We can stop a running container using the following command:
docker stop rest-server
Note that the docker ps command by default only shows running containers. We have stopped our rest-server container. To see the list of all containers including the ones that are stopped, we pass the -a flag to the docker ps command, as shown below:
docker ps -a
Let’s restart the container we just stopped.
docker restart rest-server
To remove a container, we simply run the docker run command, passing the container name. We can also pass multiple container names in one command.
docker rm rest-server
We can see all running processes inside a container using the docker top command:
docker top rest-server