

Photo by Author | Ideogram
. Introduction
Doctor By providing a permanent environment in different systems, we have made it easy to manufacture, ship and run. However, it comes with consistency trade: debugging is complicated by fraudulent people when your applications-including Azar’s applications-dowk containers are operating.
In the Doker, the new people of these people, debugging Azigar’s applications may feel like trying to fix the car with a hood welded shut. You know that something is wrong, but you can’t see what’s going on inside.
This early -friendly tutorial will teach you how to start with debugging in the dock.
. Why is debugging different in the Doker?
Before we drown, let’s understand why Dokar makes debugging difficult. When you are running locally on your machine, you can:
- See error messages immediately
- Edit the files and run them again
- Use your favorite debugging tools
- Check which files are available and what is in them
But when a doer runs inside a doker container, it is often difficult and less straight, especially if you are initially. Containers have its own file system, its environment, and its own running process.
. To set our example
Let’s start with a simple aggregate program with a bug. Don’t worry about the Doker yet; Let’s first understand who we are working with.
Create a file that says app.py
:
def calculate_sum(numbers):
total = 0
for num in numbers:
total += num
print(f"Adding {num}, total is now {total}")
return total
def main():
numbers = (1, 2, 3, 4, 5)
result = calculate_sum(numbers)
print(f"Final result: {result}")
# This line will cause our program to crash!
division_result = 10 / 0
print(f"Division result: {division_result}")
if __name__ == "__main__":
main()
If you run it in common python3 app.py
You will see that it does the correct calculation of money, but then the “division through zero” is mistakenly destroyed. Easy to place and fine, okay?
Now let’s see what happens when this simple application operates inside the doer container.
. To make your first docker container
We need to tell the Doker how to pack our program. Create a file which `Dockerfile`:
FROM python:3.11-slim
WORKDIR /app
COPY app.py .
CMD ("python3", "app.py")
Let me explain every line:
FROM python:3.11-slim
Start with a pre -created Linux system that has already been installedWORKDIR /app
Makes `/app` folder inside the container and sets it as a working directoryCOPY app.py .
Your copiesapp.py
File in the container from your computer to `/app` folderCMD ("python3", "app.py")
The Dokar tells which command runs when the container starts
Now let’s build and run this container:
docker build -t my-python-app .
docker run my-python-app
You will see an output with a mistake, but then the container stops and gets out. This leaves you to find out what has been wrong inside the isolated container.
. 1. Running interactive debugging sessions
The first debugging skill that you need is to learn how to go inside the container and check potential problems.
Instead of running your Ezar program immediately, let’s start the container and get the command prompt inside:
docker run -it my-python-app /bin/bash
Let me break these new flags:
-i
Mean “interactive” – it keeps the input stream open so you can type commands-t
Allocates a “pelvis-tati”-asylum, this terminal makes the work correctly/bin/bash
Over -rid the normal command and instead give you bush shell
Now that you have a terminal inside the container, you can run the command like this:
# See what directory you're in
pwd
# List files in the current directory
ls -la
# Look at your Python file
cat app.py
# Run your Python program
python3 app.py
You will also see the error:
root@fd1d0355b9e2:/app# python3 app.py
Adding 1, total is now 1
Adding 2, total is now 3
Adding 3, total is now 6
Adding 4, total is now 10
Adding 5, total is now 15
Final result: 15
Traceback (most recent call last):
File "/app/app.py", line 18, in
main()
File "/app/app.py", line 14, in main
division_result = 10 / 0
~~~^~~
ZeroDivisionError: division by zero
Now you can:
- Edit the file here in the container (though you will need to install the first editor)
- Discover the environment to understand what is different
- Check small pieces of interactive code
Fix the division with a zero error (maybe change `10 / 0` to 10 / 2`), save the file, and run again.
The problem is fixed. When you get out of the container, however, you lose the track of your changes. It brings us to our next technique.
. 2. Using volume for direct modification
Wouldn’t it be good if you could edit files on your computer and automatically display these changes within the container? This is exactly what the volume is increasing.
docker run -it -v $(pwd):/app my-python-app /bin/bash
Here is the new part -v $(pwd):/app
:
$(pwd)
Output the current directory path.:/app
Makes your current directory map/app
Within the container- Any file that you change on your computer immediately changes inside the container.
Now you can:
- Edit
app.py
On your computer using your favorite editor - Run inside the container, run
python3 app.py
To test your changes - Continue edit and test until it does not work
Here is a sample output here after changing the device 2:
root@3790528635bc:/app# python3 app.py
Adding 1, total is now 1
Adding 2, total is now 3
Adding 3, total is now 6
Adding 4, total is now 10
Adding 5, total is now 15
Final result: 15
Division result: 5.0
This is useful because you have to use your familiar editing environment on your computer and also within the container.
. 3. Connect the remote debugger from your IDE
If you are using such an integrated development environment (IDE) Vs. code Or PovertyYou can actually connect your IDE debuger directly to the code inside the Dokar container. This gives you the full strength of your IDE debugging tools.
Like your `Dockerfile`:
FROM python:3.11-slim
WORKDIR /app
# Install the remote debugging library
RUN pip install debugpy
COPY app.py .
# Expose the port that the debugger will use
EXPOSE 5678
# Start the program with debugger support
CMD ("python3", "-m", "debugpy", "--listen", "0.0.0.0:5678", "--wait-for-client", "app.py")
What does it do:
pip install debugpy
Installs Microsoft Debugepi LibraryEXPOSE 5678
The Doker tells that our container Port will use 5678.-
CMD
Our program starts through Debger, listening to Port 5678 for a connection. No change in your codes is required.
Create the container and run:
docker build -t my-python-app .
docker run -p 5678:5678 my-python-app
-p 5678:5678
Maps Port 5678 from inside the container to Port 5678 on your computer.
Now in the VS code, you can configure the Debug Configure (I .vscode/launch.json
) To connect the container:
{
"version": "0.2.0",
"configurations": (
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
}
)
}
When you start debugging in the VS code, it will connect your container, and you can set the brake points, inspect the variables, and step through the code as you will be with the local code.
. Ordinary debugging problems and solutions
⚠ ⚠ “My program works on my computer but not in the Dokar”
This usually means that there is a difference in the environment. Check:
- Differences of the version.
- The dependence is missing.
- Different routes to the file.
- Environmental variables.
- File permission
⚠ ⚠ “I can’t see my print statements”
- Use
python -u
Avoid output buffering. - Make sure you are running
-it
If you want interactive output. - Check if your program is actually running according to intentions (maybe it’s getting out quickly).
⚠ ⚠ “My changes are not appearing”
- Make sure you are using volume growing (
-v
, - Check if you are editing the correct file.
- Confirm that the file has been copied to the container.
⚠ ⚠ “The container immediately. Exit goes out”
- Run with
/bin/bash
Inspection of the container state. - Check out error messages
docker logs container_name
. - Make sure you
CMD
Duke file is correct.
. Conclusion
Now you have a basic toll cut to debug in the Doker:
- Interactive shells (
docker run -it ... /bin/bash
) Looking and quick fixes. - Growing volume (
-v $(pwd):/app
) Editing your local file system. - Remote debugging to use your IDE’s full capabilities
After that, you can try to use Doker Compos to manage complex applications. For now, start with these simple techniques. Most debugging problems can only be solved by going inside the container and rotating around.
The key procedure is to be: Understand what’s going on, find out what is actually happening, and then eliminate the distance between the two. Happy debugging!
Pray Ca Is a developer and technical author from India. She likes to work at the intersection of mathematics, programming, data science, and content creation. The fields of interest and expertise include dupas, data science, and natural language processing. She enjoys reading, writing, coding and coffee! Currently, they are working with the developer community to learn and share their knowledge with the developer community by writing a lesson, how to guide, feed and more. The above resources review and coding also engages lessons.