DEV Community

Dharamraj Yadav
Dharamraj Yadav

Posted on

Learning Docker: Dockerfiles, .dockerignore, and Port Mapping 🐳

Hey everyone! Here’s a quick update on what I learned last week. I finally got hands-on with two essential Docker files: the Dockerfile and .dockerignore.

First up, let's talk about the .dockerignore file. If you've used Git, this works exactly like .gitignore. When we build an image, this file prevents Docker from copying over unnecessary files and folders, which helps keep our final image clean and lightweight.
Here is my setup:

Next, I created the Dockerfile. This is basically the instruction manual for your project. We write down all the necessary commands in here, and Docker uses this file as the blueprint to build our custom image.
Take a look at the code:

After building the image, it was time to run it. This is where I learned how port mapping works. Port mapping is simply telling the Docker container, "Hey, forward your internal project port to this specific port on my local system."

In my setup below, I mapped the ports so my project runs on port 5173 inside the container's terminal, but I can actually access it in my local browser using port 3200.
Check out the terminal and browser screenshots:

Top comments (0)