①卸载可能的操作系统预安装旧版本Docker
我的Ubuntu 18.04(LTS)环境是没有预装的,但还是按照官方手册执行一下吧。
apt-get remove docker docker-engine docker.io containerd runc
②添加Docker的apt源
apt-get update
apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
③安装
apt-get install docker-ce docker-ce-cli containerd.io
④测试安装
root@ubuntu:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
运行一个官方Repository提供的“ hello-world ”示例。
⑤配置开机启动
systemctl enable docker
Docker默认是在root用户组下的,可以选择添加一个普通账户到安装时创建的“ docker ”组下。
usermod -aG docker $USER
⑥安装后的一些简单试操作
启动一个“ ubuntu ”环境的container。
root@ubuntu:~# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6cf436f81810: Pull complete
987088a85b96: Pull complete
b4624b3efe06: Pull complete
d42beb8ded59: Pull complete
Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Status: Downloaded newer image for ubuntu:latest
root@fb6c90c21a52:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@fb6c90c21a52:~# exit
exit
列出本地全部image。
root@ubuntu:~# docker image ls -a --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
ubuntu latest sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210 47b19964fb50 8 days ago 88.1MB
hello-world latest sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 fce289e99eb9 6 weeks ago 1.84kB
删除所有image。
root@ubuntu:~# docker image ls -q
47b19964fb50
fce289e99eb9
root@ubuntu:~# docker image rm $(docker image ls -q)
Error response from daemon: conflict: unable to delete 47b19964fb50 (must be forced) - image is being used by stopped container fb6c90c21a52
Error response from daemon: conflict: unable to delete fce289e99eb9 (must be forced) - image is being used by stopped container e030b394f528
可以看到删除image的时候报错了,因为image被container占用中,所以暂时无法直接删除。这时候有两种办法:一种是先干掉占用image的container然后再删除image;另一种是根据报错信息的提示,force删除。
曲线删除过程。也可以有两种方式:第一种同删除image,先输出container id,然后命令行管道删除;第二种通过“ prune ”选项删除。
root@ubuntu:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f29708ed7117 ubuntu "bash" 2 hours ago Exited (0) 2 hours ago amazing_shtern
702a2346f7cc ubuntu "bash" 18 hours ago Exited (0) 18 hours ago fervent_shtern
fb6c90c21a52 ubuntu "bash" 18 hours ago Exited (0) 18 hours ago clever_nightingale
e030b394f528 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago goofy_swartz
root@ubuntu:~# docker container ls -a -q
f29708ed7117
702a2346f7cc
fb6c90c21a52
e030b394f528
root@ubuntu:~# docker container rm $(docker container ls -a -q)
f29708ed7117
702a2346f7cc
fb6c90c21a52
e030b394f528
root@ubuntu:~# docker container ls -a -q
root@ubuntu:~# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
删除相关container以后再执行image删除操作就OK了。
root@ubuntu:~# docker image rm $(docker image ls -q)
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Deleted: sha256:47b19964fb500f3158ae57f20d16d8784cc4af37c52c49d3b4f5bc5eede49541
Deleted: sha256:d4c69838355b876cd3eb0d92b4ef27b1839f5b094a4eb1ad2a1d747dd5d6088f
Deleted: sha256:1c29a32189d8f2738d0d99378dc0912c9f9d289b52fb698bdd6c1c8cd7a33727
Deleted: sha256:d801a12f6af7beff367268f99607376584d8b2da656dcd8656973b7ad9779ab4
Deleted: sha256:bebe7ce6215aee349bee5d67222abeb5c5a834bbeaa2f2f5d05363d9fd68db41
Untagged: hello-world:latest
Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
暴力删除法,通过“ -f ”参数。
docker image rm -f $(docker image ls -q)
参考:
https://docs.docker.com/install/linux/docker-ce/ubuntu/#prerequisites
https://docs.docker.com/install/linux/linux-postinstall/