Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Container lifecycle management commands

run

Create a new container.

Start a container in backend mode and name it mynginx.
docker run --name mynginx -d nginx:latest

Start a container in backend mode, map port 80 of the container to port 80 of the host, and map the directory /data of the host to /data of the container.
docker run -p 80:80 -v /data:/data -d nginx:latest

Start a container in interactive mode and run the /bin/bash command inside the container.
docker run -it nginx:latest /bin/bash
Copy the code

start/stop/restart

  • Docker start: Starts one or more containers that have been stopped.
  • Docker stop: Stops a running container.
  • Docker restart: Restarts the container.
Start the stopped container mynginx
docker start mynginx

# Stop the running container mynginx
docker stop mynginx

# restart mynginx
docker restart mynginx
Copy the code

kill

Kill a running container.

Optional parameters:

  • -s: What signal is sent to the container. The default is KILL
Kill the container by its name
docker kill tomcat7

Kill the container based on its ID
docker kill 65d4a94f7a39
Copy the code

rm

Delete one or more containers.

Db01; db02;
docker rm -f db01 db02


# delete nginx01 from nginx01;
docker rm -v nginx01

# delete all stopped containers:
docker rm $(docker ps -a -q)
Copy the code

create

Create a new container without starting it.

# create a container with docker image nginx:latest and name it mynginx
docker create --name mynginx nginx:latest   
Copy the code

exec

Execute commands in a running container.

Optional parameters:

  • -d: Separated mode: Runs in the background
  • -I: Keeps STDIN open even without attaching
  • -t: Allocates a dummy terminal
# execute the /root/nginx.sh script in mynginx in interactive mode
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 use the docker ps -a command to view containers that are already running, and then use the container ID to enter the container.
docker ps -a 
docker exec -it 9df70f9a0714 /bin/bash
Copy the code

pause/unpause

  • Docker pause: Pauses all processes in a container.
  • Docker unpause: Restores all processes in a container.
Suspend service of the database container DB01.
docker pause db01

Restore service provided by database container DB01
docker unpause db0
Copy the code

Container operation command

ps

List containers.

Optional parameters:

  • -a: Displays all containers, including those that are not running.
  • -f: filters the displayed content based on conditions.
  • –format: specifies the return value of the template file.
  • -l: displays the newly created container.
  • -n: lists the n containers created recently.
  • –no-trunc: does not truncate the output.
  • -q: displays only container ids in silent mode.
  • -s: Displays the total file size.
List all running containers.
docker ps

List the five containers that were created recently.
docker ps -n 5

# list all created container ids.
docker ps -a -q
Copy the code

Supplementary notes:

The seven states of a container are created, Restarting, running, removing, paused, exited, and dead.

inspect

Gets the metadata of the container/image.

Optional parameters:

  • -f: specifies the returned value of the template file.
  • -s: Displays the total file size.
  • –type: Returns JSON for the specified type.
Mysql: select * from mysql:5.7;Docker inspect mysql: 5.7Get the IP of the running container mymysql.
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mymysql
Copy the code

top

View information about processes running in a container. The ps command is supported.

Mysqld = mymysql;
docker top mymysql

