Setting up Docker and debugging inside the Docker container — System Design (part 3)

Chetan Dwarkani
6 min readJan 12, 2022

--

Topic which we are going to discuss in this blog:

1. What is Docker
2. Implementing Docker setup in your local system
3. Debugging inside Docker container

If you prefer seeing the video rather than a blog post then please checkout the below link which I made

What is Docker?

In this blog, I am going to help you understand what’s Docker in simple terms and also help you in running the whole docker setup in your local system.

Well, if you have never used docker then you would be doing the deployment in the following way — Buy a server, set up all environments linked to our project such as downloading git, downloading JAVA(maybe if your project uses), setting up your API framework (say Node.js), set up the system environment variables necessary for your project and then finally running the service.

Still, you would get a boooom! Looks like you are getting a dependency error and you need to configure and install things properly.

Here’s where Docker comes to the rescue.

Docker helps you to host an environment on top of your operating system by the use of the docker daemon service which creates a layer where containers run. The container acts as a separate virtual environment on top of the existing OS. A container in a docker helps to define its boundaries having its memory and storage space. It acts like a simple virtual environment on top of system OS having its own space and all environment setups inside it. The below pic will help you to understand a bit more about what it is

As you can see in the above diagram VM architecture runs on top of the environment and it requires its own hardware space and separate OS to run and because of that reason, it’s quite slow compared to hosting and deploying docker apps. On the other hand, Docker runs on top of our existing OS with the help of the Docker engine and it is comparatively much faster to host, run and deploy docker apps.

Docker brought an enormous impact in the industry where we faced a lot of config setup problems. Imagine you are trying to set up a library that has a lot of other dependencies to be installed(such as adding JDE, JREor say python, etc.,). You need to google and find them out one after the other ( Since most of the software documentation writers are lazy to write clearly) and a simple software setup that could be completed in an hour could go to days. Docker simplified this problem and made an amazing impact. Today, all the leading companies of the world prefer using Docker to run their instance of services. It solves another great problem of horizontal scaling which I will demonstrate further in the below steps.

Let’s dive into implementing it.

Step 1: Download Docker:

Docker can be downloaded and installed via their website — https://www.docker.com/products/docker-desktop. It has simple setup steps and once you install it in your system, opening it will get you to the below screen

Step 2: Project setup:

Now the next step is to dockerize our project. I’ll use a very simple hello world app which we made in our previous blog ( please read Step 2: Run the backend service on a specific port from this https://m-chetandwarkani.medium.com/setting-up-load-balancer-system-design-e29ed2b4a957). Let’s start writing Dockerfile which is the core file responsible to create an image.

Create a file in the same project and name it as Dockerfile and write the below script

The above file says that we need to run node 16 framework inside a container and work dir is mentioned. Also, we need to copy all package.json files from the work dir. Once that is done, do an npm install and expose the port 8090 inside the container. Finally, run our service in the port as mentioned in the app.js file. — inspect=0.0.0.0:9231 is used to tell the container to listen to port 9231 for the debugger to be attached.

This should be created inside the project and after that, we need to do build the image using the below command

docker build -t simple-docker .

This will build the image of your project and after doing so we can simply go to our Docker desktop and you’ll find your image created there under the images tag

Step 3: Running image inside the container:

Now, you can either type the below command

docker run -d -p 8090:8090 simple-docker

which says to expose the port 8090 in a container and also serve the request in our system on 8090

or else you can simply hover on the image and click on the play option from docker desktop setup

Hover on the image and click on the run button and there you go — your first docker project is done. Your service will start running on port 8090 as you have mentioned in your script file

You can check and verify if it’s working by going to the browser and typing the localhost:8090 since we have hosted it in that port and you’ll see the below screen

Step 4: Debugging inside container:

Create a file by name docker-compose.yml and add the below lines of code

Docker-compose yml files are the way to manage complete docker configurations. We can add all the necessary configs inside the docker-compose file such as which image to create and on which ports to run the container as well as all other configs such as starting multiple containers etc.,

We will use it simply to tell docker that we need to mount our current project into the container and then run it on port 8090 as specified in the above file. 9231 port is added to tell container to wait for the debugger to be attached.

Next, you need to install vs code if you are not having one and in create a launch.json file in the same repo and add the below script

Then next thing is to type the below command

docker-compose up — build

You’ll end up in below screen

Then click on debug button on left panel of vs code and your debugger will get attached.

You can watch the video where I am doing all these steps right on my laptop in a step by step manner.

Thanks for giving it a read.

If you wish to learn about how to setup load balancer then watch the video I uploaded — https://www.youtube.com/watch?v=MRJZZ28kNM4

--

--

Chetan Dwarkani

Tech experience in System Design | Java Script Framework | Android framework | ReactJS | JAVA | DB Design | chetandwarkani.com