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' 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/

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' 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/

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=overlay
    ExecReload=/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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只搬烫手的砖

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值