docker 常用

基本设置

1
2
3
4
5
6
7
8
# 启动 Docker
sudo systemctl start docker
# 开机启动 Docker
systemctl enable docker
# 立即重启
shutdown -r now
# 查看 Docker 状态
systemctl status docker

操作

  • Docker 删除所有无名称的镜像(悬空镜像)
1
2
3
docker rmi $(docker images -f "dangling=true" -q)
# or
docker image prune -a -f

进入容器

1
docker exec -it CONTAINER_ID /bin/bash

nsenter

1
2
3
4
5
6
wget https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.33/util-linux-2.33-rc1.tar.gz
tar -xzvf util-linux-2.33-rc1.tar.gz
cd util-linux-2.33-rc1/
./configure --without-ncurses
make nsenter
cp nsenter /usr/local/bin
1
2
3
4
# 查看 CONTAINER_ID
docker ps -a
docker inspect -f {{.State.Pid}} CONTAINER_ID
nsenter --target 31611 --mount --uts --ipc --net --pid

查看日志

1
2
docker logs -f -t --tail 行数 容器名
docker logs -f -t --tail 10 3da

免 sudo

1
2
3
sudo gpasswd -a ${USER} docker
cat /etc/group | grep ^docker
sudo serivce docker restart

问题

IPv4 forwarding is disabled. Networking will not work.

1
2
echo 'net.ipv4.ip_forward=1' >> /usr/lib/sysctl.d/00-system.conf
systemctl restart network && systemctl restart docker