.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}

Docker

What is the docker?

  • According to its official website, Docker is a container platform, a standardized software unit that uses virtualization technology (CGROUP: provides container isolation; UnionFS: Save the image and make the container ephemeral), an open source project based on Google’s Go language, code maintained on GitHub.

According to the use docker?

  • Fast delivery and deployment

    • Light: It can be created or configured at one time, run normally anywhere, and start in seconds, saving development, testing, and deployment time.
  • Efficient virtualization

    • Kernel-based virtualization, no additional hypervisor support, low CPU/ memory consumption, more efficient.
  • Portable and extensible

    • Physical machines, virtual machines (VMS), public clouds, private clouds, personal computers, and servers can run on all platforms
  • Compare with traditional VMS

    features The container The virtual machine
    Start the Second level Minutes of class
    The hard disk to use General MB General GB
    performance Close to the native Weaker than the native
    System support Thousands of containers per machine Usually dozens
  • It can be concluded that Docker can achieve:

    • Isolate application dependencies
    • Create an application image and copy it
    • Create ready-to-use applications that are easy to distribute
    • Allows instances to scale simply and quickly
    • Test the application and destroy it later

Docker’s ambition is to create portable, lightweight containers for software programs that can run on any docker installed machine, regardless of the underlying operating system.

How to use docker?

A few basic concepts:

Image: a read-only template used to create docker containers with a writable top layer created at startup


Container: A running instance created from an image that can be started, started, stopped, and deleted. Containers are isolated from each other and look like a simplified version of the Linux environment (including root user permissions, process space, user space, network space, and so on) and the applications running in it


Repository: similar to Git, it is a place where image files are stored centrally. It is divided into Public repository and Private repository. The largest Public repository is Docker Hub


Registry server: A hosting service like GitHub hosts multiple repositories


Host: the server where the Docker resides


Data volume: A special directory that can be used by one or more containers, similar to mounting directories or files under Linux, bypassing UFS and providing many useful features:

  • Share and reuse between containers
  • Changes to data volumes take effect immediately
  • Data volume updates do not affect mirroring
  • The volume exists until no container is used

Network mode: Used to realize network communication of containers. There are five types:

  • Bridge mode, –net=bridge(default)
  • Host mode, –net=host
  • None mode, –net= None
  • Other container modes (i.e. Container mode), –net=container:NAME_or_ID
  • Custom: a feature added after Docker 1.9

To learn more


Dockerfile: a command that is easy to automate and contains all the instructions needed to create an image. Based on the instructions in Dockerfile, we can use the $docker build command to create an image, simplifying deployment by reducing the image and container creation process. Syntax commands:

  • INSTRUCTION arguments are case insensitive, but the naming convention is all uppercase
  • FROM
    • Format forFROM <image>orFROM <image>:<tag>
    • The first command must beFROMThe instructions. Also, if you create multiple images in the same Dockerfile, you can use multiple imagesFROMInstruction (once per mirror)
  • MAINTAINER
    • Format forMAINTAINER <name>To specify the maintainer information
  • RUN
    • Format forRUN <command>RUN ["executable", "param1", "param2"]
    • The former will run commands in the shell terminal, i.e/bin/sh -c; The latter usesexecThe execution. Specifying the use of other terminals can be done in the second way, for exampleRUN ["/bin/bash", "-c", "echo hello"]
    • Each of theRUNThe command executes the specified command based on the current image and submits it as a new image. You can use \ to wrap lines when the command is long
  • CMD
    • CMD ["executable","param1","param2"]useexecExecution, recommended
    • CMD command param1 param2/bin/shFor applications that require interaction
    • CMD ["param1","param2"]To provideENTRYPOINTDefault argument to
    • Specifies the command to execute when starting the container. Only one Dockerfile is allowedCMDCommand. If multiple commands are specified, only the last command will be executed
    • If the user specified a command to run when starting the container, it will be overwrittenCMDSpecified command
  • EXPOSE
    • Format forEXPOSE <port> [<port>...]
    • Tells the Docker server container the exposed port number for interconnecting systems. When starting the container, you need to pass -p, and the Docker master will automatically assign a port to forward to the specified port
  • ENV
    • Format forENV <key> <value>. Specify an environment variable that will be followedRUNCommand is used and held while the container is running.
  • ADD
    • Format for the ADD<src> <dest>
    • This command copies the specified<src>Into the container<dest>. Among them<src>Can be a relative path to the directory where the Dockerfile resides; It could also be a URL; It can also be a tar file (automatically decompressed into a directory)
  • COPY
    • Format forCOPY <src> <dest>
    • Replicating the local host<src>(relative to the directory where the Dockerfile resides) to the container<dest>. This parameter is recommended when a local directory is used as the source directoryCOPYCan,COPYNot in the case ofADD
  • ENTRYPOINT
    • ENTRYPOINT ["executable", "param1", "param2"]
    • ENTRYPOINT command param1 param2(Executed in shell)
    • Configures the command to be executed after the container is started and cannot bedocker runParameter overrides provided
    • There can only be one DockerfileENTRYPOINTWhen multiple values are specified, only the last one takes effect
  • VOLUME
    • Format forVOLUME ["/data"]
    • Create a mount point that can be mounted from a local host or other container. It is typically used to hold databases and data that needs to be held
  • USER
    • Format forUSER daemon
    • Specify the user name or UID to run the container, and thereafterRUNThe specified user is also used
    • When a service does not require administrator rights, you can use this command to specify a running user. And you can create the required user before, for example:RUN groupadd -r postgres && useradd -r -g postgres postgres. Can be used to temporarily obtain administrator rightsgosu, and not recommendedsudo
  • WORKDIR
    • The format is WORKDIR /path/to/ WORKDIR

    • Configure the working directory for subsequent RUN, CMD, and ENTRYPOINT directives

    • Multiple WORKDIR directives can be used, and subsequent commands, if their arguments are relative paths, will be based on the path specified by the previous command. Such as:

    • WORKDIR /a

    • WORKDIR b

    • WORKDIR c

      The final path is /a/b/c

  • ONBUILD
    • ONBUILD [INSTRUCTION]

    • Configure the operation instructions to perform when the created image is used as the base image for other newly created images

    • For example, Dockerfile creates the image image-a with the following content:

      [...].  ONBUILD ADD . /app/src ONBUILD RUN /usr/local/bin/node-build --dir /app/src [...]Copy the code

      If A new image is created based on image-a, the ONBUILD directive will be executed automatically when the base image is specified in the new Dockerfile using FROM image-a, which is equivalent to adding two directives later:

        FROM image-A
      
        #Automatically run the following
        ADD . /app/src
        RUN /usr/local/bin/node-build --dir /app/src
      Copy the code

      It is recommended to label images using the ONBUILD directive, such as Ruby :1.9-onbuild

