docker basic management commands

Posted by Jakei on Sun, 16 Jan 2022 20:38:35 +0100

catalogue

1, docker basic commands

1. View images - docker images

2. View all status containers - docker ps -a

3. docker -- run instruction

Workflow

4. View docker version command - docker -v

5. View docker information - docker

6. Docker help command document -- docker --help

2, docker image operation

1. Search image (public warehouse) - docker search

2. Download Image - docker pull

3. View image list - docker images

4. Get image information - docker inspect

5. Add image tag - docker tag

6. Delete image - docker rmi

7. Batch delete image

8. Export / import image - docker save/load

3, Container operation

1. Query the running status of all containers - docker ps -a

2. Create container - docker create

3. Start container - docker start

4. Start container (one-time execution) - docker run

5. View container ip address - docker inspect

6. Enter container - docker exec

7. Container export / import - docker export

8. Delete container - docker rm -f

9. View the resource status consumed by docker

summary

I docker Basic command

1. View images - docker images

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#View all images downloaded locally</span>
docker images


REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    ae2feff98a0c   9 days ago   133MB
--------------------------------------------------------------------------------
REPOSITORY: The warehouse to which the image belongs;
TAG: The label information of the image, marking different images in the same warehouse;
IMAGE ID: Mirror unique ID Number, which uniquely identifies a mirror image md5 The mode is encrypted;
CREATED: Image creation time;
VIRTUAL SIZE: Image size;
--------------------------------------------------------------------------------
</code></span>

2. View all status containers - docker ps -a

<span style="background-color:#f5f7ff"><code class="language-bash">docker <span style="color:#3d8fd1">ps</span> -a    <span style="color:#6b7394">#-The a option displays all containers 	</ span>

CONTAINER ID   IMAGE         COMMAND                  CREATED        STATUS                      PORTS     NAMES
58a0aad139d3   nginx         <span style="color:#ac9739">"/docker-entrypoint...."</span>   2 hours ago    Exited <span style="color:#999999">(</span>0<span style="color:#999999">)</span> 21 minutes ago             dazzling_wescoff

<span style="color:#6b7394">##Field description</span>
CONTAINER ID: Container ID number
IMAGE: Loaded image
COMMAND : Running program
CREATED : Creation time
STATUS: Current status
PORTS: Port mapping
NAMES: name
</code></span>

3. docker -- run instruction

<span style="background-color:#f5f7ff"><code>docker run hello-world
</code></span>

Run instruction run

① * * check * * whether the image exists locally (if not, download it directly from the docker hub)

② Create (create the image as a container) + start runs the created container

Workflow

  1. The docker client is connected to the server (the server runs in the operating system as a daemon) restful api, a typical c/s architecture
  2. The daemon on the docker server downloads the image from the docker hub (Ps: the server will first check whether the local system has this image)
  3. The server creates a new container, and then starts a container from the removed image. The container executes a script / executable program so that we can view / use (client)
  4. The docker server returns these information flows (transfers) to the client and displays them (on the terminal)
    docker client can take many forms, such as the terminal where the "docker" command tool is located

4. View docker version command - docker -v

<span style="background-color:#f5f7ff"><code>docker version 
docker -v   
</code></span>

5. View docker information - docker

6. Docker help command document -- docker --help

<span style="background-color:#f5f7ff"><code>docker --help
</code></span>

2, docker image operation

1. Search image (public warehouse) - docker search

By default, it is found in the public warehouse. If there is a private warehouse, it will be found in the private warehouse

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker search keyword</span>
<span style="color:#6b7394">#Example:</span>
docker search nginx 
docker search centos: 7
->At the same time, we can also make our own images and push them to docker hub upper
</code></span>

2. Download Image - docker pull

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker pull warehouse name [: label]</span>
<span style="color:#6b7394">#If you do not specify a label when downloading an image, the latest version of the image in the warehouse will be downloaded by default, that is, the label selected is the latest label</ span>
docker pull centos:7
docker pull nginx
</code></span>

3. View image list - docker images

<span style="background-color:#f5f7ff"><code>docker images
</code></span>

4. Get image information - docker inspect

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker inspect image ID</span>
<span style="color:#6b7394">#Example: viewing nginx image information</span>
docker insect 605c77e624dd
</code></span>

5. Add image tag - docker tag

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker tag name: [old tag] new name: [new tag]</span>
<span style="color:#6b7394">#Example:</span>
docker tag nginx: latest nginx:lnmp  <span style="color:#6b7394">#Label nginx lnmp. The original label was latest</span>

</code></span>

6. Delete image - docker rmi

