A few opening words
Following the topic of Docker for Newbie, now I would like to instruct you to use the practice of using Docker to run a webserver using NodeJS. I chose NodeJS because it is a popular language today, if you have any difficulties with your language please comment this article I will try to answer if possible.
Start
Think dockerize direction
Back to the previous series, I introduced you the concepts of Docker Images and Docker Container. So to be able to use yourself let’s revolutionize the thought first. Suppose, now I have 1 source code NodeJS and then how to run. First, you need to create a virtual machine running ubuntu, then install NodeJS and then copy the code into it, then install the ostrich-type dependencies and run the node index.js
right? Here’s how you guys don’t have a docker or do that on the server. Okay, so now I will think of the docker more (I call dockerize). First I have 1 images already running NodeJS already (The nature of images running NodeJS is also from debian images and then cooking, installing nodejs and npm), then copy the code, install the dependency and then the node index.js
If you look at the number of ways you see to reduce each one must install NodeJS only When I thought about it, I would rather install it on the computer than install the big docker. If you think further with docker, you can fix the NodeJS version in the file and just run one command, this will minimize the time for subsequent runs (because just run the container up) and minimize. error next time (Because it runs under a configuration file from available)
Well, no more trouble, let’s configure a docker file running NodeJS server with express.js
Practice it
Create project with Dockerfile
First of all, I created the package.json file and installed the express
package
1 2 3 | yarn init -y <span class="token comment"># Tạo package.json</span> yarn add express <span class="token comment"># Cài express</span> |
For anyone who doesn’t know how to use NodeJS, package.json is like ruby gemfile
or python’s requirement.txt
.
Next, I index.js
with the following content
1 2 3 4 5 | <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">"express"</span> <span class="token punctuation">)</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">const</span> <span class="token constant">PORT</span> <span class="token operator">=</span> <span class="token number">3000</span> <span class="token punctuation">;</span> app <span class="token punctuation">.</span> <span class="token function">listen</span> <span class="token punctuation">(</span> <span class="token constant">PORT</span> <span class="token punctuation">,</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"Server is running"</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> app <span class="token punctuation">.</span> <span class="token keyword">get</span> <span class="token punctuation">(</span> <span class="token string">"/"</span> <span class="token punctuation">,</span> <span class="token punctuation">(</span> req <span class="token punctuation">,</span> res <span class="token punctuation">)</span> <span class="token operator">=></span> res <span class="token punctuation">.</span> <span class="token function">send</span> <span class="token punctuation">(</span> <span class="token string">"hello world"</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
In other languages, create the corresponding files to build the server, and try again with curl as shown below
Next, put it into the docker, you create Dockerfile with the following command
1 2 | <span class="token function">touch</span> Dockerfile |
First of all, on the first line of Dockerfile
, we have to show which image we want to build from (Here is my node ) Here I use the lts
version
1 2 3 | <span class="token comment"># Choose images</span> <span class="token keyword">FROM</span> node <span class="token punctuation">:</span> lts |
In the next line we will declare the path of our code in the container, we will put in /usr/src/app
1 2 3 | <span class="token comment"># Create app directory</span> <span class="token keyword">WORKDIR</span> /usr/src/app |
In the next line we will copy the entire source of the project into the image and download the dependencies
1 2 3 | <span class="token comment"># Copy source code </span> <span class="token keyword">COPY</span> . . |
- Sign
.
The first is the path to your file on your computer, I’ll put it as a dot because I’ll get the entire file in the same context as Dockerfile. - Sign
.
The second isusr/src/app
. If you do not declareWORKDIR
, you will have to point to the container code location
If the language you need requires installation of packages, please add them to the line
1 2 | <span class="token keyword">RUN</span> npm install |
For some languages, there is a folder package right inside the project folder, like NodeJS’s node_modules, create a .dockerignore file and ignore node_modeles, like .gitignore.
And finally add the following line to run the server
1 2 | <span class="token keyword">CMD</span> <span class="token punctuation">[</span> <span class="token string">"node"</span> <span class="token punctuation">,</span> <span class="token string">"index.js"</span> <span class="token punctuation">]</span> |
If you are npm install
then npm install
or node index.js
are normal bash commands, so why not use RUN node index.js
or CMD npm install
That’s the wallet due to the difference in the meaning of the commands. That is as follows
RUN
: execute the command (s) in a new layer and create a new container. For example, it is often used to install software packages.CMD
: set command and / or set default parameters in dockerfile, this command or default parameters can be overwritten from the command line when docker container runs.ENTRYPOINT
: Configure a container to run as an executable
Refer here
There are also many more interesting things in Dockerfile
you can refer here
Build dockerfile
We use the command to build from Dockerfile to images
1 2 | docker build -f Dockerfile -t docker-express <span class="token keyword">.</span> |
Explain:
-f Tên_file
: You can create files likeDockerfile.development
. With-f
you can customize the file name-t Tên_images_mới
: Here I am building images with the namedocker-express
. In practice people will also use tags likelastest
anddevelopment
by adding:
and the tag name after images likedocker-express:latest
.
: Is the docker’s context. You can imply that the directory contains Dockerfile for simplicity
To verify that images have been created, use the following command in Terminal
1 2 | docker images |
Running containers with docker images
Use the following command
1 2 | docker run --rm -d -p 3000:3000 --name <span class="token function">test</span> docker-express |
Explain:
--rm
: Usually when you stop docker, that docker will be in a stop state, not completely terminated. Adding this params will help terminate the stop-d
: Help the container run in the background-pl:c
:l
is the port of your computer andc
is the port of the container. This params will have a map port effect. If you use80:3000
, the reverse port 3000 of the container will be port 80 of the computer--name tên_container
: name the container to help you easily manipulatedocker-express
: name images above
After running we will get a string as follows e7f5f8b86ee1b4e7a16ef38cc24547c598c9aaafccd1de23efeb668ec33a6c0d
, this is the container’s ID to interact, you can manipulate the container by 7-8 characters of this string (usually I copy randomly As much as possible) or container name
Some useful commands with docker
In addition to run
and build
we can use some of the following commands. For the name
field can be replaced by the id
as noted above
List of running containers
1 2 | docker <span class="token function">ps</span> |
Use the -a
parameter to see stopped containers. Containers running with the --rm
container parameter will be deleted immediately after stopping
Stop 1 container
1 2 | docker stop name |
Start again a container has stopped
1 2 | docker start name |
Delete completely 1 container
1 2 3 | docker <span class="token function">rm</span> name <span class="token comment"># Có thể thêm tham số -f với các container đang ch</span> |
Delete completely 1 container
1 2 3 | docker <span class="token function">rm</span> name <span class="token comment"># Có thể thêm tham số -f với các container đang ch</span> |
Inspect 1 container
1 2 | docker inspect name |
Stop or delete all containers (For Windows used on Powershell)
1 2 | docker <span class="token function">rm</span> -f <span class="token variable"><span class="token variable">$(</span> docker <span class="token function">ps</span> -aq <span class="token variable">)</span></span> |
List of images
1 2 | docker images |
Completely delete 1 image
1 2 | docker rmi name |
Stop or delete all containers (For Windows used on Powershell)
1 2 | docker rmi <span class="token variable"><span class="token variable">$(</span> docker images -q <span class="token variable">)</span></span> |
For images, you need to delete the containers that are running with those images
In the following article, I will show you how to run many containers with Docker compose and practice CRUD with NodeJS