Docker

Containerization technique

I. Problems solved

  1. The developer and tester environments are inconsistent, resulting in different run results
  2. The deployment and migration of applications and environments are heavy
  3. VMS consume a lot of resources and have poor performance

Ii. Development history

  1. Virtualization – Virtualizes hardware resources (cpus, memory, hard disks, and networks) and installs operating systems (oss) in the virtualized space
  2. Containerization Container – Packages applications and environments into a unified Container that can run in the Container Runtime on any operating system – similar to the JVM

Three, core components

  1. Docker engine (daemon) – a long-running process

    • Interact with the Docker client

    • Build, run, and distribute Docker containers

  2. Docker image – read-only template, built from a series of instructions

    • The Build and package phases of the Docker life cycle

    • When the container is started from the image, a writable layer is created on top of the image

  3. Docker container

    • Run and isolate applications

    • The startup and run phases of the Docker lifecycle

  4. The Docker warehouse

    • Image files of a certain type are stored centrally, and different images are distinguished by tags, similar to projects in Git
    • Repository Registry, which is responsible for hosting repositories, is similar to GitHub
    • According to the image of the public or not, can be divided into public warehouse, private warehouse

Four, instructions,

1. The mirror

  • Get – docker pull NAME[:TAG]

    • The default Registry for NAME is registry.hub.docker.com
    • TAG is latest by default
  • Add a tag – docker tag [original image name: tag] [destination image name: tag] – Two mirror messages will be sent, but both belong to the same image

  • Docker inspect NAME[:TAG]

  • View hierarchical information – docker history Image ID

  • View all image information – docker images – Use -f to filter information

  • Search – Docker search name

  • delete

    • docker rmi NAME[:TAG]

    • docker rmi IMAGE_ID

      If a container is running the image, it cannot be deleted. You can run the -f command to forcibly delete the image, but it is not recommended

  • Upload – docker push NAME[:TAG] – default upload to docker Hub official repository

  • Create from DockerFile -docker build [-f DockerFile path] [-t image name: tag]

2. The container

  • Create – docker create NAME[:TAG]

    • – I – Interactive mode

    • -t – Pseudo terminal

    • -d – Background running

    • -e key=value – Specifies the environment variable

    • -m Memory size n/A Maximum memory usage N/A B, K, M, or G

    • -v Linux directory :Docker directory [:ro] – Mapping the Linux directory to the data volume in the container

      The Linux directory must exist

    • -p port: port – Maps a Linux port to a Docker port

    • –name Container name – Specifies the container name

    • –rm – Container Automatically deletes the container after exiting

    • –network

      • Bridge – [default] Network bridge, authenticated by MAC address (router authenticated by IP address)
      • Host – used host IP – no longer available-p
      • Overlay – Links multiple Docker engines together so that clusters can communicate with each other
      • Macvlan – Assigns MAC addresses to containers and displays them as physical devices on the network
      • None – Disables the network
    • — TMPFS – Data volumes are mounted only in memory and do not persist

  • Start – docker start Indicates the container ID

  • Check the status

    • Run the container – Docker PS
    • All containers – Docker PS-A
  • Check the details

    • Docker inspect Container ID
    • Docker Stats container ID
  • Docker run NAME[:TAG]

    Equivalent to create + start

    You can use the create option parameter

  • stop

    • Docker stop container ID -t time – [default] 10 seconds
    • Docker kill Container ID
  • – docker restart Indicates the container ID

  • -docker exec it container ID /bin/bash

  • Deleted – docker RM Container ID

    If the container is running, it cannot be deleted. You can run the -f command to forcibly delete the container

  • Pause – docker pause Indicates the id of the container

  • Pause recovery – docker ID of the unpause container

3. The warehouse

  • Login – docker login -u username -p password
  • Logout – Docker logout
  • Certification documents – copy the document content can avoid under Linux login/root /. Docker/config. Json

4. DockerFile

It consists of lines of command statements

【 Objective 】 Construct mirror image

  • FROM NAME[:TAG] – Based on mirror
  • MAINTAINER Name mailbox – MAINTAINER name and mailbox
  • COPY Linux Path Docker path – Copies native resources into a Docker container – supports wildcards and regulars
  • RUN command – Runs a command
  • ADD path 1 Docker path – Obtain resources (remotely) to the Docker container
    • [Recommended] RUN wget is used for remote resources
    • [Recommended] Use COPY for local resources
  • CMD command – Only one command can be run after Docker is started
  • ENTRYPOINT command – similar to CMD directive
  • ENV key=value – Sets environment variables
  • VOLUME Docker path – Declares a path in a Docker container as an anonymous VOLUME
  • WORKDIR Docker path – Switch directory
  • EXPOSE Port 1 Port 2 – Sets the listening port

5. Data volume

  • Creation n/A Docker volume create Specifies the volume name
  • View the list – docker volume ls
  • View message – docker volume inspect Volume name
  • Deleted -docker volume Rm volume name

6. Compose

[Objective] Docker containers that run multiple images at the same time

The command

  • Run (build + start) -docker-compose up -d
  • Check out -docker-compose PS
  • View log-docker-compose logs
  • Service port binding – docker-compose port Service name port
  • Rebuild the service-docker-compose build
  • Docker-compose start docker-compose start
  • Docker-compose stop [service name]
  • Docker-compose rm [docker-compose]
  • Docker-compose down docker-compose down [-v]
  • Run the -docker-compose run service name command

configuration

The default file name is docker-comemage.yml

  • Version-swarm combination requires 3.0 or above
  • Services – Defines a service
  • Image – Uses an image
  • Build – Builds images using DockerFile
  • Ports – Sets port mapping
  • Volumes – Mapping data volume container
  • Command – Overrides the commands that run by default when the container is started
  • Depends_on – Sets the service startup sequence
  • Environment – Sets environment variables
  • Network_mode Sets the network mode
version: '2.0' # version 2.0
services: # Define service
  mysql: # service name
    image: Mysql: 5.7 # Use mirror
    ports: # port mapping
      - 3306: 3306
    volumes: Volume # mapping
      - /mydata/mysql/log:/var/log/mysql
      - /mydata/mysql/data:/var/lib/mysql
      - /mydata/mysql/conf:/etc/mysql
    environment:
      MYSQL_ROOT_PASSWORD: myPassword
  redis:
     image: Redis: 6.0
     ports:
       - 6379: 6379
  halo:
    image: tony60107/halo:v1
    ports:
      - 8090: 8090
Copy the code

7. Swarm

[Objective] Modify the Docker container cluster configuration without manual restart

  • Viewing cluster status – docker info
  • View node information – Docker node ls
  • Viewing Service Information
    • All services – Docker service ls
    • Docker service ps [docker service name]
  • Check token messages in Manager – docker swarm join- Token Manager
  • Docker swarm Join token adds working node to cluster – Docker swarm Join token
  • Docker swarm Join – Token Manager add management node to cluster – Docker swarm Join – Token Manager
  • Docker swarm leave –force docker swarm leave –force
  • Publish services to clusters – Docker service create service name
    • -p Port: indicates the port-port mapping
    • — Replicas Number of instances – Total number of running instances
    • –name Name – User-defined service name
  • Docker service rm [docker service name]
  • Docker Service Scale Service name = Number of instances
  • Update service – Docker service Update property name property