背景说明
使用Docker过程中需要记住一些辅助命令,此时相对比较费脑子,这里整理了快捷命令。
解决方案
创建脚本文件idocker
并放置至目录/usr/local/bin/下
#!/bin/bash
#example:
#params $2 :第二个参数表示
#idocker in mysql
if [ "$1" == "reset" ];then
docker stop $(docker ps -q )
docker rm $(docker ps -a -q )
fi
if [ "$1" == "images" ];then
docker images --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}"
fi
if [ "$1" == "ips" ];then
docker inspect --format "{{.Name}} {{.Config.Image}} {{.Config.Hostname}} {{.NetworkSettings.IPAddress}}" $(docker ps -q)
fi
if [ "$1" == "ps" ];then
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Networks}}\t{{.Status}}\t{{.Ports}}"
fi
if [ "$1" == "search" ];then
for Repo in $* ; do
curl -s -S "https://registry.hub.docker.com/v2/repositories/library/$Repo/tags/" | \
sed -e 's/,/,\n/g' -e 's/\[/\[\n/g' | \
grep "name" | awk -F\" '{print $4;}' | \
sort -fu | \
sed -e "s/^/${Repo}:/"
done
fi
if [ "$1" == "in" ];then
docker exec -it $2 /bin/bash
fi
if [ "$1" == "ip" ];then
docker inspect --format "{{.NetworkSettings.IPAddress}}" $2
fi
if [ "$1" == "volumes" ];then
docker inspect --format "{{.Config.Volumes}}" $2
fi
if [ "$1" == "start" ];then
if [ "$2" == "yii" ];then
sudo /usr/local/bin/docker-compose -f /tmp/advanced/docker-compose.yml start
fi
fi
if [ "$1" == "stop" ];then
if [ "$2" == "yii" ];then
sudo /usr/local/bin/docker-compose -f /tmp/advanced/docker-compose.yml stop
fi
fi
!/bin/bash指定shell解释器
赋予可执行权限
[root@template ~]# chmod +x /usr/local/bin/idocker
[root@template ~]# idocker ps
CONTAINER ID NAMES IMAGE NETWORKS STATUS PORTS
e47c82f7326e haproxy haproxy playwithdocker_default Up 11 hours 0.0.0.0:80->8080/tcp, :::80->8080/tcp
f4f4a00cc786 pwd golang playwithdocker_default Up 11 hours
1c271864f6a2 l2 golang playwithdocker_default Up 11 hours 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:8022->22/tcp, :::8022->22/tcp, 0.0.0.0:8053->53/tcp, :::8053->53/tcp
进入容器
[root@template ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d91aa5eac508 nginx "/docker-entrypoint.…" 49 minutes ago Up 49 minutes 80/tcp blissful_darwin
[root@template ~]# idocker in d91aa5eac508
root@d91aa5eac508:/#
root@d91aa5eac508:/# exit
exit
[root@template ~]#
容器主机
[root@template ~]# idocker ip d91aa5eac508
172.17.0.2
[root@template ~]#