基本上就是按照官方的教程一步步来的:https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
会出来一个warning,不用管:
gpg: WARNING: unsafe ownership on homedir '/home/lz/.gnupg'
echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo docker version
返回这样一段信息:
Client: Docker Engine - Community Version: 20.10.5 API version: 1.41 Go version: go1.13.15 Git commit: 55c4c88 Built: Tue Mar 2 20:18:05 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.5 API version: 1.41 (minimum version 1.12) Go version: go1.13.15 Git commit: 363e9a8 Built: Tue Mar 2 20:16:00 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.4 GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e runc: Version: 1.0.0-rc93 GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec docker-init: Version: 0.19.0 GitCommit: de40ad0
sudo groupadd docker
返回:groupadd: group 'docker' already exists
sudo usermod -aG docker $USER
newgrp docker
然后你运行docker version,发现现在不用加sudo也可以像上面那样返回版本信息了。也可以用下面的测试一下;
docker run hello-world
返回如下,就表示docker-ce安装成功了:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
更多后安装操作可见(如设置开机自启动,http代理等):https://docs.docker.com/engine/install/linux-postinstall/
首先可以用帮助查看一下命令:
docker --help
然后在这里放一下常用的命令:
docker pull ${CONTAINER NAME} #拉取镜像 docker images #查看本地所有镜像 docker ps #查看所有正在运行的容器,加-q返回id docker ps -a #查看所有容器,加-q返回id docker rmi ${IMAGE NAME/ID} #删除镜像 docker rm ${CONTAINER NAME/ID} #删除容器 docker save ${IMAGE NAME} > ${FILE NAME}.tar #将镜像保存成文件 docker load < ${FILE NAME}.tar #从文件加载镜像 docker start ${CONTAINER NAME/ID} #运行一个以前运行过的容器 docker stop ${CONTAINER NAME/ID} #停止一个正在运行的容器 docker logs ${CONTAINER NAME/ID} #显示运行容器的日志 docker run... #运行一个容器 --name ${container name} #设置容器名称 -p ${host port}:${container port} #映射主机和容器内的端口 -e ${env name}=${env value} #添加环境变量 -d #后台运行 -v ${host folder path}:${container folder path} #将主机目录挂在到容器内
docker ps -f "status=exited" #显示所有退出的容器 docker ps -a -q #显示所有容器id docker ps -f "status=exited" -q #显示所有退出容器的id docker restart $(docker ps -q) #重启所有正在运行的容器 docker stop $(docker ps -a -q) #停止所有容器 docker rm $(docker ps -a -q) #删除所有容器 docker rm $(docker ps -f "status=exited" -q) #删除所有退出的容器 docker rm $(docker stop $(docker ps -a -q)) #停止并删除所有容器 docker start $(docker ps -a -q) #启动所有容器 docker rmi $(docker images -a -q) #删除所有镜像 docker exec -it ${CONTAINER NAME/ID} /bin/bash #进入容器内 docker exec -it ${CONTAINER NAME/ID} ping ${CONTAINER NAME/ID} #一个容器ping另外一个容器 docker top ${CONTAINER NAME/ID} #显示一个容器的top信息 docker stats #显示容器统计信息(正在运行) docker stats -a #显示所有容器的统计信息(包括没有运行的) docker stats -a --no-stream #显示所有容器的统计信息(包括没有运行的) ,只显示一次 docker stats --no-stream | sort -k8 -h #统计容器信息并以使用流量作为倒序 docker system docker system df #显示硬盘占用 docker system events #显示容器的实时事件 docker system info #显示系统信息 docker system prune #清理文件