docker rmi is short for docker rm image

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format:</span>
docker rmi Warehouse name:label	 <span style="color:#6b7394">#When a mirror has multiple labels, only the specified labels are deleted</span>
docker rmi image ID number	   <span style="color:#6b7394">#The mirror is completely deleted</span>
</code></span>

7. Batch delete image

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#docker images -q can load the image ID</span>

<span style="color:#6b7394">#Batch delete all images</span>
docker rmi <span style="color:#ee9900">`docker images -q`</span>

<span style="color:#6b7394">#Batch delete nginx image</span>
docker rmi <span style="color:#ee9900">`docker images<span style="color:#ac9739">|</span><span style="color:#3d8fd1">grep</span> <span style="color:#ac9739">"nginx"</span>`</span>
</code></span>

8. Export / import image - docker save/load

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Export mirror</span>
<span style="color:#6b7394">#Format: docker save -o saves the image saved by the file name</span>
docker save -o nginx_v1 nginx:latest			<span style="color:#6b7394">#The saved image is named nginx and exists in the current directory</span>
<span style="color:#3d8fd1">scp</span> nginx_v1 @root:192.168.59.111:/opt   <span style="color:#6b7394">#Export the exported image to another server in scp mode</span>

<span style="color:#6b7394">#Images can be imported from different places, but there must be a docker engine, and the version cannot be too different</span>
<span style="color:#6b7394">#Format: docker load < saved file</span>
docker load <span style="color:#ac9739"><</span> nginx_v1 
</code></span>

3, Container operation

1. Query the running status of all containers - docker ps -a

<span style="background-color:#f5f7ff"><code class="language-bash">docker <span style="color:#3d8fd1">ps</span> -a

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS    PORTS     NAMES
8b0a7be0ff58   nginx:latest   <span style="color:#ac9739">"/docker-entrypoint...."</span>   57 seconds ago   Created             inspiring_swanson

Container ID number	   Loaded image     Running program               Creation time       Current status port mapping name
</code></span>

2. Create container - docker create

<span style="background-color:#F5f7ff "> < code class =" language bash "> the newly created container is stopped by default and does not run any program. A process needs to be initiated to start the container.

<span style="color:#6b7394">#Format: docker create [options] image</span>
<span style="color:#6b7394">#Common options:</span>
-i: Keep the input of the container open
-t: Give Way Docker Assign a pseudo terminal

<span style="color:#6b7394">#Example:</span>
docker create -it nginx:latest /bin/bash
</code></span>

3. Start container - docker start

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker start container ID / name</span>
docker start b2a57b3ea48a
docker <span style="color:#3d8fd1">ps</span> -a
</code></span>

4. Start container (one-time execution) - docker run

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Add the - d option to make the Docker container run in the background in the form of daemon. And the program that the container is running cannot end</ span>

<span style="color:#6b7394">#Example 1:</span>
docker run -itd nginx:latest /bin/bash

<span style="color:#6b7394">#Example 2: exit after execution</span>
docker run centos:7 /usr/local/bash -c <span style="color:#3d8fd1">ls</span> /   

<span style="color:#6b7394">#Example 3: execute persistent tasks as a daemon without exiting after execution</span>
docker run -d centos:7 /usr/local/bash -c <span style="color:#ac9739">"while true;do echo hello;done"</span> 
</code></span>

​​​​​​​

5. View container ip address - docker inspect

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker inspect container ID</span>
docker <span style="color:#3d8fd1">ps</span> -a   <span style="color:#6b7394">#First view the ID of the runtime container</span>
docker inspect ba8d61d35e32
</code></span>

6. Enter container - docker exec

The container state entering the container must be the up state

And shell are two running modes

  • docker run -it will create the foreground process, but will terminate the process after entering exit.

  • docker attach connects to the input / output stream in the container by connecting stdin, and terminates the container process after entering exit

  • docker exec -it will connect to the container. You can enter the container and operate like sh. you can exit the container through exit without affecting the operation of the container.

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#When you need to enter the container for command operations, you can use the docker exec command to enter the running container</ span>

<span style="color:#6b7394">#Format: docker exec -it container ID / name / bin / Bash</span>
-i Option means to keep the input of the container open;
-t Option means let Docker Assign a pseudo terminal.

<span style="color:#6b7394">#Example: enter (three ways)</span>
docker run -itd centos:7 /bin/bash  <span style="color:#6b7394">#Run container first</span>
docker <span style="color:#3d8fd1">ps</span> -a 
①use run Enter, you can use ctrl+d Exit, exit the terminal directly
docker run -it centos:7 /bin/bash 

②To permanently enter, exit or run, use docker <span style="color:#3d8fd1">exec</span>
docker <span style="color:#3d8fd1">ps</span> -a 
docker <span style="color:#3d8fd1">exec</span> -it b99e0771c4e1  /bin/bash

