Skip to content

Commit 231dea2

Browse files
committed
Added docker learnings and updated readme.md
1 parent e47c05b commit 231dea2

24 files changed

Lines changed: 18969 additions & 3 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@
55
.terraform.lock.hcl
66
terraform.tfstate
77
terraform.tfstate.backup
8+
node_modules
9+
npm-debug.log
10+
.env
11+
.vscode
12+
*.iml
13+
.idea
14+
*.log
815

docker-zero-to-hero/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:18-alpine AS installer
2+
WORKDIR /app
3+
COPY package*.json ./
4+
RUN npm install
5+
COPY . .
6+
RUN npm run build
7+
8+
FROM nginx:latest AS deployer
9+
COPY --from=installer /app/build /usr/share/nginx/html

docker-zero-to-hero/README.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ There are three important components:
4444

4545
### Docker Daemon
4646

47-
### Docker Daemon
48-
4947
The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.
5048

5149
### Docker Client
@@ -239,12 +237,14 @@ COPY --from=installer /app/build /usr/share/nginx/html
239237
### Build and Run
240238

241239
```bash
242-
docker build -t multistage .
240+
docker build --load -t multistage .
243241
docker images
244242
docker run -it -dp 3000:80 multistage
245243
docker ps
246244
```
247245

246+
**Note:** NGINX listens on port 80 inside the container. The `docker run -p 3000:80` option maps container port 80 to host port 3000. You can access the container at `localhost:3000`, even without `EXPOSE 3000` in your Dockerfile.
247+
248248
### How Docker Works Internally
249249

250250
1. When you run `docker build`, Docker reads the `Dockerfile` line by line and uses your current folder as the build context.
@@ -275,6 +275,85 @@ docker ps
275275

276276
14. When you push an image, Docker checks which layers are already in the registry. It only uploads what's missing.
277277

278+
### Docker Networking
279+
280+
Networking allows containers to communicate with each other and with the host system. Containers run isolated from the host system and need a way to communicate with each other and with the host system.
281+
282+
Containers have networking enabled by default, and they can make outgoing connections.
283+
284+
**Bridge:** The default network driver. If you don't specify a driver, this is the type of network you are creating. Bridge networks are commonly used when your application runs in a container that needs to communicate with other containers on the same host.
285+
286+
**Host:** Removes network isolation between the container and the Docker host, and uses the host's networking directly. (Not supported on macOS/Windows)
287+
288+
**None:** Completely isolates a container from the host and other containers. `none` is not available for Swarm services.
289+
290+
**Overlay:** Overlay networks connect multiple Docker daemons together and enable Swarm services and containers to communicate across nodes. This strategy removes the need to do OS-level routing.
291+
292+
**Macvlan:** Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses. Using the macvlan driver is sometimes the best choice when dealing with legacy applications that expect to be directly connected to the physical network, rather than routed through the Docker host's network stack.
293+
294+
#### Example Commands
295+
296+
```bash
297+
# Start a busybox container as root
298+
docker run -itd --user root busybox sh
299+
docker exec -it <container_id> sh
300+
301+
> **Note:** While ping inside container i.e `ping google.com` If you get 'ping: permission denied', use --privileged as below.
302+
303+
docker run -itd --privileged busybox sh
304+
docker exec -it <container_id> sh
305+
306+
# List networks
307+
docker network ls
308+
# Inspect the bridge network
309+
docker network inspect bridge
310+
# Create a bridge network
311+
docker network create my-bridge
312+
# Run containers on the custom bridge network
313+
docker run --network my-bridge --name container1 -d nginx
314+
docker run --network my-bridge --name container2 -d alpine sleep 3600
315+
docker exec -it container2 ping container1
316+
317+
# Run with host network (Linux only)
318+
docker run --network host -d nginx
319+
```
320+
321+
> **Note:** On macOS and Windows, the `--network host` option is not supported because Docker Desktop uses a virtualized environment and does not provide the host network driver. This option only works natively on Linux. When you run `docker run --network host ...` on macOS or Windows, the container will exit or fail because it cannot use the host network mode.
322+
323+
### Docker Compose
324+
325+
Docker Compose is a tool for defining and running multi-container applications. It streamlines development and deployment by allowing you to manage services in a single YAML file.
326+
327+
**Why use Compose?**
328+
329+
- **Simplified control:** Define and manage multi-container apps in one YAML file, streamlining orchestration and replication.
330+
- **Efficient collaboration:** Shareable YAML files support smooth collaboration between developers and operations, improving workflows and issue resolution.
331+
- **Rapid application development:** Compose caches the configuration used to create a container. When you restart a service that has not changed, Compose re-uses the existing containers, allowing quick environment changes.
332+
- **Portability across environments:** Compose supports variables in the Compose file, enabling customization for different environments or users.
333+
334+
**How Compose works:**
335+
With Docker Compose, you use a YAML configuration file (the Compose file) to configure your application's services, then create and start all the services from your configuration with the Compose CLI.
336+
337+
> **Note:** The `version` key is obsolete in Compose v2+ and can be omitted.
338+
339+
Refer: `docker-zero-to-hero/docker-compose/docker-compose.yaml`
340+
341+
## Docker Compose Commands
342+
343+
```bash
344+
cd DevOps/docker-zero-to-hero/backend
345+
npm install
346+
```
347+
348+
> **Note:** A basic backend package has been generated with Express and an entry point (index.js). Dependencies are installed and ready for use in your Docker Compose setup. You can now run docker compose up
349+
350+
```bash
351+
docker compose up
352+
docker compose down
353+
docker compose logs
354+
docker compose ps
355+
```
356+
278357
---
279358

280359
## Summary
@@ -285,6 +364,8 @@ This guide covers Docker from basics to advanced concepts including:
285364
- Creating and managing Docker images
286365
- Multi-stage builds for optimized production images
287366
- Internal workings of Docker
367+
- Docker networking
368+
- Docker compose
288369

289370
For more advanced topics, refer to the official [Docker Documentation](https://docs.docker.com/).
290371

214 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const express = require('express');
2+
const app = express();
3+
const port = process.env.PORT || 5000;
4+
5+
app.get('/', (req, res) => {
6+
res.send('Hello from Docker Compose Backend!');
7+
});
8+
9+
app.listen(port, () => {
10+
console.log(`Backend server running on port ${port}`);
11+
});

0 commit comments

Comments
 (0)