docker常用命令总结.

DOCKER

查看指定服务名的日志(只有一个服务实列)
docker service ps t_mserv_espare|grep Running|awk '{print $1}'|xargs docker service logs -f

构建镜像

docker build -t <TAG_NAME> <Dockerfile_PATH>

运行镜像

docker run -d -p 3000:3000/tcp -v /d:\\test:/home/test gettingg-started

-d(etach) 后台模式运行 -p(ublish) <hostPort>:<containerPort>/<protocol[tpc,udp...]> 端口映射

-v(olume) 磁盘映射 hostPath:containerPath

查看镜像列表

docker images

查看容器实例列表

docker ps 或者 docker container ls

查看实例中运行的程序的STD_OUT(标准输出)(eg:c语言中的print/printf,node:console.log,java:System.out…)

docker logs <containerId>

运行当前运行中容器中的命令

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a
container
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container

eg: docker exec -it 12e /bin/sh

执行上述命令后会会自动连接到目标容易的终端

删除容器

`docker rm -f

创建服务

docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]

强制重启服务

docker service update your_service_name --force

查看service的containerId

for f in $(docker service ps -q t_mserv_base_mq);do docker inspect --format '{{.ServiceID}} {{.NodeID}} {{.Status.ContainerStatus.ContainerID}} {{.DesiredState}}' $f; done

docker 容器可视化

docker pull dockersamples/visualizer:latest
docker service create \
--name=viz \
--publish=12306:8080/tcp \
--constraint=node.role==manager \
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
dockersamples/visualizer:latest

DockerFile

COPY SRC DEST

复制src文件到dest

ADD SRC DEST

复制src文件到dest,如果src为压缩文件,怎复制和自动解压

CMD cmd args…

docker run 时运行

RUN cmd args…

docker build时运行,可能

HEX(NUM) -> DECIMAL
int r= (int)(NUM / 10)*16 + NUM%10

日志

docker service logs --help

Usage:  docker service logs [OPTIONS] SERVICE|TASK

Fetch the logs of a service or task

Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--no-resolve Do not map IDs to Names in output
--no-task-ids Do not include task IDs in output
--no-trunc Do not truncate output
--raw Do not neatly format logs
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps

eg:

查询日志通过服务名

docker service logs t_mserv_xgb_project --since 2022-04-22

查询日志通过taskId

获取服务对应的taskId

docker service ps t_mserv_xgb_project

通过taskId获取日志输出

docker service logs taskId --since 2022-04-22