Docker for Newbie: Part 3 – Introducing Dockerfile and some commands

Tram Ho

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

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

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

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

In the next line we will declare the path of our code in the container, we will put in /usr/src/app

In the next line we will copy the entire source of the project into the image and download the dependencies

  • 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 is usr/src/app . If you do not declare WORKDIR , 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

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

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

Explain:

  • -f Tên_file : You can create files like Dockerfile.development . With -f you can customize the file name
  • -t Tên_images_mới : Here I am building images with the name docker-express . In practice people will also use tags like lastest and development by adding : and the tag name after images like docker-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

Running containers with docker images

Use the following command

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 and c is the port of the container. This params will have a map port effect. If you use 80: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 manipulate
  • docker-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

Use the -a parameter to see stopped containers. Containers running with the --rm container parameter will be deleted immediately after stopping

Stop 1 container

Start again a container has stopped

Delete completely 1 container

Delete completely 1 container

Inspect 1 container

Stop or delete all containers (For Windows used on Powershell)

List of images

Completely delete 1 image

Stop or delete all containers (For Windows used on Powershell)

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 ?

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo