Skip to content

Latest commit

 

History

History
118 lines (86 loc) · 6.4 KB

docker.md

File metadata and controls

118 lines (86 loc) · 6.4 KB

Docker

What is docker

  • 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_structure.png


Docker notes

Docker vs VM

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.

docker_vs_vm.png

Compare with VM, containers get less isolation, but "lightweight", smaller and faster which allows more instance running.

docker_compare_with_vm.png

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.

Docker files

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

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.

Docker storage

https://docs.docker.com/storage/volumes/

docker_storage_3mode.png


Docker layers

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.

Docker internal

http://cpp.sh/93s34 Unix clone example

Docker commands

docker-cheat-sheet

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

More Info

basic

Advanced