This paper sorted out common commands in Docker, which can be used as a memo, and can also be used as a review article.

Docker image, container, log and other related commands are covered, the text with operation diagram, not difficult oh.

Like a sentence: “eight hours for life, eight hours for development.”

'

Cover location: Shaoyang, Hunan

Author: ☞

Docker-related commands

  1. View the Docker version number

    docker --version
    Copy the code

  2. View Docker information

    docker info
    Copy the code

  3. View the Docker help command

    docker --help
    Copy the code

    You can view all the Docker commands, but not very friendly to students who are not good at English. 🙃~~ (me too) ~~

Docker image related commands

2.1 Mirror View

  • Listing local Mirrors

    1. View all mirrors

    docker images
    Copy the code

    2. View all images including the intermediate image layer

    docker images -a
    Copy the code

    3, other

    docker images -q # display only the mirror ID
    docker history -H imageserver # display the historical creation of the specified image. Parameters: -h Image size and date imageserver I made a image myself
    Copy the code

2.2 mirror search

  1. Search for MySQL mirror

    docker search mysql 
    Docker search mysql:5.7
    #docker search < image name >:
    Copy the code
  2. –filter=stars: only starts>=600 is displayed

    docker search --filter=stars=600 mysql
    Copy the code
  3. –automated: Lists only the images whose automated =OK

    docker search  --automated mysql
    Copy the code

2.3 Image download

  1. Download the latest official image (here using Redis as an example)

    docker pull redis If the version number is not specified, the latest version is pulled by defaultDocker pull redis: 5.0.7# specify the version number by quoting the version number.
    Copy the code
  2. Download all Redis images for the repository

    docker pull -a redis
    Copy the code
  3. Download the warehouse image publicly available on the Internet

    docker pull bitnami/redis # is a mirror that someone else has made and posted publicly on the Internet
    Copy the code

2.4. Mirror Deletion

  1. Deleting a Mirror

    Note :(do not delete the image forcibly. You must delete the container associated with the image before deleting it.)

    Docker rmi redis:latest
    docker rmi redis
    Copy the code
  2. Forced delete (that is, running containers are also forcibly deleted, but running containers are not deleted (tested))

    ## Force delete (for container processes running based on images)
    docker rmi -f redis
    Copy the code
  3. Delete in batches (separate images with Spaces)

    docker rmi -f redis mysql nginx
    Copy the code
  4. Example Delete all local mirrors

    docker rmi -f $(docker images -q) #$() can be simply interpreted as nested statements in SQL statements
    Copy the code

2.5. Mirror Building

Here is only a simple example, a sentence or two. Involves writing Dockerfile files.

(1) Write our Dockerfile file

from centos:latest

ENV mypath /usr
WORKDIR $mypath

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD /bin/bash
Copy the code

(2) Build an image

Docker build -f /myDocker/Dockerfile2 -t mycentos:1.3Copy the code
If dockerfile is in the current directoryDocker build -t mycentos:1.3Copy the code

Explanation:

1. Docker build is the command used to build images in docker

-f: specifies the path of the dockerfile file

3. -t: Specifies the version number of the image generation name as optional.

4, the last decimal point means in the current directory, that is, the Dockerfile directory

Docker container-related commands

3.1. View containers

1. View the running container

docker ps
Copy the code

2. View the ID of the running container

 docker ps -q
Copy the code

3. View running + historically run containers

docker ps -a
Copy the code

4. Obtain the IP address of the running container Redis

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis
Copy the code

5. View container meta information (redis is used as an example)

docker inspect myredis #myredis is myredis container name
Copy the code

It is effective when starting a cluster.

3.2. Start the container

Start the paused container

Docker start < container name | | container id >Copy the code

Restart the container

docker restart redis  # < container name | | container id >
Copy the code

Here is an example of starting a Redis container

docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name myredis -v /home/redis/myredis/myredis.conf:/etc/redis/redis.conf -v /home/redis/myredis/data:/data -d redis redis-server /etc/redis/redis.conf  --appendonly yes  --requirepass 000415
Copy the code
  1. –restart=always Always starts on the machine

  2. — Log is for logging

  3. -p 6379:6379 Mounts port 6379

  4. –name: Give the container a name

  5. -v Mount the data volume

    • / home/redis/myredis myredis. Conf: / etc/redis/redis. Conf. Here is the Linux directory path myredis under the conf and redis redis. Conf mount together.
    • / home/redis/myredis/data: / data the same as above
  6. -d redis Indicates that background Redis is enabled

  7. Redis-server /etc/redis/redis.conf Start redis with the configuration file and load the conf file in the container. Eventually found is mounted directory/etc/redis/redis conf is under Linux/home/redis/myredis myredis. Conf

  8. — appendOnly yes enables Redis persistence

  9. ==– requirePass 000415 Set password == (if you are using an internal Docker container, do not set it. But if you want to open to the outside, be sure to set, I have been engaged, you can see this article “Ali cloud server poisoning ‘Kirito666’ experience”)

  10. Successful interface

3.3. Container processes

##top support ps command parameters, format:
docker top [OPTIONS] CONTAINER [ps OPTIONS] 
## Lists the processes running in the Redis container
docker top redis Check all processes running the container
Copy the code

3.4 Stop and delete containers

  1. Stop a running container

    docker stop myredis #stop can be followed by either the container name or the container ID
    Copy the code
  2. Kill a running container

    docker kill myredis # same as above
    Copy the code

  1. Delete a stopped container

    docker rm myreis # same as above
    Copy the code
  2. Delete a runtime container

    docker rm -f myredis # same as above
    Copy the code

  1. Delete all containers

    docker rm -f $(docker ps -a -q) #$() You can understand nested statements in SQL statements
    Copy the code

Docker view run logs

Basic format: Docker logs [OPTIONS] CONTAINER

OPTIONS: Available parameters see the following figure.

  • — Details Displays more information
  • -f, –follow Tracks real-time logs
  • –since string Displays logs since a timestamp, or relative time, such as 30m (30 minutes)
  • –tail String How many lines of the log are displayed from the end of the log. The default is all
  • -t, –timestamps displays timestamps
  • — Until string Displays logs from before a timestamp, or relative time, such as 30m (30 minutes)

For example:

4.1. View all run logs of the container:

Docker logs < container id>Copy the code

4.2 View the log. Only the last 200 lines are displayed

docker logs --tail 200 CONTAINER_ID
Copy the code

4.3 View logs generated in the last 30 minutes

docker logs --since 30m CONTAINER_ID
Copy the code

4.4 View logs generated after a certain time

dockere logs -t --since "2021-07-31T10:35:10" d0e #d0e is my container ID
Copy the code

4.5. View logs generated during a period

docker logs -t --since "2021-07-31T10:35:10" --until "2021-07-31T10:36:10" CONTAINER_ID
Copy the code

Five, talk to yourself

It is another day to continue to roll inside, ah, yesterday I saw group chat, the big guy inside is still discussing the problem early in the morning, to tell the truth, I am a little afraid. I don’t know if you’re inviting anyone to sleep. ☠ 😇

Or 🛌. (I still think 🛌 more cool ah, alas, but can not choose ah)