命令概览
Docker-hub 国内镜像
镜像地址
1) 阿里云 docker hub mirror
https://registry.cn-hangzhou.aliyuncs.com
如果有账号的, 使用:
[系统分配前缀].mirror.aliyuncs.com
具体上阿里云容器HUB控制台查看.
2) 腾讯云 docker hub mirror
https://mirror.ccs.tencentyun.com
3) 华为云
https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com
4) docker中国
https://registry.docker-cn.com
5) 网易
http://hub-mirror.c.163.com
6) daocloud
http://f1361db2.m.daocloud.io
修改步骤
直接修改 /etc/docker/daemon.json (docker 版本 >= 1.10 时) 内容为:
{"registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"]}
修改后重启服务:
systemctl daemon-reload
systemctl restart docker
Docker Toolbox 镜像源站
https://developer.aliyun.com/...
Docker CE 镜像源站
https://developer.aliyun.com/...
docker version
语义:
显示docker版本信息
Show the Docker version information
语法:
docker version [OPTIONS]
root@ubuntu01:~# docker version
Client:
Version: 20.10.12
API version: 1.41
Go version: go1.17.3
Git commit: 20.10.12-0ubuntu4
Built: Mon Mar 7 17:10:06 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server:
Engine:
Version: 20.10.12
API version: 1.41 (minimum version 1.12)
Go version: go1.17.3
Git commit: 20.10.12-0ubuntu4
Built: Mon Mar 7 15:57:50 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.9-0ubuntu3
GitCommit:
runc:
Version: 1.1.0-0ubuntu1
GitCommit:
docker-init:
Version: 0.19.0
GitCommit:
docker info
语义:
显示系统信息
Display system-wide information
语法:
docker info [OPTIONS]
root@ubuntu01:~# docker info
Client:
Context: default
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 3
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-47-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.799GiB
Name: ubuntu01
ID: 6ADA:43LE:7TEJ:YVTJ:CD44:MURH:KH2K:EQ3D:SLXU:G5CW:ZGJQ:5MXD
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://registry.cn-hangzhou.aliyuncs.com/
Live Restore Enabled: false
docker --help
语义:
帮助信息
Help about the command
语法:
docker COMMAND --help
docker --help
docker pull --help
docker image --help
docker image ls --help
docker container --help
docker container ls --help
root@ubuntu01:~# docker image --help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.
docker search
语义:
搜索镜像
Search the Docker Hub for images
语法:
docker search [OPTIONS] TERM
参数:
-f key=value : 筛选
root@ubuntu01:~# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13152 [OK]
mariadb MariaDB Server is a high performing open sou… 5024 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 622 [OK]
percona Percona Server is a fork of the MySQL relati… 584 [OK]
bitnami/mysql Bitnami MySQL Docker Image 76 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 66
linuxserver/mysql-workbench 43
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 37
ubuntu/mysql MySQL open source fast, stable, multi-thread… 36
circleci/mysql MySQL is a widely used, open-source relation… 27
google/mysql MySQL server for Google Compute Engine 21 [OK]
rapidfort/mysql RapidFort optimized, hardened image for MySQL 13
bitnami/mysqld-exporter 3
ibmcom/mysql-s390x Docker image for mysql-s390x 2
vitess/mysqlctld vitess/mysqlctld 1 [OK]
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
hashicorp/mysql-portworx-demo 0
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d… 0
cimg/mysql 0
drud/mysql 0
silintl/mysql-backup-restore Simple docker image to perform mysql backups… 0 [OK]
corpusops/mysql https://github.com/corpusops/docker-images/ 0
drud/mysql-docker-local-57 This repo has been deprecated, new tags are … 0
drud/mysql-local-57 ddev mysql local container 0
root@ubuntu01:~# docker search mysql -f stars=1000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13152 [OK]
mariadb MariaDB Server is a high performing open sou… 5024 [OK]
docker image pull
语义:
拉取镜像
Pull an image or a repository from a registry
语法:
docker image pull [OPTIONS] NAME[:TAG]
参数:
-q : 只显示镜像ID
别名:
docker pull
root@ubuntu01:~# docker image pull nginx
Using default tag: latest #
latest: Pulling from library/nginx
7a6db449b51b: Pull complete
ca1981974b58: Pull complete
d4019c921e20: Pull complete
7cb804d746d4: Pull complete
e7a561826262: Pull complete
7247f6e5c182: Pull complete
Digest: sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
docker image ls
语义:
列出本地镜像
List images
语法:
docker image ls [OPTIONS] [REPOSITORY[:TAG]]
参数:
-q : 只显示镜像ID
别名:
docker images
root@ubuntu01:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 2dc39ba059dc 6 days ago 77.8MB
mysql latest ff3b5098b416 8 days ago 447MB
hello-world latest feb5d9fea6a5 11 months ago 13.3kB
root@ubuntu01:~# docker image ls -q
2dc39ba059dc
ff3b5098b416
feb5d9fea6a5
docker image rm
语义:
删除一个或多个镜像
Remove one or more images
语法:
docker image rm [OPTIONS] IMAGE [IMAGE...]
参数:
IMAGE 可以是镜像的Name或者容器的ID
-f : 强制删除
别名:
docker rmi
root@ubuntu01:~# docker image rm nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f
Deleted: sha256:2b7d6430f78d432f89109b29d88d4c36c868cdbf15dc31d2132ceaa02b993763
Deleted: sha256:83cfe7b2ae78dff709b878ea6827c2b7447910f22f5c18d2155bc4ee678b6589
Deleted: sha256:8354a3628193232756e7e39423937a89bdea9738fcd033e3009c34847d44f1cc
Deleted: sha256:edd004a0175251a1fa027a4949079439eb989b9c9d0dc863c2c834e0da69ed2e
Deleted: sha256:e65b26350d575233ec4815a0ced147030bb9db419084e6a1561aab8c9f05f20e
Deleted: sha256:d75805fc21788b744c171b7ca303a153b34b61b61401c74348b0a16c9bfabbc8
Deleted: sha256:6485bed636274e42b47028c43ad5f9c036dd7cf2b40194bd556ddad2a98eea63
root@ubuntu01:~# docker image rm -f nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f
Deleted: sha256:2b7d6430f78d432f89109b29d88d4c36c868cdbf15dc31d2132ceaa02b993763
Deleted: sha256:83cfe7b2ae78dff709b878ea6827c2b7447910f22f5c18d2155bc4ee678b6589
Deleted: sha256:8354a3628193232756e7e39423937a89bdea9738fcd033e3009c34847d44f1cc
Deleted: sha256:edd004a0175251a1fa027a4949079439eb989b9c9d0dc863c2c834e0da69ed2e
Deleted: sha256:e65b26350d575233ec4815a0ced147030bb9db419084e6a1561aab8c9f05f20e
Deleted: sha256:d75805fc21788b744c171b7ca303a153b34b61b61401c74348b0a16c9bfabbc8
Deleted: sha256:6485bed636274e42b47028c43ad5f9c036dd7cf2b40194bd556ddad2a98eea63
批量删除镜像
# 方法 1
docker image rm -f $(docker image ls -aq)
# 方法 2
docker image ls -aq | xargs docker image rm -f
容器是由镜像创建的,所以,先有镜像,后有容器
可以将镜像理解为类,容器理解为实例,创建容器就相当于是类的实例化。
root@ubuntu01:~# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
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
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
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
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
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
docker container run
语义:
创建新容器,并在新容器中运行命令。
Run a command in a new container
语法:
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
参数:
CONTAINER可以是容器的Name或者容器的ID
--name=“Name” : 指定容器名字
-d : 后台运行。创建容器后,让容器在后台运行
-ti : 交互模式。交互模式下执行exit,容器会停止运行
-p : 主机端口:容器端口
别名:
docker run
# 创建容器,不运行命令
# 容器创建后,如果没有任务可执行或者任务执行完之后,容器会退出。因此,如果创建容器时不添加命令,容器创建后随即停止。
root@ubuntu01:~# docker run ubuntu
root@ubuntu01:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edcda8245844 ubuntu "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago pedantic_bose
# 交互模式-不添加命令
# 交互模式时,分配了一个伪终端(tty)。创建容器时虽然没有手动添加命令,但是容器分配了一个终端,终端等待用户输入,容器不会退出。可以理解为容器正在执行“终端”命令。执行exit后容器退出。
root@ubuntu01:~# docker run -ti ubuntu
[root@2a606ed2d4e3 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@2a606ed2d4e3 /]# exit
exit
root@ubuntu01:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a606ed2d4e3 ubuntu "/bin/bash" 33 seconds ago Exited (0) 5 seconds ago quizzical_shtern
# 交互模式-添加命令/bin/bash
root@ubuntu01:~# docker run -ti ubuntu /bin/bash
[root@984cdb2438cd /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@984cdb2438cd /]# exit
exit
root@ubuntu01:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
984cdb2438cd ubuntu "/bin/bash" 14 seconds ago Exited (0) 9 seconds ago pedantic_boyd
# 添加命令
root@ubuntu01:~# docker container run ubuntu /bin/bash # 执行bash,bash执行完之后容器退出
root@ubuntu01:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db15971b8e56 ubuntu "/bin/bash" 6 seconds ago Exited (0) 6 seconds ago agitated_lalande
root@ubuntu01:~# docker container run -ti ubuntu /bin/bash # 交互模式下执行bash,容器进入交互模式
[root@91a8f0804e2f /]# exit
exit
root@ubuntu01:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91a8f0804e2f ubuntu "/bin/bash" 31 seconds ago Exited (0) 24 seconds ago unruffled_mccarthy
db15971b8e56 ubuntu "/bin/bash" 56 seconds ago Exited (0) 56 seconds ago agitated_lalande
root@ubuntu01:~# docker run -tid ubuntu /bin/bash # 交互模式下后台执行bash,容器在后台运行
480bfe44ebcdd0d00df1129605df5c00bac29026b78bcb21b92da9bc84509043
root@ubuntu01:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
480bfe44ebcd ubuntu "/bin/bash" 4 seconds ago Up 3 seconds heuristic_gagarin
91a8f0804e2f ubuntu "/bin/bash" 46 seconds ago Exited (0) 39 seconds ago unruffled_mccarthy
db15971b8e56 ubuntu "/bin/bash" About a minute ago Exited (0) About a minute ago agitated_lalande
# 指定容器名称
root@ubuntu01:~# docker container run -tid --name ubuntu_1 ubuntu /bin/bash
c0b564effd23e77154e9a2384a5806ff52e264f37e5e1af9ac85cc7671e4ff5a
root@ubuntu01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0b564effd23 ubuntu "/bin/bash" 3 seconds ago Up 2 seconds ubuntu_1