Docker for Beginners

Docker for Beginners

What is Docker?

Docker is an open-source platform for containerizing applications. It allows you to package applications into containers. The software that hosts the containers is called Docker Engine. At first docker was released in 2013 and it was developed by Docker, Inc.


What is a Docker Container?

A Docker container is a lightweight, standalone, and executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings. Containers are designed to run consistently across different computing environments, from development to production, and from a developer's laptop to a cloud server.


Importance of Docker

1. Consistency: Docker ensures that the software will run the same way in development, testing, and production environments.

2. Scalability: Easily scale applications by adding more containers.

3. Resource Efficiency: Containers use fewer resources than traditional virtual machines.

4. DevOps Integration: Docker is essential for CI/CD, enabling automated testing and deployment.


Installing Docker

Docker Desktop simplifies installation on Windows and macOS. It includes Docker Engine, Docker CLI, Docker Compose, and other tools.

On Linux, you can install Docker Engine using package managers like apt for Ubuntu or yum for CentOS.

Installation Steps: Download the Docker Desktop, follow the installation wizard, and verify the installation using docker --version

How to use Docker with CLI or VsCode

Here is a simple node.js application in VsCode, we are going to use the terminal to execute docker commands here, we can use CLI also to do the same. It is a simple application that will be running on PORT 3003.

To make an image from this app, I have to add a file name "Dockerfile" without any extension, and have to add a few commands (I will explain line by line later), in my case, the file is like this,

FROM node:20 this line means this image will download and use node image version 20 from the Docker Hub (Docker Hub is a cloud-based registry service for storing, sharing, and managing Docker container images).

WORKDIR /node-app will create a directory (if not exist) in the container's filesystem. Then it sets the working directory to /node-app. This means that any RUN, CMD, ENTRYPOINT, COPY, or ADD instructions that follow will be executed in the context of this directory.

COPY package.json . this command will copy the package.json file in the node-app folder.

RUN npm install this will install the necessary node modules as per the package.json file.

COPY . . this will copy the rest of the files of this project to the node-app folder.

EXPOSE 3003 instructs Docker to expose port 3003 from the container to the outside world, allowing other services or containers to connect to applications running on that port.

CMD ["node", "app.js"] tells Docker to execute the Node.js application named app.js using the node runtime within the container.


Building and Running the Docker Image

Now create a docker image by the command docker build, you can add a TAG for the image by -t, for example, docker build -t "dockernodeapp01" .

It will be doing something like this in the terminal or CLI, and build an image with the tag given by your command. To see that type docker images, this command will show all built docker images of your system.

Now, we will create the container that will run our application by this image. To do so, we will execute the following code, where we are setting the name of our container, set the container port to our local port and use the image name "dockernodeapp01", we can use the image id "8b132e976b3d" instead of the name.

docker run -d --name NodeApp3003 -p 3003:3003 dockernodeapp01

After executing this command you can check the running container by using the command docker ps

Here it shows that the container was created and running on port 3003, Let us now browse the application

So, that is how it is working from the container.

Rony Barua

SQA Lead at IT Magnet | ISTQB | CEH | CHFI | QA Automation | API Test | Talent Acquisition | Teacher | Mentor

9 个月

Nice

要查看或添加评论,请登录

Kartik Chandra Biswas的更多文章

社区洞察

其他会员也浏览了