yum 安装docker
#地址
https://yq.aliyun.com/articles/110806
#:安装
root@ubuntu:~# sudo apt-get update
root@ubuntu:~# sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
root@ubuntu:~# curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
root@ubuntu:~# sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
root@ubuntu:~# sudo apt-get -y update
#:查看docker有哪些版本
root@ubuntu:~# apt-cache madsion docker-ce
#:安装指定版本docker和客户端
root@ubuntu:~# apt install docker-ce=5:18.09.9~3-0~ubuntu-bionic docker-ce-cli=5:18.09.9~3-0~ubuntu-bionic
#: 启动docker
root@ubuntu:~# systemctl start docker
root@ubuntu:~# systemctl enable docker
配置加速器
#:登录阿里云,找到镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9916w1ow.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
docker常用命令
#:下载镜像
root@ubuntu:~# docker pull nginx
#:列出镜像
root@ubuntu:~# docker images
#:运行容器
root@ubuntu:~# docker run --rm -it -p 81:80 nginx
--rm 只运行一次,退出后容器消失
-d 后台运行
-p 端口映射
#:显示进程
root@ubuntu:~# docker ps -a
-a 显示全部(包括运行的,退出的,等等)
#:进入到运行中的容器
root@ubuntu:~# docker exec -it d7025ac92727
#:删除容器
root@ubuntu:~# docker rm -f 5d45f5accbb2
-f 删除停止的镜像
-fv 删除镜像,类似rm -rf
#:查看日志
root@ubuntu:~# docker logs -f f3beeaba3dc0
-f 持续查看
#:容器的启动和关闭
root@ubuntu:~# docker stop cf0b2ed6953c
root@ubuntu:~# docker start cf0b2ed6953c
#;批量删除已退出的容器
root@ubuntu:~# docker rm -fv `docker ps -a | grep "Exited" |awk '{print $1}'`
nsenter命令使用
#:先安装nsenter命令
root@ubuntu:~# apt install util-linux
#:查看容器信息
root@ubuntu:~# docker inspect cf0b2ed6953c
#:查看容器PID
root@ubuntu:~# docker inspect -f {{.State.Pid}} cf0b2ed6953c
#:查看容器IP地址
root@ubuntu:~# docker inspect -f {{.NetworkSettings.Networks.bridge.IPAddress}} cf0b2ed6953c
指定容器的dns
root@ubuntu:~# docker run -d -it --dns 192.168.7.101 -p 85:80 nginx