Docker common commands

Container lifecycle management commands

run

Create a new container.

# Start a container using docker image nginx:latest background mode,
# And name the container mynginx.  
docker run --name mynginx -d nginx:latest  

# Use the image nginx:latest to start a container in background mode,
# Map port 80 of the container to port 80 of the host,
# The directory / data of the host is mapped to / data of the container.  
docker run -p 80:80 -v /data:/data -d nginx:latest  

# Start a container in interactive mode using the mirror nginx:latest,
# Execute the / bin/bash command inside the container.  
docker run -it nginx:latest /bin/bash  

start/stop/restart

  • docker start: start one or more containers that have been stopped.
  • docker stop: stops a running container.
  • docker restart: restart the container.

kill

Kill a running container. Optional parameters:

  • -s: what signals are sent to the container? The default is KILL
# Kill the container according to its name  
docker kill tomcat7  

# Kill the container according to the container ID  
docker kill 65d4a94f7a39  

rm

Delete one or more containers.

# Forced deletion of containers db01 and db02:  
docker rm -f db01 db02  

# Delete the container nginx01 and delete the data volumes mounted by the container:  
docker rm -v nginx01  

# Delete all stopped containers:  
docker rm $(docker ps -a -q)  

create

Create a new container without starting it.

# Create a container using docker image nginx:latest, and name the container mynginx  
docker create --name mynginx nginx:latest   

exec

Execute the command in the running container. Optional parameters:

  • -d: separation mode: running in the background
  • -i: keep STDIN open even if there is no attachment
  • -t: assign a pseudo terminal
# Execute the in container / root/nginx.sh script in interactive mode in the container mynginx  
docker exec -it mynginx /bin/sh /root/nginx.sh  

# Open an interactive terminal in the container mynginx  
docker exec -i -t  mynginx /bin/bash  

# You can also view the container that is already running through the docker ps -a command, and then use the container ID to enter the container.  
docker ps -a   
docker exec -it 9df70f9a0714 /bin/bash  

pause/unpause

  • docker pause: pauses all processes in the container.
  • docker unpause: recover all processes in the container.
# Pause the service provided by the database container db01.  
docker pause db01  

# The recovery database container db01 provides services  
docker unpause db0  

Container operation command

ps

List containers. Optional parameters:

  • -a: display all containers, including those not running.
  • -f: filter the displayed content according to conditions.
  • – format: Specifies the template file for the return value.
  • -l: displays recently created containers.
  • -n: list the n recently created containers.
  • – no TRUNC: do not truncate the output.
  • -q: silent mode, only the container number is displayed.
  • -s: displays the total file size.
# Lists all running container information.  
docker ps  

# Lists the information of the five containers that were recently created.  
docker ps -n 5  

# Lists all created container ID s.  
docker ps -a -q  

Supplementary notes:
The container has seven statuses: created, restarting, running, removing, paused, exited, and dead.

inspect

Gets the metadata of the container / image. Optional parameters:

  • -f: Specifies the template file for the return value.
  • -s: displays the total file size.
  • – type: returns JSON for the specified type.
# Get the meta information of mysql:5.7 image.  
docker inspect mysql:5.7  

# Get the IP of the running container mysql.  
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mymysql 

top

View the process information running in the container and support ps command parameters.

# View the process information of MySQL container.  
docker top mymysql  

