在这之前呢,推送过民工哥2篇文章
Docker容器技术入门(一)
Docker容器技术入门(二)
很多人都感觉这文章这么简单、这么基础,可是别忘记了“万丈高楼平地起”,如果少了这个平地址,你这高楼估计也起不了。所以,基础是学习任何一门技术或者一个技术点的重中之重,也可以说是很关键的决胜点。
所以呢,今天,民工哥给大家总结了这20个Docker Command,愿各位小伙伴在通往“玩转Docker”路上不再无助!!!!
安装完成docker容器服务之后,需要了解如何操作它?在shell命令行下直接输入docker就可以查看帮助信息,如下。
[root@master ~]# docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-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:
container Manage containers
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
volume Manage volumes
Commands:
attach Attach 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 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
1、docker start/stop/restart/kill
启动/停止/重启/杀掉容器
实例操作如下:
[root@docker ~]# docker start myweb
[root@docker ~]# docker stop myweb
[root@docker ~]# docker restart myweb
[root@docker ~]# docker kill -s kill myweb
参数 -s #向容器发送信号
这些命令不做过多解释
2、docker run
创建并启动一个新的容器
常用参数如下:
-d #后台运行容器,并返回容器ID
-i #以交互式模式运行容器,常与-t参数同时使用
-t #给容器重新分配一个伪终端,常与-i参数同时使用
--name #给容器指定一个名称
-m #指定容器使用内存的最大值
--net #指定容器使用的网络类型
--link #链接到另一个容器
实例操作如下:
[root@docker ~]# docker run -d --name nginx nginx:latest
#后台启动并运行一个名为nginx的容器,运行前它会自动去docker镜像站点下载最新的镜像文件
[root@docker ~]# docker run -d -P 80:80 nginx:latest
#后台启动并运名为nginx的容器,然后将容器的80端口映射到物理机的80端口
[root@docker ~]# docker run -d -v /docker/data:/docker/data -P 80:80 nginx:latest
#后台启动并运名为nginx的容器,然后将容器的80端口映射到物理机的80端口,并且将物理机的/docker/data目录映射到容器的/docker/data
[root@docker ~]# docker run -it nginx:latest /bin/bash
#以交互式模式运行容器,然后在容器内执行/bin/bash命令
3、docker rm
删除容器
常用参数如下:
-f #强制删除一个运行中的容器
-l #删除指定的链接
-v #删除与容器关联和卷
实例操作如下:
[root@docker ~]# docker rm -f mydocker
#强制删除容器mydocker
[root@docker ~]# docker rm -f dockerA dockerB
#强制删除容器dockerA,dockerB
[root@docker ~]# docker rm -v mydocker
#删除容器,并删除容器挂载的数据卷
4、docker create
创建一个新的容器但不启动它
[root@docker ~]# docker create --name myserver nginx:latest
09b93464c2f75b7b69f83d56a9cfc23ceb50a48a9db7652ee4c27e3e2cb1961f
#创建一个名为myserver的容器
5、docker exec
在运行的容器中执行命令
常用参数如下:
-d #在后台运行
-i #保持STDIN打开
-t #分配一个伪终端
实例操作如下:
[root@docker ~]# docker exec -it mydocker /bin/sh /server/scripts/docker.sh
hello world!!!!!!!!!!
#以交互模式执行容器中的/server/scripts/docker.sh脚本
[root@docker ~]# docker exec -it mydocker /bin/sh
root@b1a0703e41e7:/#
#以交互模式给容器分配一个伪终端连接
6、docker ps
列出容器(正在运行)
常用参数如下:
-a #列出所有容器包括停止的
-f #根据条件过滤显示内容
-l #列出最近创建的容器
-n #列出最近创建的N个容器,N为数字
-q #只显示容器ID
-s #显示总文件大小
实例操作如下:
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd96d72ed9c7 google/cadvisor "/usr/bin/cadvisor..." 47 hours ago Up 47 hours 0.0.0.0:8082->8080/tcp cadvisor
665563143eb7 grafana/grafana "/run.sh" 2 days ago Up 2 days 0.0.0.0:3000->3000/tcp grafana
f2304dad5855 tutum/influxdb "/run.sh" 2 days ago Up 2 days 0.0.0.0:8083->8083/tcp, 0.0.0.0:8086->8086/tcp influxdb
#列出正在运行的容器
[root@docker ~]# docker ps -n 2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd96d72ed9c7 google/cadvisor "/usr/bin/cadvisor..." 47 hours ago Up 47 hours 0.0.0.0:8082->8080/tcp cadvisor
665563143eb7 grafana/grafana "/run.sh" 2 days ago Up 2 days 0.0.0.0:3000->3000/tcp grafana
#列出最近创建的2个容器
[root@docker ~]# docker ps -a -q
bd96d72ed9c7
665563143eb7
f2304dad5855
9921d2660307
#显示所有容器的ID
7、docker inspect
获取容器的元数据
常用参数如下:
-f #指定返回值格式或模板文件
-s #显示总文件大小
--type #为指定类型返回JSON
实例操作如下:
[root@docker ~]# docker inspect bd96d72ed9c7
[
{
"Id": "bd96d72ed9c713591ba8db0ed4c0ae2689188255da71033c7bced6bb34aa8542",
"Created": "2018-05-23T09:22:10.633809699Z",
"Path": "/usr/bin/cadvisor",
"Args": [
"-logtostderr",
"-storage_driver=influxdb",
"-storage_driver_db=cadvisor",
"-storage_driver_host=192.168.3.82:8086"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 17589,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-05-23T09:22:10.769771142Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:75f88e3ec333cbb410297e4f40297ac615e076b4a50aeeae49f287093ff01ab1",
"ResolvConfPath": "/var/lib/docker/containers/bd96d72ed9c713591ba8db0ed4c0ae2689188255da71033c7bced6bb34aa8542/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/bd96d72ed9c713591ba8db0ed4c0ae2689188255da71033c7bced6bb34aa8542/hostname",
"HostsPath": "/var/lib/docker/containers/bd96d72ed9c713591ba8db0ed4c0ae2689188255da71033c7bced6bb34aa8542/hosts",
"LogPath": "",
"Name": "/cadvisor",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",