Tutorial
I have already created a sample project. It contains a basic folder structure based on Total.js documentation and a Dockerfile.
Docker can build images automatically by reading the instructions from a
Dockerfile
. ADockerfile
is a text document that contains all the commands a user could call on the command line to assemble an image. Usingdocker build
users can create an automated build that executes several command-line instructions in succession.
How to write its Dockerfile
In the below section I will explain instructions given in Dockerfile.
Dockerfile explanation
FROM node:10
This instruction sets the Base Image for the subsequent instruction.
In the above example, I am using node:10 base image because it contains packages related to a node already installed. So, therefore I don’t have to install node in the base image again.
WORKDIR /usr/src/sampleApp
WORKDIR instruction sets the working directory for subsequent instructions.
It can be changed as needed. If the directory specified in WORKDIR doesn’t exist it will be created.
COPY . .
The COPY instruction copies files or directories from the source directory and adds them to the filesystem of the container at the path specified in the destination.
In the above example, I copying the files and folders from src directory to the dest directory specified in the above command.
RUN npm install
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
In the above example, I am installing all the packages specified in the package.json file.
CMD mv config.example config && mv release.js.example release.js && node release.js
CMD is used to execute commands inside docker container
In the above example, some files are being renamed and finally starting the Total.js application server in release mode.
Testing
In this section, I will explain how to build a docker image using the Dockerfile and test the application.
sudo docker build -t sampleApp:<image-tag> -f Dockerfile .
This command will build a docker image based on the instructions given in Dockerfile.
sudo docker run sampleApp:<image-tag>
This command will run the docker image build above as a container. Once the image is run as a container. It will show the ip and port on which the server is running.
This sums up the tutorial of dockerizing Total.js based application.