Docker安装以及修改Docker的默认存储路径
1. Docker安装
1.1 使用官方安装脚本自动安装
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun或者curl -sSL https://get.daocloud.io/docker | sh
测试:
sudo docker run hello-world
出现问题:
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.See 'docker run --help'.
问题分析: 默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
1、创建docker组(安装docker时会自动创建,一般无需重新创建)
$ sudo groupadd docker
2、将当前用户加入到docker用户组
$ sudo gpasswd -a ${USER} docker
3 、重新启动docker
sudo service docker restart #或者执行以下命令,无须重新登录 newgrp docker
4、测试docker命令是否可以使用sudo正常使用
docker ps
测试:
$ docker run --rm hello-world
结果:
Unable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldb8dfde127a29: Pull completeDigest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24Status: Downloaded newer image for hello-world:latestHello 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 theexecutable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bashShare 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/
1.2 建立 docker 用户组
默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
1、创建docker组(安装docker时会自动创建,一般无需重新创建)
$ sudo groupadd docker
2、将当前用户加入到docker用户组
$ sudo gpasswd -a ${USER} docker
3 、重新启动docker
sudo service docker restart #或者执行以下命令,无须重新登录 newgrp docker
4、测试docker命令是否可以使用sudo正常使用
docker ps
测试:
$ docker run --rm hello-world
结果:
Unable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldb8dfde127a29: Pull completeDigest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24Status: Downloaded newer image for hello-world:latestHello 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 theexecutable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bashShare 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/
1.3 卸载 docker
删除安装包:
sudo apt-get purge docker-ce
删除镜像、容器、配置文件等内容:
sudo rm -rf /var/lib/docker
2. 修改Docker的默认存储路径
-
1. 停止docker服务:
sudo service docker stop
-
2. 修改docker.service配置文件,使用–graph 参数指定新的目录
$ sudo vi /lib/systemd/system/docker.service
在打开的文本里,找到ExecStart进行相应的修改:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph="/home/xiaoyuanzi/python/Docker" --storage-driver=overlayExecReload=/bin/kill -s HUP $MAINPID
-
3. 重新加载配置文件
$ sudo systemctl daemon-reload
-
4. 启动docker,查看docker状态
$ sudo systemctl start docker$ sudo systemctl enable docker$ sudo systemctl status docker
-
5. 查看默认路径是否修改成功
$ sudo docker info或者直接查看docker路径sudo docker info | grep "Docker Root Dir"
结果:
-
其他方法: 未验证是否成功
https://blog.51cto.com/forangela/1949947
https://zhuanlan.zhihu.com/p/95533274
https://blog.csdn.net/BigData_Mining/article/details/104921479
作者:努力的袁
来源链接:https://blog.csdn.net/qq_44747572/article/details/120923891
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。