docker常用操作

Dockerfile编写

编写一般不会一次就完成,常常会出错

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Sending build context to Docker daemon  5.902MB
Step 1/10 : FROM docker:git as builder
 ---> 9be5c3cd302c
Step 2/10 : RUN git clone https://github.com/nkrode/RedisLive.git /redislive
 ---> Using cache
 ---> a0884ca42295
Step 3/10 : COPY ./redis-live.conf /redislive/src
 ---> Using cache
 ---> 6aef0cf6bec6
Step 4/10 : FROM python:2.7.16-slim-stretch
 ---> 48e3247f2a19
Step 5/10 : COPY --from=builder /redislive /usr/src/redislive
 ---> ab68d08c25ab
Removing intermediate container af6af2719327
Step 6/10 : WORKDIR /usr/src/redislive/src
 ---> 1c5459430d7c
Removing intermediate container b84bcb62e9a4

比当Step 6报错时,
可以进入第5步产生的镜像ab68d08c25ab查看详情或调试
docker run –rm -it ab68d08c25ab bash

  • bash 与 ash
    进入容器通常是 docker exec -it redis bash
    但有的容器里面不是bash 是ash 或其他shell
    可先看看其内部是用的什么shell
    docker exec redis ls /bin | grep sh

  • 遇到 entrypoint 的镜像
    docker run -it –name jstorm-zookeeper –entrypoint /bin/bash zookeeper:3.4

  • docker pause 或 unpause
    docker run -d –name rds redis &&
    sleep 0.1 && docker pause rds &&
    docker logs rds

  • 查看内存使用情况 与进程 docker top redis
    docker stats redis
    docker stats –no-stream redis

  • history 查看镜像层大小
    docker history 镜像id

  • 查看容器的ip
    docker inspect -f {{.NetworkSettings.IPAddress}} redis

  • 批量删除停止的容器
    docker ps -a | grep “Exited” | awk ‘{print $1}’ | xargs docker rm

  • 批量删除无tag的镜像
    docker rmi `docker images | grep none | awk ‘{print $3}‘`