This is the second day of my participation in the August More text Challenge. For details, see: August More Text Challenge

What is a docker

Docker is an open source engine that makes it easy to create a lightweight, portable, and self-contained container for any application. The containers that developers compile and test on their laptops can be deployed in batches in production environments, including VMS, Bare Metal, OpenStack clusters, and other base application platforms.Copy the code

Docker is a container that can virtualize one or more relatively independent environments (files, networks, etc.) in a host. For example, virtualize two relatively independent operating systems in a host.

Docker concept

The installation of Docker is very convenient. It is usually one-click installation, which will not be introduced here. Specifically, you can view the official documents. Volumes and networks are mainly responsible for managing data and networks between containers. The concepts of Docker are introduced below

Mirror image (image)

Mirroring is one of the core concepts of Docker. An image can be simply understood as a system that provides the most basic services. It is essentially a file system. The image contains data such as programs, files, and configurations needed to run the system.

This section describes how to operate a mirror

  • Viewing the Mirror List

        # List all mirrors
        docker image ls
        
        # List all mirrors
        docker images
    Copy the code

    Both docker image ls and docker images can be executed to return a list of all images.

    REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    node         latest    d1b3088a17b1   2 months ago   908MB
    redis        latest    fad0ee7e917a   2 months ago   105MB
    nginx        latest    d1a364dc548d   2 months ago   133MB
    mysql        latest    c0cdc95609f1   2 months ago   556MB
    centos       latest    300e315adb2f   7 months ago   209MB
    Copy the code
  • Pull the mirror

    DockerHub provides a lot of images, just like NPM library, users can pull the images on DockerHub at will

        docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
        # Optional parameter OPTIONS
        # -a, --all-tags Pull a mirror of all matching names
        # --disable content-trust (default true)
        # --platform String If the server supports multiple platforms, you can set the platform
    Copy the code

    To download a node image, run the docker pull node command. After the image is successfully downloaded, run the docker images command to view the image.

  • Run the mirror

    Once the image is created, it’s time to run the image using docker run

        docker run --name node-container -it node /bin/bash
    Copy the code

    Docker run is a command for running docker image. The specific parameters are a little too many. For detailed operation parameters, please refer to here. After the command is successfully executed, you can view it in the container.

There are many other commands in mirroring, such as:

  • Remove the mirrordocker image rm
  • The introduction of the mirrordocker image import
  • Submit mirrordocker image push

, etc.

Docker’s image is briefly introduced here, if you want to learn more about it, you can click here

The container (the container)

The second core concept of container Docker. Can be thought of simply as an application. These containers are generated by running the image. One image can generate multiple containers, and they can be independent of each other or associated with each other.

The main operation of the container

  • Running was described in the description of running mirrors, usingdocker run [imageName]Create and run a container.
  • stop
        The stop container command can index the container using the container name or ID
        docker container stop [containerName | containerId]
    Copy the code
  • Remove the container
        docker container rm [containerName | containerId]
    Copy the code

    It’s important to note that,A running container cannot be deleted, the deleted container must be in the stopped state

  • Into the container

    Enter the container usingdocker execCommand,Learn more about the exec command
        # Enter the container
        docker exec -it [containerName | containerId] [command]
    Copy the code
  • Export and import containers
        # Export container
        docker export [containerID | containerName] > [outputName].tar
        # Import container
        docker import [target]
    Copy the code

Warehouse (repository)

Repositories are the third core concept of Docker. Imagine, after we’ve deployed the environment on one host, if I want to deploy the same environment on another host. So I just put the deployment of the warehouse into the warehouse, and then in another host to pull the image run on it, very convenient and fast. The core of a warehouse is push and pull. To learn more

Network (network)

By default, containers created by Docker are not associated with each other. If we want to put two networks into a LAN, we need to use Docker’s network. Common network commands are as follows:

  • createdocker network create [networkName]
  • Check the networkdocker network ls
  • Remove the networkdocker network rm [networkName | id]

The basic process is to create a network and specify the container to this network when you create the container. Specific network commands:

    Create a network
    docker network create my-network
    
    # Create container node_1 and place the container on the newly created network
    docker run --network my-network -d -it --name node_1 node
    
    # Create container node_2 and place it on the newly created network
    docker run --network my-network -d -it --name node_2 node
    
Copy the code

Log in to node_1 dokcer exec-it node_1 and check whether node_2 and node_1 are in the same LAN. Run the ping command ping node_2

In this way, the two networks are connected.

Volume (volumn)

The default Docker container is not only network independent, but also file independent. If you want to view files outside the container, or if the two containers share files, you need to use volumes. Common operations on volumes are as follows:

  • Create a volumedocker volume create [option] [volumeName]
  • Viewing the Volume Listdocker volume ls
  • Viewing Volume Detailsdocker volume inspect [volumeName]
  • Remove the volumedocker volume rm [volumeName]

Using volumes: Using volumes requires that volumes are bound to the container as soon as the container is run. Bind the docker run -v [volumeName] [targetPath] -d -d it [imageName] command for example: to enable both containers to access a file path outside the container, you can do this

    Create a volume
    docker volume create my-volume
    
    # Create a container node_1 and specify the volume of the container and the destination path
    docker run -v my-volume /share --name node_1 -d -it node
    
    # Create a container node_2 and specify the volume of the container and the destination path
    docker run -v my-volume /share --name node_2 -d -it node
Copy the code

Create a volume and create two containers with a common volume mapped to the /share path inside the container. When you’re done, entering the container will generate the /share directory

Build a Web service using Docker

After introducing the concept and usage of Docker, we can now start to practice. Our goal is to use Docker to build a Web service. Use nginx and Node images

Start by writing a Web service with Node

const http = require('http');

const app = http.createServer((req, res) = > {
    res.end('Hello Docker');
})

app.listen(8080.() = > {
  console.log('server running at 8080');
});

Copy the code

And then create the container

    # Pull nginx and Node images
    docker image pull nginx node
    
    # Create network
    docker network create my-network
    
    # create a volume
    docker volumn create my-volume
    
    # Create nginx container
    docker run nginx -v my-colume:/share --network my-network -it -d -p 80:80 --name nginx nginx /bin/bash
    
    Create a node container
    docker run -it -v my-volume:/share --network my-network -it -d --name node node /bin/bash
    
    # Put the Web service file into the shared volume
    
    Execute the node service
    docker exec -it node node /share/index.js &
    
    # Modify nginx configuration to point port 80 to node service
    server {
        listen 80;
        server_name test;
        
        location / {
            http_proxy http://node:8080; }}Copy the code

In this way, the node service can be accessed by accessing the server port 80

summary

This article mainly introduces what Docker is, the three cores of Docker (image, container, warehouse), and introduces the network and volume. Finally, a Web service was built by using Docker and Node.

If there are any inaccuracies or errors in this article, please note them in the comments section

reference

2.www.docker.com 3.Docker — from introduction to practice