Docker 小技巧

yexiaobai

以下都会使用到容器 id,所以执行以下指令查看容器 id 或者容器名称

[yexiaobaideMacBook-Pro: ~]$ sudo docker ps 
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                              NAMES
20ec96201fa3        swarm               "/swarm manage token   26 hours ago        Up 26 hours         0.0.0.0:2222->2375/tcp             suspicious_sinoussi   
7e90d4598a74        swarm               "/swarm join -addr=1   26 hours ago        Up 26 hours         2375/tcp                           sad_cray              
abf7cdbe5d1c        rancher/server      "/usr/bin/s6-svscan    26 hours ago        Up 26 hours         3306/tcp, 0.0.0.0:8080->8080/tcp   ecstatic_morse     

检查环节变量

sudo docker inspect --format '{{.Config.Env}}' abf7cdbe5d1c

查看日志

sudo docker logs -f abf7cdbe5d1c

备份

sudo docker run --rm -v /tmp:/backup --volumes-from <container-name> busybox tar -cvf /backup/backup.tar <path-to-data>

恢复备份

sudo docker run --rm -v /tmp:/backup --volumes-from <container-name> busybox tar -xvf /backup/backup.tar <path-to-data>

使用 docker exec 进入容器

sudo docker exec -it ecstatic_morse  bash

没有剩余磁盘空间

使用以下指令删除容器和镜像

sudo docker ps -aq | xargs sudo docker rm # 删除所有容器
sudo docker images -aq -f dangling=true | xargs sudo docker rmi # 删除未使用的镜像

清理日志 - 非 root 权限

 sudo su -c "echo "" > $(sudo docker inspect --format='{{.LogPath}}' ecstatic_morse)"      

使用别名

.zshrc 或者 .bashrc 中写入以下脚本:

alias dockercleancontainers="docker ps -aq | xargs docker rm"
alias dockercleanimages="docker images -aq -f dangling=true | xargs docker rmi"
alias dockerclean="dockercleancontainers && dockercleanimages"
alias docker-killall="docker ps -q | xargs docker kill"

# runs docker exec in the latest container
function docker-exec-last {
  docker exec -ti $( docker ps -a -q -l) /bin/bash
}

function docker-get-ip {
  # Usage: docker-get-ip (name or sha)
  [ -n "$1" ] && docker inspect --format "{{ .NetworkSettings.IPAddress }}" $1
}

function docker-get-id {
  # Usage: docker-get-id (friendly-name)
  [ -n "$1" ] && docker inspect --format "{{ .ID }}" "$1"
}

function docker-get-image {
  # Usage: docker-get-image (friendly-name)
  [ -n "$1" ] && docker inspect --format "{{ .Image }}" "$1"
}

function docker-get-state {
  # Usage: docker-get-state (friendly-name)
  [ -n "$1" ] && docker inspect --format "{{ .State.Running }}" "$1"
}

function docker-memory {
  for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do docker ps | grep $line | awk '{printf $NF" "}' && echo $(( `cat /sys/fs/cgroup/memory/docker/$line*/memory.usage_in_bytes` / 1024 / 1024 ))MB ; done
}
# keeps the commmand history when running a container
function basher() {
    if [[ $1 = 'run' ]]
    then
        shift
        docker run -e HIST_FILE=/root/.bash_history -v $HOME/.bash_history:/root/.bash_history "$@"
    else
        docker "$@"
    fi
}
# backup files from a docker volume into /tmp/backup.tar.gz
function docker-volume-backup-compressed() {
  docker run --rm -v /tmp:/backup --volumes-from "$1" debian:jessie tar -czvf /backup/backup.tar.gz "${@:2}"
}
# restore files from /tmp/backup.tar.gz into a docker volume
function docker-volume-restore-compressed() {
  docker run --rm -v /tmp:/backup --volumes-from "$1" debian:jessie tar -xzvf /backup/backup.tar.gz "${@:2}"
  echo "Double checking files..."
  docker run --rm -v /tmp:/backup --volumes-from "$1" debian:jessie ls -lh "${@:2}"
}
# backup files from a docker volume into /tmp/backup.tar
function docker-volume-backup() {
  docker run --rm -v /tmp:/backup --volumes-from "$1" busybox tar -cvf /backup/backup.tar "${@:2}"
}
# restore files from /tmp/backup.tar into a docker volume
function docker-volume-restore() {
  docker run --rm -v /tmp:/backup --volumes-from "$1" busybox tar -xvf /backup/backup.tar "${@:2}"
  echo "Double checking files..."
  docker run --rm -v /tmp:/backup --volumes-from "$1" busybox ls -lh "${@:2}"
}

使用 COPY 而不是 ADD

在下载之后和开始使用之前使用 checksum 检查

ENV JENKINS_VERSION 1.625.3
ENV JENKINS_SHA 537d910f541c25a23499b222ccd37ca25e074a0c

RUN curl -fL http://mirrors.jenkins-ci.org/war-stable/$JENKINS_VERSION/jenkins.war -o /usr/share/jenkins/jenkins.war \
  && echo "$JENKINS_SHA /usr/share/jenkins/jenkins.war" | sha1sum -c -

在相同 layer 清理

RUN apt-get update && \
    apt-get install -y curl python-pip && \
    pip install requests && \
    apt-get remove -y python-pip curl && \
    rm -rf /var/lib/apt/lists/*

记录日志到stdout

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log

参考

阅读 3.4k

yexiaoxiaobai
刀锋上的跳舞的 SRE,喜爱 Perl, Python, Go,常被正则婊戏耍,热衷于鼓捣一些小玩具。

就是不告诉你 O(∩_∩)O哈哈~。

4.8k 声望
867 粉丝
0 条评论

就是不告诉你 O(∩_∩)O哈哈~。

4.8k 声望
867 粉丝
文章目录
宣传栏