Docker is a tool that help us to work with the containers. We can pull the images from it's public docker hub repo and run the images in our machine to work with softwares like nodejs, mongodb, linux which otherwise needs to be installed on our Operating System manually.
Containers are the isolated environments in your system that runs on the top of your systems infrastructure but are in isolation. Containers itself doesn't contain the full operating system but rather it contains some binaries. Docker sits in between our machine Operating system and the Container to manage how we use the our computer resources.
to share the container we create the Images of our container which can be replicted on other machine.
Images can also be created from Dockerfile.
Image contain set of instruction about how to create the container. Think this as image is an Class and Container is it's Object/Instance.
Docker follows Client Server architecture under the hood. So there are three things :
- docker cli <- This is our client
- docker daemon <- This is the server
- REST API <- This sets communication between cli commands and the daemon.
docker --version: To know the version of the dockerdocker ps: To list the running containersdocker ps -a: To list all the containers running/stopped.
docker run ubuntu:latest: This will start running our ubuntu container, replace latest withversionnumber if required, if the iamge is not present it will look in the container registry and then download it.docker run -it ubuntu:latest /bin/bash: start container while opening an interactive terminal session inside the container.docker run -d ubuntu: Run the container in detached mode.docker run -dit --name my-ubuntu ubuntu:latest: If you want the container to stay alive silently in the background so you can connect to it later, combine detached (-d) and interactive flagsdocker exec -it my-ubuntu bash: if you started the container with previous command then you can enter into it with this command. container when running we use theexeccommand.
docker pull node:<version>: To pull the image from remote registrydocker stop containerId: stop the running container.docker container prune: delete all the stopped containers.
We can run them one by one too.
docker pull node:22-alpine: Ran the container from local image, otherwise pull from the docker hub.docker create node:22-alpine: Creates a new writeable container layer over the specified image and prepares its execution configuration.docker start <container>: Boots up the newly created container, assigns it a process ID, and executes its main startup application or custom script we can attach the flags to these commands to mimic behaviour ofdocker run
๐ Real-Time Code Syncing with Docker Bind Mounts
This guide covers how to link a local folder on your host machine to a running Docker container. This allows you to write code in your local text editor (like VS Code) and have it execute instantly inside a containerized environment without losing data.
๐ Part 1: Initializing the Local Environment
1. Create a Project Folder
Open your local terminal (Git Bash, PowerShell, or CMD) and create your workspace:
mkdir docker-node-project cd docker-node-project2. The Mounting Command (Terminal Shortcuts)
To link your folder, you must spin up the container using a Bind Mount (
-v) and a Working Directory (-w) flag. Choose the command that matches your terminal:/to stop path scrambling)docker run -dit --name local-node-env -p 8080:3000 -v /"\$(pwd)":/app -w /app node:alpinedocker run -dit --name local-node-env -p 8080:3000 -v %cd%:/app -w /app node:alpine๐ก Alternative: Using Hardcoded Absolute Paths
Instead of relying on terminal variables like
$(pwd)or%cd%, you can explicitly type out the exact absolute path to your folder. This works reliably across different terminal windows:docker run -dit \ --name local-node-env \ -p 8080:3000 \ -v "C:\Users\Lenovo\Desktop\docker-node-project":/app \ -w /app \ node:alpine(Ensure you wrap the Windows path in quotes if your folder names contain spaces!)
๐๏ธ Part 2: Terminal Discipline (Where to Run Commands)
When using bind mounts, your local computer folder and the container folder are a single, synced space. However, your laptop and the container have completely different operating systems and engines.
You must be disciplined about where you type your commands:
Ctrl + S, writing JS)vi.npm installdocker exec)Invalid Win32 applicationerrors.npm startdocker exec)-pport mapping.๐ Part 3: Writing Code and Syncing Files
1. Create files locally
Open the
docker-node-projectfolder in VS Code on your laptop and create these two files:package.json{ "name": "docker-mount-test", "version": "1.0.0", "main": "server.js", "scripts": { "start": "nodemon -L server.js" }, "dependencies": { "express": "^4.19.2" }, "devDependencies": { "nodemon": "^3.1.0" } }server.js2. Execute within the Container Environment
Following the discipline rules above, open an execution session into your container to install and run the stack:
(Check your laptop folderโthe
node_modulesfolder generated inside the container has successfully synced backward to your local Windows computer filesystem!)๐ ๏ธ Part 4: Overcoming the Windows File-System Trap
The Problem: Nodemon doesn't reload on file save
When you save a file in Windows, the underlying Linux filesystem inside the container does not always receive the file-change notification event across the Docker Desktop file-sharing bridge. Standard
nodemonwill sit idle and miss your code changes.The Fix: Legacy Watch (Polling)
You must force Nodemon to actively scan and poll the filesystem for changes by adding the
-L(or--legacy-watch) flag.Ensure your
package.jsonstart script uses the flag:Alternatively, trigger it manually inside the container shell using:
With polling enabled, saving your
server.jsin VS Code will now instantly trigger a server restart inside the container. You only need to manually refresh your browser athttp://localhost:8080to see updates.