The preparatory work

  1. Download and install the Docker client from the Official Docker website.

    After the installation is successful, run docker –version on the terminal. If the following information is displayed, the docker is successfully installed.

  1. Register a Docker account

  2. Sometimes it is difficult to pull the image from DockerHub in China, so you can configure the image accelerator.

    • Azure China Image (dockerhub.azk8s.cn)

    • Ali Cloud Accelerator (login account required)

    • Seven niuyun acceleration (reg-mirror.qiniu.com)

After the Settings are successful, run the Docker Info command on the terminal to check whether the accelerated image takes effect.

This is the end of the Docker preparation.

The use of the Docker

Before using Docker, let’s first understand what is Docker? What are its characteristics? What’s the difference between it and a traditional virtual machine? Then answer each of the above questions.

  1. What is a Docker?

    • Docker is an open source application container engine, based on the go language development.
    • Build, Ship and Run Any App, Anywhere
    • Linux based efficient, agile, lightweight container (lightweight virtualization) solution.
  2. The characteristics of

    • Efficient use of system resources
    • Fast startup time
    • Consistent operating environment
    • Continuous delivery and deployment
    • Easier migration
  3. Compare with traditional virtual machines

  1. Core concepts mirror, container, warehouse

    • Image: A read-only template for Docker
    • Container: running instance of an image
    • Registry: The server where images are stored
  2. Docker use: Take building an Nginx server as an example.

    1. Pull the official nginx image

      docker pull nginx
      Copy the code
    2. Check whether the image is successfully installed

      docker images
      Copy the code
    3. Create a WWW directory and put an index. HTML in it

      mkdir www
      echo 'hello docker' >> www/index.html
      Copy the code
    4. Start mirroring: map the WWW directory to the project location of nginx and map port 80 of the virtual machine to port 8080.

      Command explanation:

      • Docker run: Starts the container
      • –name: indicates the container name
      • -d: background running
      • -v: mounts the host directory or file to the corresponding directory or file in the container

      Nginx in the docker container configuration file directory are:

      • Log location: /var/log/nginx/
      • Configuration file location: /etc/nginx/
      • Project location: /usr/share/nginx/ HTML
      docker run -p 8080:80 -v $PWD/www:/usr/share/nginx/html -d nginx
      Copy the code

As you can see from the picture, after executing the command, the container’s UUID is printed. The nginx container is already running in the background. Enter http://127.0.0.1:8080 to display hello Docker.

  1. Remove the mirror

    docker rmi nginx
    Copy the code
  2. Check the container

    docker ps -a
    Copy the code
  3. Stop the container

    docker stop e05
    Copy the code
  4. Restart the container

    docker start e05
    Copy the code
  5. Create a dummy terminal and go inside the container to see if the WWW is in /usr/share/nginx/html.

    docker exec -it e05 /bin/bash
    Copy the code

As we can see from the image, after executing the command we go inside the container and actually see index.html in the project location.

  1. Remove the container

    docker rm e05
    Copy the code
  2. Customizing images: Take customizing a NodeJS image as an example.

    1. Pull the node image

      docker pull node
      Copy the code
    2. Create the Node-server directory and initialize a project.

      mkdir node-server
      cd node-server
      npm init -y
      Copy the code
    3. Create app.js in Node-server.js

      const http = require('http');
      const server = http.createServer((req, res) = > {
          res.end('hello nodeJS');
      });
      server.listen(3000.() = > {
          console.log('Port 3000 started! ');
      })
      Copy the code
    4. Create Dockerfile.

      Command explanation:

      FORM: Build an image from XX image

      COPY < source path 1> < source path 2>: adds files from the context directory to the specified path in the container

      ADD < source path 1> < source path 2>: adds files from the context directory to the specified path in the container

      WORKDIR: specifies the working directory

      RUN NPM install: RUN NPM install

      EXPOSE 3000: Exposes the 3000 port

      CMD: Similar to the RUN command, used to RUN a program

      CMD < > shell command CMD [" < executable file or command > ", "< param1 >", "< param2 >",...  CMD ["<param1>","<param2>",...]Copy the code

      Docker container node directory location:

      FORM node:latest
      ADD . /app/
      WORKDIR /app/
      RUN npm install
      EXPOSE 3000
      CMD ["node"."app.js"]
      Copy the code
    5. Custom image

      docker build -t nodeserver .
      Copy the code

  1. View image customization

    docker run -p 3000:3000 nodeserver
    Copy the code

Port 3000 starts, and the browser enters HTTP :127.0.0.1:3000, which also displays Hello nodeJS successfully.

At this point we have a NodeJS image completed.

The use of docker – compose

Docker-compose is a tool for defining and running multiple container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.

Docker installation has included Docker-Componse, can be input in the terminaldocker-compose --versionTo viewdocker-composeVersion.

Compose uses three steps:

  • Use Dockerfile to define your application’s environment
  • Use docker-comemage.yml to define the services that make up the application so that they can run together in an isolated environment.
  • Finally, execute the docker-compose up command to get the entire application up and running

So let’s implement compose.

  1. First create the composement-demo directory and Dockerfile, either manually or by command.

    mkdir compose-demo && cd compose-demo
    touch Dockerfile
    Copy the code
  2. Write the contents of the nginx image to a Dockerfile.

    FROM nginx:latest
    RUN echo 'Hello docker' > /usr/share/nginx/html/index.html
    Copy the code
  3. Create the docker-comemage. yml file

    touch docker-compose.yml
    Copy the code
  4. Docker-compose creates two containers, one is the Dockerfile created by the current directory, and the other is the nodeserver image created above.

    # yaml configuration
    version: '3'
    services:
      nginx:
        build: .
        ports:
         - "8080:80"
      node:
        image: "nodeserver"
        ports:
          - "3000:3000"
    Copy the code
  5. Start the application

    docker-compose up
    Copy the code

Docker-compose has been successfully started.

Then type http://127.0.0.1:8080 and http://127.0.0.1:3000 in your browser. Also successful to see the corresponding page.

The simple use of Docker ends here. For more advanced versions, stay tuned.