After writing the Dockerfile, you can create the image with the $docker build command

  • The basic format is$docker build [options] path, the command reads the Dockerfile in the specified path (including subdirectories), and sends all contents in the path to the Docker server for creating the image. Therefore, it is recommended that the directory where Dockerfile is stored be empty. Also through.dockerignoreFile (add a matching pattern per line) to make the Docker ignore directories and files under the path
  • Such as:$ docker build -t ${dockerImageName} --force-rm -f ./Dockerfile .

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

These are the basic concepts. Now start installing Docker in Ubuntu and start an Nginx container (nginx doesn’t need to run inside containers in most scenarios, just to give you an example).

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

$ cat /etc/issue
Ubuntu 16.04.6 LTS \n \l
Copy the code
  1. Install using shell command:

    1. $ curl -sSL https://get.docker.com/ | sh
    2. More platforms and installation methods
  2. Check the installation. If the version information is displayed, the installation is successful:

    $ docker -v
    Docker version 18.09.5, build e8ff056
    Copy the code

    Ps: $docker info for more information

  3. Let’s start by creating a container:

     $Docker run - meeting \ -d sup \ --name=demo \ --restart=always luncheon \ -u root scheme \ -p 8002:80 rendering \ -v /var/demo:/var/demo size latest meal
    Copy the code

    Based on nginx:latest, create a container named Demo and map port 80 inside the container to port 8002 on the host so that the traffic passing port 8002 on the host can be forwarded to port 80 inside the container

    Run Creates and starts a container. There are more options.

    • Check whether the specified image exists locally, if not, download from the public repository (docker pull XXX)
    • Create and start a container with an image
    • Assign a file system and mount a read-write layer outside the read-only mirror layer
    • Bridge a virtual interface from the host host’s configured bridge interface to the container
    • Configure an IP address from the address pool to the container
    • Execute the user-specified application
    • The container is terminated after execution

    The container was run in the background in a Daemonized mode


    What is the name of the container


    Always Indicates the restart policy. Always Starts when the daemon starts. For example, if the server breaks down or restarts, the container automatically restarts


    Roe: -u Specifies the user of the container


    Host host ports: container ports; multiple ports may be applied for multiple times


    The value is -v. Host directory: a container directory that can be used to mount multiple data volumes


    There is an image name, which is the one used by the container. The local image can be viewed using $docker images

    Uncommented version:

    docker run \
    -d \
    --name=demo \
    --restart=always \
    -u root \
    -p 8002:80 \
    -v /var/demo:/var/demo \
    nginx:latest
    Copy the code
  4. View a running container:

    $ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES C24Cfee8226a nginx:latest "nginx -g 'daemon of..." 6 minutes ago Up 6 minutes 0.0.0.0:8002->80/ TCP demoCopy the code

    Ps: $docker ps -a view all containers (including running, stopped)

    Now access port 8002 of the host (security group enabled) and you can see the welcome page of nginx:

  5. Enter the container:

    perform$ docker exec -it demo bash, demo can also be replaced with the container ID:$ docker exec -it c24cfee8226a bash

    Run the $netstat -lntp command to check port usage in the container:

    Proto Recv -q Send -q Local Address Foreign Address State PID/Program name TCP 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master proCopy the code

    If netstat is not found, run $apt update && apt install net-tools

    Exit the container by executing $exit

    Ps Common commands:

    • $ docker inspect demoYou can view more information about the container
    • $docker stop Container name/IDTermination of the container
    • $docker Rm Container name/IDDelete containers that are not running
    • $docker RMI image name/IDDelete images that are not occupied (no container is running)
    • $ docker rmi $(docker images -f "dangling=true" -q)Batch delete asnoneImage of (no container running)

【 reference 】 :

  1. www.kancloud.cn/kancloud/do…
  2. Leilux. Making. IO/Lou/docker_…
  3. docs.docker.com
  4. Blog.csdn.net/ithaibianti…

===🧐🧐

Finally: Next time, based on Docker, build CI (Jenkins) to build deployment front-end projects