Ok,

Docker common command list

preface

This chapter will record the actual combat operation of Docker, with the ultimate goal of solving common problems of using Docker in the development process. At the end of this chapter, we will use Docker to achieve an elegant and comfortable development experience.

In actual combat

How do I install Docker

There are many ways and scenarios to install Docker, including but not limited to Linux, Windows, Macs, cloud servers, PCS, and more. It can also be installed in a variety of ways, including manual installation, scripted installation, and wizard installation. In short, Docker can be ubiquitous, arbitrary, simple and powerful.

Desktop installation:

  • Windows Docker Installation
  • Mac Docker Installation

Server installation:

  • Linux Docker Installation
  • Windows Server Docker Installation

Important: This section is about installing Docker on Linux and Windows, as this is the most common development and deployment environment

How to Obtain a Mirror

First, let’s take a look at the sources of Docker images:

  • Image warehouse services, such as Docker Hub, Ali Cloud container image service, private image warehouses built by companies/individuals

    Mirror warehouse is the most important way for us to obtain mirror, generally divided into official warehouse and unofficial warehouse. Docker Hub is the mirror warehouse used by Docker CLI by default. It is the mirror warehouse service officially built and maintained by Docker, but it is also divided into official warehouse and unofficial warehouse.

  • Create your own image using the Docker build command

  • Offline mirror

Here are some of the most common procedures for obtaining images from Docker Hub:

  1. Search the mirror

    docker search ${image name}
    Copy the code

    For example, to search a centos image, run the docker search centos command. For more search methods, see docker search –help

  2. Access to the mirror

    docker pull ${image name}:${image version}
    Copy the code

    For example, obtain a centos 7 image

    docker pull centos:7

    See docker pull –help for more details

  3. Look at mirror

    docker images
    #or
    docker image ls
    Copy the code

  4. Remove the mirror

    docker rm ${image name}
    Copy the code

    Example Delete centos

    docker rm centos

    See docker rm –help for more deletion methods

  5. Other Image operations Image images

How to run containers

Before explaining Docker container operation, we should first understand the whole container life cycle, and understand the function of each command by comparing the life cycle will get twice the result with half the effort!

  1. Start the container

    1. Create and start

      $ docker run --name mycentos centos echo 'Hello,world! '
      Hello,world!
      Copy the code

      PS:

      – The name parameter gives a name to the container to run, for convenience. A container can be identified with the generated ID and name in Docker.

      Think about:

      If you run docker ps-a to view all containers, you will see that the container you just ran is in the Exited state (Stopped state).

      What is the lifecycle flow of the container after the above command is executed?

      When docker run is used to create containers, the standard operations that Docker runs in the background include:

      • Check whether the specified image exists locally and download it from the public repository if it does not
      • 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
    2. Start the terminated container

      docker start ${image name/image id}
      Copy the code

      Docker start mycentos

      At this point, you can view the running container through Docker PS

  2. Daemon operation

    docker run -d ${image name/image id}
    Copy the code

    Here we just add a parameter -d to realize the daemon state, also known as background running.

    But it’s important to note that this alone is not enough.

    This is mainly because Docker automatically stops containers when no process is running!

    You can try this:

    docker run --name mycentos -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done"
    Copy the code

    As you might have guessed, this is just starting the Docker container and running a loop in the background.

    You can try docker ps and Docker logs mycentos to see what happens.

  3. Termination of the container

    docker stop ${image name/image id}
    Copy the code

    In addition, Docker Restart can terminate a running container and then restart it.

  4. Into the container

    It says that we can make the container run in the background with the -d argument, but how do we re-enter the container after that? There are two ways to do this, and pay attention to the nuances.

    1. Attach command to enter

      docker attach ${image name/image id}
      Copy the code

      Do docker attach mycentos and tell me what you see.

    2. Exec command entry

      Docker exec -it ${image name/image id} ${Copy the code

      -it is a combination of two parameters, the end effect of which is to enter the container in interactive mode

      In this case, we can do some simple operations like in centos system, try LS

      Think about it: Where did the looping hello World go?

    3. What’s the difference?

      While the attach and exec commands both give us access to a container operation, attach accesses the command terminal window that is currently running in the background. Exec, on the other hand, reopens a terminal window for us, as simple as that, and the magic seems to get simpler and simpler.

  5. Remove the container

    docker rm ${image name/image id}
    Copy the code
  6. Other containers operate on Docker containers

summary

The focus of this section is the container lifecycle diagram, which provides insight into the meaning of each command through transitions between states in the lifecycle.

In addition, Docker is so simple and elegant because its commands are almost identical to Linux commands.

To be continued… If you want to know anything, please leave a comment below. I will gradually improve this article and help you fully master the powerful Docker!

How to install Nginx

How to Install database

How do I attach a data volume

How do I share data between containers

How do you persist data

How do I map ports

How do YOU deploy a front-end project

How are back-end projects deployed

How to package an image with Dockerfile

How do I publish Docker images

Configuring the Network

How do YOU cluster data

How to run MySQL

How do you allow containers to communicate with each other