Docker基本命令
获取命令帮助
[root@localhost03 ~]# 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
常用命令
docker info
#查看Docker信息
[root@localhost03 ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
Server:
Containers: 4
Running: 4
Paused: 0
Stopped: 0
Images: 2
Server Version: 20.10.2
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
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: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-862.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 31.25GiB
Name: localhost03.abc.com
ID: 3RAL:VLTG:CYYB:6QMI:LDVR:5IQV:ESZY:HFPB:2VLX:MSU4:A7JQ:3ASF
Docker Root Dir: /var/lib/docker
Debug Mode: false
HTTP Proxy: http://rcd.rtwork.win:7282/
HTTPS Proxy: http://rcd.rtwork.win:7282/
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
docker —version
# 查看版本
docker search nginx
# 联网查询镜像
docker image ls
# 查看当前系统中有哪些镜像
docker image pull nginx:latest
# 下载镜像
--no-trunc
#不要截断输出.
[root@localhost03 ~]# docker image ls --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee 9 days ago 133MB
ericwang2006/ttnode latest sha256:3ff94a452e165978fadbae30d3f445112d0abd829bad4369bb6ec835df43182c 12 days ago 108MB
portainer/agent latest sha256:c81206f03f4ab591bc8db5782d2a45d0546d5df9ba0ebb55e55fd33bf7f7aed7 4 months ago 94.8MB
容器的操作
docker command
start #启动一个停止的容器
stop #关闭容器 -15
kill #强制终止容器 -9
run #基于镜像启动容器
pause #暂停容器
unpause #让一个暂停的容器继续运行
docker ps -a
查看当前存在的容器,包括运行,停止,暂停的容器.
docker run command
-t, --tty #打开容器的时候分配一个终端
-i, --interactive #使用交互模式登陆容器
--name #指定容器的名称
-d, --detach #让容器工作在后台
--network #让容器使用哪个网络 docker network ls 查看网络
容器的用户空间是靠一个进程生成的,这个进程就是运行在用户空间的哪个唯一进程PID为1如果这个进程被杀容器就停止了.
而且这个进程必须工作在前台.
查看容器的详细信息
docker container inspect box1
Usage: docker container inspect [OPTIONS] CONTAINER [CONTAINER…]
进入容器
docker exec -it ttnode1 /bin/bash
有些命令执行不了
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
docker attach ttnode1
退出后会导致容器停止
Usage: docker attach [OPTIONS] CONTAINER
docker container inspect -f {{.State.Pid}} ttnode1
获取容器的名称空间
nsenter -t 6767 -u -i -n -p
直接进入容器的名称空间
[root@localhost03 ~]# nsenter --help
用法:
nsenter [options] <program> [<argument>...]
Run a program with namespaces of other processes.
选项:
-t, --target <pid> 要获取名字空间的目标进程
-m, --mount[=<file>] enter mount namespace
-u, --uts[=<file>] enter UTS namespace (hostname etc)
-i, --ipc[=<file>] enter System V IPC namespace
-n, --net[=<file>] enter network namespace
-p, --pid[=<file>] enter pid namespace
-U, --user[=<file>] enter user namespace
-S, --setuid <uid> set uid in entered namespace
-G, --setgid <gid> set gid in entered namespace
--preserve-credentials do not touch uids or gids
-r, --root[=<dir>] set the root directory
-w, --wd[=<dir>] set the working directory
-F, --no-fork 执行 <程序> 前不 fork
-Z, --follow-context set SELinux context according to --target PID
-h, --help 显示此帮助并退出
-V, --version 输出版本信息并退出
更多信息请参阅 nsenter(1)。
# 过滤获取PID(json数据可是用.来分割"{{.State.Pid}}")
[root@localhost03 ~]# docker container inspect -f "{{.State.Pid}}" ttnode2
4176
# 获取完整信息
[root@localhost03 ~]# docker container inspect ttnode2
[
{
"Id": "cfb8509cc36b3f094186e741b77751630e2bf705195fde0270b2958b690b6451",
"Created": "2021-06-02T09:19:26.857895468Z",
"Path": "/usr/node/start.sh",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4176,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-06-02T09:19:27.31586878Z",
"FinishedAt": "0001-01-01T00:00:00Z"
直接在容器中执行命令
docker run --name test2 mycentos:v1.8
ls /
# docker run --name test2 mycentos:v1.8 ls /
bin
data
dev
download
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
查看日志
docker logs -f ttnode1
Usage: docker logs [OPTIONS] CONTAINER
导出镜像
docker save nginx:1.14-alpine > /home/nginx.14.tar.gz
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
导入镜像
docker load < /home/nginx.14.tar.gz
Usage: docker load [OPTIONS]
容器删除
docker rm ttnode1
删除容器
docker rmi nginx
删除镜像
批量删除多个容器
docker rm $( docker ps -a -q)
评论抢沙发