# View the process information of all running containers.  
for i in  `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done  

events

Get real-time events. Parameter Description:

  • -f: Filter events according to conditions;
  • – since: displays all events after the specified timestamp;
  • – until: the running time is displayed until the specified time;
# Displays all events after docker July 1, 2016.  
docker events  --since="1467302400"  

# The docker image is displayed as MySQL: 5.6 related events after July 1, 2016.  
docker events -f "image"="mysql:5.6" --since="1467302400" 

Note: if the specified time is to the second level, you need to convert the time into a timestamp. If the time is a date, it can be used directly, such as – since = "2016-07-01".

logs

Get the log of the container. Parameter Description:

  • -f: trace log output
  • – since: displays all logs for a certain start time
  • -t: display timestamp
  • – tail: only the latest N container logs are listed
# Trace and view the log output of the container mynginx.  
docker logs -f mynginx  

# View the latest 10 logs of container mynginx since July 1, 2016.  
docker logs --since="2016-07-01" --tail=10 mynginx  

export

Export the file system as a tar archive to STDOUT. Parameter Description:

  • -o: write the input to the file.
# Save the container with id a404c6c174a2 as a tar file by date.  
docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2  

ls mysql-`date +%Y%m%d`.tar  

port

Lists the port mappings for the specified container.

# View the port mapping of container mynginx.  
docker port mymysql  

Container rootfs command

commit

Create a new image from the container. Parameter Description:

  • -a: the author of the submitted image;
  • -c: use the Dockerfile instruction to create an image;
  • -m: description at the time of submission;
  • -p: pause the container during commit.
# Save the container a404c6c174a2 as a new image,
# And add submitter information and description information.  
docker commit -a "guodong" -m "my db" a404c6c174a2  mymysql:v1   

cp

Used to copy data between container and host. Parameter Description:

  • -L: keep links in source and target
# Copy the host / www/runoob directory to the container 96f7f14e99ab's / www directory.  
docker cp /www/runoob 96f7f14e99ab:/www/  

# Copy the host / www/runoob directory to the container 96f7f14e99ab, and rename the directory www.  
docker cp /www/runoob 96f7f14e99ab:/www  

# Copy the container 96f7f14e99ab's / www directory to the host's / tmp directory.  
docker cp  96f7f14e99ab:/www /tmp/  

diff

Check for changes to the file structure in the container.

# View the file structure changes of MySQL container.  
docker diff mymysql  

Mirror warehouse command

login/logout

docker login: log in to a docker image warehouse. If no image warehouse address is specified, it defaults to the official warehouse. Docker Hubdocker logout: log out of a docker image warehouse. If no image warehouse address is specified, it defaults to the official warehouse. Docker Hub Parameter Description:

  • -u: login user name
  • -p: login password
# Log in to Docker Hub  
docker login -u user name -p password  

# Log out of Docker Hub  
docker logout  

pull

Pull or update the specified image from the image warehouse. Parameter Description:

  • -a: pull all tagged images
  • – disable content trust: ignores the verification of the image and is enabled by default
# Download the latest version of java image from Docker Hub.  
docker pull java  

# Download all images with REPOSITORY as java from Docker Hub.  
docker pull -a java  

push

To upload the local image to the image warehouse, log in to the image warehouse first. Parameter Description:

  • – disable content trust: ignores the verification of the image and is enabled by default
# Upload the local image myapache:v1 to the image warehouse.  
docker push myapache:v1  

search

Find the image from Docker Hub. Parameter Description:

  • – automated: only images of automated build type are listed;
  • – no TRUNC: displays the complete image description;
  • -F < filter condition >: lists the mirrors of the specified condition.
# Find all images whose image names contain java and the number of collections is greater than 10 from the Docker Hub  
docker search -f stars=10 java  

NAME                  DESCRIPTION                           STARS   OFFICIAL   AUTOMATED  
java                  Java is a concurrent, class-based...   1037    [OK]         
anapsix/alpine-java   Oracle Java 8 (and 7) with GLIBC ...   115                [OK]  
develar/java                                                 46                 [OK]  

Parameter description of each column:

  • NAME: the NAME of the mirror warehouse source
  • DESCRIPTION: DESCRIPTION of the image
  • OFFICIAL: is it officially released by docker
  • stars: similar to the star in Github, which means like and like
  • AUTOMATED: automatic build

Local mirror management commands

images

Lists the local mirrors. Parameter Description:

  • -a: list all local images (including the intermediate image layer. By default, the intermediate image layer is filtered out);
  • – digests: displays the summary information of the image;
  • -f: display the image that meets the conditions;
  • – format: Specifies the template file of the return value;
  • – no TRUNC: display complete image information;
  • -q: only the image ID is displayed.
# View the list of local mirrors.  
docker images  

# Lists the images in the local image whose REPOSITORY is ubuntu.  
docker images  ubuntu  

rmi

Delete one or more local mirrors. Parameter Description:

  • -f: forced deletion;
  • – no prune: the process image that does not remove the image is removed by default;
# Force deletion of local image guodong/ubuntu:v4.  
docker rmi -f guodong/ubuntu:v4

tag

Mark the local image and group it into a warehouse.

# Mark the image ubuntu:15.10 as a runoob/ubuntu:v3 image.  
docker tag ubuntu:15.10 runoob/ubuntu:v3  

build

Used to create an image using Dockerfile. Parameter Description:

  • – build Arg = []: set the variables when creating the image;
  • – cpu shares: set cpu usage weight;
  • – CPU period: limit CPU CFS cycles;
  • – CPU quota: limit CPU CFS quota;
  • – cpuset CPUs: Specifies the CPU id used;
  • – cpuset MEMS: Specifies the memory id used;
  • – disable content trust: ignore verification and enable by default;
  • -f: specify the Dockerfile path to use;
  • – force RM: delete intermediate containers during the setting of mirroring;
  • – isolation: use container isolation technology;
  • – label = []: sets the metadata used by the image;
  • -m: set the maximum memory value;
  • – memory swap: set the maximum value of swap to memory + swap, "- 1" means unlimited swap;
  • – no cache: no cache is used during image creation;
  • – pull: try to update the new version of the image;
  • – quiet, -q: quiet mode. Only the image ID is output after success;
  • – rm: delete the intermediate container after setting the image successfully;
  • – SHM size: set the size of / dev/shm. The default value is 64M;
  • – ulimit: ulimit configuration.
  • – Square: compress all operations in Dockerfile into one layer.
  • – tag, -t: the name and label of the image, usually in the format of name:tag or name; You can set multiple labels for a mirror in a single build.
  • – network: default. Set the network mode of the RUN command during build
# Create an image using the Dockerfile of the current directory, labeled runoob/ubuntu:v1  
docker build -t runoob/ubuntu:v1 .   

# Create an image using the Dockerfile of the URL github.com/cracker/docker-firefox  
docker build github.com/creack/docker-firefox  

# Create an image from the location of the - f Dockerfile file  
docker build -f /path/to/a/Dockerfile .  

history

View the creation history of the specified mirror. Parameter Description:

  • -H: print the image size and date in a readable format. The default is true;
  • – no TRUNC: display the complete submission record;
  • -q: only submit record ID s are listed.
# View the creation history of the local image guodong/ubuntu:v3.  
docker history guodong/ubuntu:v3  

save

Save the specified image as a tar archive. Parameter Description:

  • -o: File exported to.
# Generate my image from runoob/ubuntu:v3_ ubuntu_ V3.tar document  
docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3  

load

Import the image exported using the docker save command. Parameter Description:

  • – input, - I: Specifies the imported file instead of STDIN.
  • – quiet, - Q: compact output information.
# Import mirror  
docker load --input fedora.tar  

import

Create a mirror from the archive. Parameter Description:

  • -c: create an image using the docker instruction;
  • -m: description at the time of submission;
# From mirror archive my_ubuntu_v3.tar creates an image named runoob/ubuntu:v4  
docker import  my_ubuntu_v3.tar runoob/ubuntu:v4    

Basic version information command

info

Displays Docker system information, including the number of images and containers.

# View docker system information.  
docker info 

version

Displays Docker version information.

docker version 

Ref:

Tags: Docker

Posted on Sun, 05 Dec 2021 17:31:06 -0500 by cordex