③docker attach,Will connect via stdin,Connected to the input / output stream in the container, public input exit Terminate the container process after (temporary, not recommended)

</code></span>

① Using run to enter is a one-time entry

② Permanent entry, using docker exec

7. Container export / import - docker export

Users can migrate any Docker container from one machine to another. During the migration process, you can use the docker export command to export the created container as a file, regardless of whether the container is running or stopped. Export files can be transferred to other machines, and container migration can be realized through corresponding import commands.

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Export format: docker export container ID / name > file name</span>
docker <span style="color:#3d8fd1">export</span> b99e0771c4e1 <span style="color:#ac9739">></span> centos_7

<span style="color:#6b7394">#Import format: cat file name | docker import – image name: Label</span>
method①
docker <span style="color:#3d8fd1">import</span> centos_7  centos: v1	<span style="color:#6b7394">#Images are generated after import, but containers are not created</span>
method②
<span style="color:#3d8fd1">cat</span> centos_7 <span style="color:#ac9739">|</span>docker <span style="color:#3d8fd1">import</span> - centos:v2


</code></span>

8. Delete container - docker rm -f

<span style="background-color:#f5f7ff"><code class="language-bash"><span style="color:#6b7394">#Format: docker rm [-f] container ID / name</span>

1.<span style="color:#6b7394">#You cannot delete a running container. You can only -f force it to be deleted, or stop it before deleting it</span>
docker <span style="color:#3d8fd1">rm</span> 3224eb044879

2.<span style="color:#6b7394">#Containers that have exited can be deleted directly</span>
docker <span style="color:#3d8fd1">rm</span> 1270a6791069 

3.<span style="color:#6b7394">#Delete based on name matching</span>
docker <span style="color:#3d8fd1">rm</span> -f distracted_panini


4.<span style="color:#6b7394">#Delete all running containers</span>
docker <span style="color:#3d8fd1">rm</span> -f <span style="color:#ee9900">`docker <span style="color:#3d8fd1">ps</span> -q`</span>

5.<span style="color:#6b7394">#Delete all containers</span>
docker <span style="color:#3d8fd1">rm</span> -f <span style="color:#ee9900">`docker <span style="color:#3d8fd1">ps</span> -aq`</span>

6.<span style="color:#6b7394">#Selective batch deletion (regular matching)</span>
docker <span style="color:#3d8fd1">ps</span> -a l <span style="color:#3d8fd1">awk</span> <span style="color:#ac9739">' {print "docker rm "<span style="color:#ee9900">$1</span>}'</span>l <span style="color:#3d8fd1">bash</span>

7.<span style="color:#6b7394">#Delete container in exit state</span>
<span style="color:#6679cc">for</span> i <span style="color:#6679cc">in</span> `dockef <span style="color:#3d8fd1">ps</span> -a l <span style="color:#3d8fd1">grep</span> -i <span style="color:#6679cc">exit</span> / <span style="color:#3d8fd1">awk</span> <span style="color:#ac9739">'{print <span style="color:#ee9900">$1</span>}'</span> '<span style="color:#999999">;</span> <span style="color:#6679cc">do</span> docker <span style="color:#3d8fd1">rm</span> -f <span style="color:#ee9900">$i</span><span style="color:#999999">;</span><span style="color:#6679cc">done</span>
</code></span>

1. # you cannot delete a running container. You can only -f force it to be deleted, or stop it first and then delete it

2. # exiting containers can be deleted directly

9. View the resource status consumed by docker

<span style="background-color:#f5f7ff"><code>docker stats
</code></span>

summary

docker version query system version

docker info query the total number of containers (running, stopped, ready)

Containers: Running: Paused: Paused: Stopped

docker search nginx (image name)

docker pull (image name): (version) download the image (search on the dockerhub official website)

docker images view images

Linux distribution image: alpine pentos redhat Debian (apt package management, centos is rpm) production environment may be used

docker inspect (image name): (version) view image resource details

docker ps -a view container id

docker rm (container id) deletes containers (only exited containers can be deleted -f forcibly)

docker rmi (image name): (version number) delete the image

docker rmi docker images -q deletes all images based on the image id

docker save -o file name image name image export

Docker load < image file name image import

docker create -it image name: Version / bin/bash create image

docker start container id start container

docker run image name: Version / usr/bin/bash -c ls / create and start container

docker run image name: Version / usr/bin/bash -c "while true;do echo hello;done" keeps running in the background

docker run -it image name: version number / bin/bash enter container

docker exec -it container id /bin/bash enters the container (the container must be running)

docker export container ID > file name container export

docker import export file name specifies the image name container import (the imported image will be the image)

Proportion of docker stats container resources

Topics: Operation & Maintenance Docker Container