1. Overview
Docker is an open-source platform that enables developers to automate the deployment and management of applications within lightweight, isolated containers. It provides a consistent environment for applications to run, regardless of the underlying infrastructure.
Docker container is quite similar to VM in term of they has it’s own process, root file.
But since VM is the isolated OS itself, otherwise Docker containers share the same OS. VM tends to be more secure compared with Docker container but much more slower.
source: weave.works
2. Docker concepts/components
Image
Docker images are the building blocks of containers, contain everything to run application (code, libraries, system tools,…).
Images is created from Dockerfile.
docker build -t {image-name}:{tag} // create from dockerfile
docker image ls // see the list of images
docker run {image-name}:{tag} // run the container from the specified image
docker image rm {name/id} -f // remove image, -f is force
Container
Containers are instances of Docker images -> running and executing in an isolated environment.
// run the container from the specified image + mapping port
docker run --name {name} {image-name} -p 3000:3000
docker start {name/id} // run existed container
docker stop {name/id} // stop container
docker ps -a // list containers
docker container rm {name/id} // remove container
Dockerfile
Within the Dockerfile, use various instructions to define the image’s configuration:
FROM: parent image
RUN: execute command -> install dependencies
WORKDIR: working directory -> easier to manage and organize
COPY/ADD: copy local files to the image
EXPOSE: which port container should listen in run time
CMD/ENTRYPOINT: command to run when container starts
Example:
FROM node:16-alpine
EXPOSE 6060
WORKDIR /app
COPY ["package.json", ".babelrc", "tsconfig.json", "nodemon.json", "yarn.lock", ".env", "./"]
RUN yarn
RUN yarn add ts-node-dev --save
COPY . .
CMD [ "yarn", "dev" ]
Volume
Volumes in Docker can be used to mount data from a container to the host machine or vice versa.
docker run -v /path/on/host:/path/in/container {image-name}
// -v flag is used to specify the volume mount
Any changes in the container -> persisted on the host machine -> access and manage the data from outside the container.
Others
In case of running out disk space, we can use:
docker system prune
to clean up unused data in Docker environment (containers, volumes, images,…)