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

### 修改Docker Windows版本中的默认存储位置 对于Windows系统的Docker默认情况下,镜像和其他数据会保存在`C:\Program Files\Docker`目录下[^1]。然而,如果希望调整这一设定,使这些资源能够被放置于其他磁盘分区或是特定文件夹内,则需按照如下方法执行: #### 配置新存储路径 为了实现上述目标,在较新版的Docker Desktop中(适用于Windows),推荐的方式是编辑或创建位于用户主目录下的`.docker\daemon.json`文件,并在此处定义一个新的键值对来指明期望的数据根目录。 具体来说,可以将该JSON对象设置成类似于这样: ```json { "data-root": "E:\\DockerData" } ``` 这里的`"E:\\DockerData"`应当替换为你想要使用的实际路径。需要注意的是,当涉及到网络共享或其他特殊类型的卷时,可能需要额外考虑权限等问题[^2]。 完成此步之后,还需要确保关闭并重新启动Docker Desktop应用程序以便让改动生效。这一步骤非常重要,因为只有这样做才能使得新的配置参数得到应用[^3]。 另外值得注意的一点是在某些场景下,比如企业环境中或者安全策略较为严格的场合,可能会遇到由于权限不足而导致无法成功变更的情况。此时建议联系管理员获取帮助或适当调整本地计算机的安全设置以满足需求[^4]。 #### 示例命令片段用于验证更改是否有效 一旦完成了以上所有步骤,可以通过运行简单的测试指令来检验配置是否正确无误地进行了更新。例如,查看当前正在使用的存储位置可通过以下PowerShell命令实现: ```powershell Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options' ``` 不过更直接的方法可能是利用官方提供的CLI工具查询相关信息: ```bash docker info --format '{{ .DockerRootDir }}' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值