I. Command Line
You can get help information from the command line by running docker or docker help command (we take CentOS as an example):
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: builder Manage builds config Manage Docker configs container Manage containers context Manage contexts engine Manage the docker engine image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command.
Among them:
info and version are commands related to system environment information
events, history, logs are commands related to log information
Login pull push search is a D ocker H ub service-related command
The rest are routine maintenance-related commands
II. Command Grammar
Basic usage: Command + parameter
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker info Client: Debug Mode: false Server: Containers: 4 Running: 0 Paused: 0 Stopped: 4 Images: 2 Server Version: 19.03.1 ...............(ellipsis) Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Security Options: Kernel Version: 3.10.0-693.2.2.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker version Client: Docker Engine - Community Version: 19.03.1 API version: 1.40 Go version: go1.12.5 Git commit: 74b1e89e8a Built: Thu Jul 25 21:17:37 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.1 API version: 1.40 (minimum version 1.12) Go version: go1.12.5 Git commit: 74b1e89e8a Built: Thu Jul 25 21:27:55 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: v1.2.6 GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb runc: Version: 1.0.0-rc8 GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f docker-init: Version: 0.18.0 GitCommit: fec3683
View the downloaded image
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 3556258649b2 2 weeks ago 64.2MB hello-world latest fce289e99eb9 7 months ago 1.84kB learn/tutorial latest a7876479f1aa 6 years ago 128MB
The parameters of a single character can be grouped together:
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker run -t -i ubuntu bash # Using Mirrors ubuntu:latest Start a container in interactive mode,Execution in containers bash Orders,The red part represents the use of separate parameters Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 7413c47ba209: Pull complete 0fe7e7cbb2e8: Pull complete 1d425c982345: Pull complete 344da5c95cec: Pull complete Digest: sha256:c303f19cfe9ee92badbbbd7567bc1ca47789f79303ddcef56f77687d4744cd7a Status: Downloaded newer image for ubuntu:latest root@a0629a1e86e4:/# cat /etc/issue Ubuntu 18.04.2 LTS \n \l root@a0629a1e86e4:/# exit exit [root@iz2ze2bn5x2wqxdeq65wlpz ~]#
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker run -ti ubuntu bash # The labeled red part represents the combination of individual character parameters. root@5b84ee078161:/# exit exit
[root@iz2ze2bn5x2wqxdeq65wlpz ~]# docker rm imgid-1 imgid-2 # The labeled red part indicates that multiple parameters are used in one line. Error: No such container: imgid-1 Error: No such container: imgid-2
D ocker background process parameters
parameter | introduce |
---|---|
--api-enable-cors=false | Remote API calls. |
-b, --bridge="" | Bridge bridging devices on a system into Docker containers, and when none is used, the network in the containers can be deactivated. |
--bip="" | Use the CIDR address to set the IP of the network bridge. This parameter and - b cannot be used together. |
-D, --debug=false | Open Debug mode. For example: docker-d-D |
-d, --daemon=false | Open Daemon mode. |
--dns=[] | Set up the container to use the DNS server. For example: docker-d -- DNS 8.8.8.8 |
-dns-search=[] | Set the container to search for domain names using the specified DNS. For example: docker-d -- dns-search example.com |
--exec-driver="native" | The setup container uses the specified runtime driver. For example: docker-d-e LxC |
-G, --group="docker" | In the background mode, assign the specified group to the corresponding unix socket. Note that when this parameter, group, assigns an empty string, group information is removed |
-g, --graph="/var/lib/docker" | Set up the Docker runtime root directory |
-H, --host=[] | Setting the socket binding specified in background mode can bind one or more tcp://host:port, unix://path/to/socket, fd:/* or fd://socketfd. For example: $docker-H tcp://0.0.0:2375 PS or $export DOCKER_HOST= "tcp://0.0.0.0:2375"$docker PS |
-icc=true | Set up communication with inline container enabled. |
--ip="0.0.0.0" | Setting the default IP address used when the container binds IP |
--ip-forward=true | Set the net.ipv4.ip_forward of the startup container |
--iptables=true | Set the iptable rule to start the Docker container custom |
--mtu=0 | Set the MTU value of the container network. If there is no such parameter, select the default route MTU. If there is no default route, set it to the constant value 1500. |
-p, --pidfile="/var/run/docker.pid" | Set the background process PID file path. |
-r, --restart=true | Setting containers in operation before reboot |
-s, --storage-driver="" | Setting up the container runtime using the specified storage driver, such as specifying the use of device mapper, can be as follows: docker-d-s device mapper |
--selinux-enabled=false | Set up selinux support enabled |
--storage-opt=[] | Setting Storage Driver Parameters |
III. Docker Order Complete
Container Life Cycle Management run start/stop/restart kill rm pause/unpause create exec //Container operation ps inspect top attach events logs wait export port //Container rootfs command commit cp diff //Mirror Warehouse login pull push search //Local Mirror Management images rmi tag build history save load import Docker information|Edition info version
1. docker run: create a new container and run a command
Syntax: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS Notes: - a stdin: specify the standard input and output content type, and select STDIN/STDOUT/STDERR three items; - d: Run the container in the background and return the container ID; - i: Running containers in interactive mode, usually in conjunction with - t; - P: Random Port Mapping, Container Internal Port Random Mapping to High Port of Host - p: Specify port mapping in the form of host (host) port: container port - t: A pseudo-input terminal is reassigned for the container, usually used in conjunction with-i. name="nginx-lb": specify a name for the container; - dns 8.8.8.8: The DNS server used by the specified container is the same as the host by default; - dns-search example.com: Specify the container DNS search domain name, which is the same as the host by default; - h "mars": specify the host name of the container; - e username="ritchie": Set environment variables; - env-file=[]: Read environment variables from the specified file; - cpuset="0-2" or --cpuset="0,1,2": bind the container to run on the specified CPU; - m: Set the maximum memory used by the container; - net="bridge": specify the network connection type of container, support bridge/host/none/container: four types; --link=[]: Add a link to another container; Expo=[]: Open a port or a group of ports; - volume, - v: Bind a volume
Example:
Start a container in background mode using docker mirror nginx:latest and name it mynginx.
docker run --name mynginx -d nginx:latest
Start a container in background mode using mirror nginx:latest and map port 80 of the container to host random port.
docker run -P -d nginx:latest
Using mirror nginx:latest, start a container in background mode, map 80 ports of the container to 80 ports of the host, and map directory / data of the host to / data of the container.
docker run -p 80:80 -v /data:/data -d nginx:latest
Bind the 8080 port of the container and map it to the 80 port of the local host 127.0.0.1.
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
Start a container in interactive mode using mirror nginx:latest and execute the / bin/bash command in the container.
docker run -it nginx:latest /bin/bash