# check all processes running the container.
for i in  `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done
Copy the code

events

Get real-time events.

Parameter Description:

  • -f: filters events based on conditions.
  • –since: displays all events from the specified timestamp;
  • –until: Running time displays until the specified time;
# show all docker events since July 1, 2016.
docker events  --since="1467302400"

# show docker image as mysql:5.6 related events after July 1, 2016
docker events -f "image"="Mysql: 5.6" --since="1467302400" 
Copy the code

Description:

If the specified time is in seconds, the time needs to be converted to a timestamp. If the time is a date, you can use it directly, for example –since=”2016-07-01″.

logs

Get the container’s logs.

Parameter Description:

  • -f: tracks the output of logs
  • –since: Displays all logs from a start time
  • -t: displays the timestamp
  • –tail: Lists only the latest N container logs
# trace the output of mynginx.
docker logs -f mynginx

# check out the latest 10 mynginx logs from July 1, 2016.
docker logs --since="2016-07-01" --tail=10 mynginx
Copy the code

export

Export the file system to STDOUT as a tar archive.

Parameter Description:

  • -o: writes the input content to the file.
# save a404C6C174A2 as tar file by date.
docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2

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

port

Lists the port mappings for the specified container.

Mynginx port mapping
docker port mymysql
Copy the code

Container rootfs command

commit

Create a new image from the container.

Parameter Description:

  • -a: submits the mirror author.
  • -c: use the Dockerfile command to create an image.
  • -m: Description of submission;
  • -p: Suspends the container at commit time.
Save container A404C6C174A2 as a new image and add submitter information and description information.
docker commit -a "guodong" -m "my db" a404c6c174a2  mymysql:v1 
Copy the code

cp

Used to copy data between containers and hosts.

Parameter Description:

  • -l: preserves the link in the source target
# copy host/WWW /runoob to/WWW in container 96f7f14e99ab
docker cp /www/runoob 96f7f14e99ab:/www/

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

# copy the/WWW directory of container 96f7f14e99ab to/TMP on the host.
docker cp  96f7f14e99ab:/www /tmp/
Copy the code

diff

Check for changes to the file structure in the container.

# check the file structure of the container mymysql.
docker diff mymysql
Copy the code

Mirror Warehouse command

login/logout

Docker login: login to a Docker repository. If the address of the repository is not specified, the default is the official repository docker Hub

Docker logout: Logs out a Docker repository. If the address of the repository is not specified, the repository is the official Docker Hub by default

Parameter Description:

  • -u: indicates the login user name
  • -p: indicates the login password
# Log in to Docker HubDocker login -u Username -p Password# log out of Docker Hub
docker logout
Copy the code

pull

Pulls or updates the specified image from the mirror repository.

Parameter Description:

  • -a: Pulls all tagged mirrors
  • –disable-content-trust: indicates that mirror verification is ignored. This function is enabled by default
Download the latest version of Java image from Docker Hub.
docker pull java

Download REPOSITORY from Docker Hub for all images of Java.
docker pull -a java
Copy the code

push

To upload a local image to the image repository, log in to the image repository first.

Parameter Description:

  • –disable-content-trust: indicates that mirror verification is ignored. This function is enabled by default
# upload myapache:v1 to the mirror repository.
docker push myapache:v1
Copy the code

search

Find the image from the Docker Hub.

Parameter Description:

  • — Automated: Lists only images of the automated Build type.
  • –no-trunc: displays a complete image description.
  • -f < Filter criteria >: lists the mirrors under the specified conditions.
Docker Hub = Docker Hub = Docker Hub = Docker Hub = 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]
Copy the code

Parameter description of each column:

  • NAME: indicates the NAME of the mirror repository source
  • DESCRIPTION: indicates the DESCRIPTION of the mirror
  • OFFICIAL: Is it OFFICIAL with Docker
  • Stars: Similar to Github star, which means to like or like
  • AUTOMATED: Automatic construction

Local mirror management commands

images

List local mirrors.

Parameter Description:

  • -a: lists all local images (including the intermediate image layer, which is filtered out by default).
  • –digests: Ests that display ests;
  • -f: displays mirrors that meet the conditions.
  • –format: specifies the return value of the template file;
  • –no-trunc: displays complete mirror information.
  • -q: Displays only the mirror ID.
View the list of local mirrors.
docker images

# list of ubuntu images with REPOSITORY in the local image.
docker images  ubuntu
Copy the code

rmi

Example Delete one or more local mirrors.

Parameter Description:

  • -f: Forcible deletion.
  • –no-prune: does not remove the process mirror of this mirror.
# Force delete local image guodong/ Ubuntu :v4.
docker rmi -f guodong/ubuntu:v4
Copy the code

tag

Mark the local image to a repository.

Runoob/Ubuntu: V3Docker tag ubuntu: runoob/ubuntu 15.10: the v3Copy the code

build

Used to create an image using Dockerfile.

Parameter Description:

  • –build-arg=[] : sets variables for image creation;
  • –cpu-shares: sets CPU usage weight;
  • –cpu-period: limits the CPU CFS period.
  • Cpu-quota: limits the CPU CFS quota.
  • –cpuset-cpus: specifies the CPU ID to be used.
  • –cpuset-mems: specifies the memory ID to be used.
  • –disable-content-trust: disables verification and is enabled by default.
  • -f: specifies the Dockerfile path to use.
  • –force-rm: delete the intermediate container during the mirroring process.
  • — Isolation: use container isolation technology;
  • –label=[] : sets the metadata used by the mirror;
  • -m: Sets the maximum memory size.
  • –memory-swap: Set the maximum value of swap to memory +swap. “-1” indicates no limit on swap.
  • –no-cache: the process of creating a mirror does not use cache.
  • –pull: Try to update a new version of the image;
  • –quiet, -q: indicates the quiet mode. If the mode succeeds, only the image ID is displayed.
  • –rm: Delete the intermediate container after setting the mirror successfully.
  • –shm-size: set the size of /dev/shm. The default value is 64 MB.
  • –ulimit: ulimit configuration.
  • — Squash: Squash all operations in a Dockerfile into a single layer.
  • –tag, -t: indicates the name and label of the image. Usually, name is in the tag or name format. Multiple labels can be set for a single image in a single build.
  • –network: default. Set the network mode of the RUN directive during build
# create an image using the current directory Dockerfile labeled runoob/ Ubuntu :v1
docker build -t runoob/ubuntu:v1 . 

Create an image using the Dockerfile at github.com/creack/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 .
Copy the code

history

View the creation history of a specified mirror.

Parameter Description:

  • -h: prints the image size and date in a readable format. The default value is true.
  • –no-trunc: Displays complete commit records.
  • -q: Lists only the ID of the submission record.
# view the creation history of guodong/ Ubuntu :v3
docker history guodong/ubuntu:v3
Copy the code

save

Saves the specified image as a tar archive.

Parameter Description:

  • -o: indicates the output file.
# generate my_ubuntu_v3.tar from runoob/ Ubuntu :v3
docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3
Copy the code

load

Import the image exported using the docker save command.

Parameter Description:

  • –input, -i: Specifies the file to import, instead of STDIN.
  • –quiet, -q: condenses the output information.
# import image
docker load --input fedora.tar
Copy the code

import

Create an image from the archive.

Parameter Description:

  • -c: apply the docker command to create an image.
  • -m: Description of submission;
# create an image from the image archive file my_ubuntu_v3.tar and name it runoob/ Ubuntu :v4
docker import  my_ubuntu_v3.tar runoob/ubuntu:v4  

Copy the code

Basic version information command

info

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

# check docker system info.
docker info
Copy the code

version

The Docker version information is displayed.

docker version
Copy the code