- Docker is a containerization platform which packages your application and all its dependencies together in the form of containers so as to ensure that your application works seamlessly in any environment be it development or test or production.
- Docker containers, wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries etc. anything that can be installed on a server.
- This guarantees that the software will always run the same, regardless of its environment.
Docker originally used LinuX Containers (LXC), but later switched to runC (formerly known as libcontainer), which runs in the same operating system as its host. This allows it to share a lot of the host operating system resources. Also, it uses a layered filesystem (AuFS) and manages networking.
Linux containers are technologies that allow you to package and isolate applications with their entire runtime environment—all of the files necessary to run. This makes it easy to move the contained application between environments (dev, test, production, etc.) while retaining full functionality.
Compare with VM, Linux containers have following advantages:
- Quick start. Start container is just starting a process from OS, not start VM OS + process.
- Take less resources. Container is not a complete OS, and only take needed resources.
- Smaller size. Only take needed component while VM need entire OS.
Compare with VM, containers get less isolation, but "lightweight", smaller and faster which allows more instance running.
AuFS is a layered file system, so you can have a read only part and a write part which are merged together. One could have the common parts of the operating system as read only (and shared amongst all of your containers) and then give each container its own mount for writing.
More information, please go to stackoverflow - docker vs VM.
Rules:
Write .dockerignore file
Container should do one thing
Understand Docker caching! Use COPY and RUN commands in proper order to utilize that.
Merge multiple RUN commands into one
Remove unneeded files after each step
Use proper base image (alpine versions should be enough)
Set WORKDIR and CMD
Use ENTRYPOINT when you have more than one command and/or need to update files using runtime data
Use exec inside entrypoint script
Prefer COPY over ADD
Specify default environment variables, ports, and volumes inside Dockerfile
Best practices for writing Dockerfiles - docker doc
How to write excellent Dockerfiles - blog 中文
Docker Compose is a tool to streamline the definition and instantiation of multi-tier, multi-container Docker applications.
version: "3"
services:
web:
build: .
volumes:
- web-data:/var/www/data
redis:
image: redis:alpine
ports:
- "6379"
networks:
- default
'docker-compose up' will launch all containers defined in yml file. For more information, you could go to compose doc and compose command options, for examples you could go to docker samples voting app or lyft amundsen.
https://docs.docker.com/storage/volumes/
Using a union filesystem allows each layer that is created to be reused by an unlimited number of images. This saves a lot of disk space and allows images to be built faster since it is just re-using an existing layer. More information please go to this article Digging into docker layers.
http://cpp.sh/93s34 Unix clone example
Purpose | Command Example | Comments |
---|---|---|
Create | docker run -t -i container-name | |
Check all running container | docker ps -a | |
Stop | docker stop container-ID | |
Restart | docker restart container-ID | |
Remove all containers based on docker image name | link |
basic
- Docker 微服务教程 阮一峰的网络日志
- Docker 入门 云溪论坛 part1 part2
- How Yelp use Docker
- Docker discussions - stackoverflow
- Docker 从入门到实践
- Docker 和 Kubernetes 从听过到略懂:给程序员的旋风教程
- medium Multistage build
Advanced