Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems II - Lab 09
Faster builds, hot reload with bind mounts, multi-container Compose, and env vars
.gitignore, but for the Docker build context: it excludes files from being sent to the daemon.node_modules, Dockerfile, .env, .git, *.lognode_modules
Dockerfile
.env
.git
*.log.dockerignore.Copy package.json + package-lock.json
npm install
cached unless deps change
Copy source code
doesn't bust the install cache
COPY package.json package-lock.json ./
RUN npm install
COPY . .docker exec -it <container> bash opens a live shell inside a running container to inspect it directlynodemon restarts the Node.js server automatically when watched files change$(pwd) for the absolute host path (%cd% on Windows); relative paths don't workdocker run -d -p 4000:4000 -v $(pwd):/app my-express-app:ro to make it read-only: -v $(pwd):/app:ronode_modules from existing if it's missing on the host, fixed with an anonymous volumedocker run -d -v $(pwd):/app:ro -v /app/node_modules my-express-app| Type | Syntax | Use Case |
|---|---|---|
| Bind Mount | -v /host:/container | Two-way dev sync |
| Read-Only Bind Mount | -v /host:/container:ro | Safer source sync |
| Anonymous Volume | -v /container | Protect node_modules |
| Named Volume | -v myvolume:/container | Database persistence |
docker build/docker run commandsdocker compose up -dcontainer_name, build, ports, volumes, environment, env_file, depends_ondocker run -v, Compose accepts relative paths like ./srcservices:
node-app:
build: .
ports:
- "4000:4000"
volumes:
- ./src:/app/src:ro| Command | What It Does |
|---|---|
docker compose up -d | Build if needed, start all services in the background |
docker compose down | Stop and remove containers (add -v to also remove volumes) |
docker compose logs -f <service> | Follow a service's logs |
docker compose exec <service> bash | Open a shell inside a running service |
| Method | Where It's Set |
|---|---|
ENV | Dockerfile instruction, baked into the image |
--env / -e | docker run flag |
--env-file | docker run flag, loads a whole file |
environment: | Inline in docker-compose.yml |
env_file: | In docker-compose.yml, recommended for secrets |
services:
node-app:
env_file:
- .env.dockerignore keeps secrets and bloat out of images; ordering Dockerfile instructions correctly keeps builds fast via caching:ro makes them safer, and anonymous volumes protect directories like node_modules.env and be loaded via env_file, never hardcoded into the imagedocker exec -it and docker logs are the go-to tools for inspecting a running containerMahmoud Abas|Lab 09: Docker Advanced - Optimization, Volumes, Compose & Environment Variables