Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems II - Lab 08
Why Docker exists, writing a Dockerfile, and building and running containers
PostgreSQL/PostGIS plus Redis plus Angular) requires every developer to manually install compatible versions of everythingLocal
manual install
Test
manual install again
Staging
manual install again
Production
manual install again
docker build -t myapp .
docker run myapp| OOP Concept | Docker Equivalent |
|---|---|
| Class / Blueprint | Docker Image |
| Object / Instance | Docker Container |
Dockerfile.Docker Image
Container 1
Container 2
Container 3
Dockerfile instruction creates a layer in the resulting imagenode:14 base image
WORKDIR /app
COPY package.json
RUN npm install
COPY source files
hub.docker.com) is the public registry for Docker images, the GitHub of Docker imagesNode.js, PHP, Python, PostgreSQL, Redis, nginx, and more-slim, -alpine; Alpine variants are dramatically smaller, about 5 MB base| Instruction | Purpose |
|---|---|
FROM | Sets the base image |
WORKDIR | Sets the working directory inside the container |
COPY | Brings files in |
RUN | Executes a build-time command |
CMD | The startup command |
EXPOSE | Documents the listening port |
FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 4000
CMD ["npm", "start"]package.json and run npm install before copying the rest of the sourcenpm install when dependencies actually change, not on every code editdocker build -t express-node-app .
docker image lsdocker run starts a container from an image; add -d to run it detached in the backgroundEXPOSE 4000 is declared?-pdocker run -d --name express-node-app-container -p 4000:4000 express-node-app-p host:container maps a host port to the container's port, here 4000:4000| Command | Action |
|---|---|
docker ps | Running containers |
docker ps -a | All containers, including stopped |
docker stop <name> | Stop a running container |
docker rm <name> | Remove a stopped container |
docker rm -f <name> | Force stop and remove in one command |
docker image ls | List images |
docker rmi <image> | Remove an image, only if no container uses it |
docker logs <name> | Show a container's output |
FROM node:14-alpinedocker image ls after rebuilding with each variantDockerfile layers are cached; order instructions from least to most frequently changing for faster rebuilds-alpinedocker build creates the image; docker run -p host:container starts it and exposes it to the hostdocker ps, docker stop, docker rm, docker rmi, docker logsMahmoud Abas|Lab 08: Introduction to Docker - Containers and Images