0%

我们在执行docker命令时,比如docker ps时,可能会遇到如下command not found的错误。

1
2
3
sudo docker ps
[sudo] password for simon:
sudo: docker: command not found

这是什么原因呢?我们看看如下两条命令。

1
2
3
4
5
env | grep PATH
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/simon/.local/bin:/home/simon/bin

sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin

发现环境变量PATH是不相同的。之所以出现sudo: docker: commmand not found的问题,是因为在PATH=/sbin:/bin:/usr/sbin:/usr/bin下面找不到docker程序。

可以添加一个docker组来解决。

如果是通过二进制安装的话,就不会自动生成docker组,所以需要我们自己生成。

1
sudo groupadd docker

同时把用户加入到这个组中,并重启docker。

1
2
3
sudo gpasswd -a simon docker
Adding user simon to group docker
sudo systemctl restart docker

用户以docker组重新登录一下

1
newgrp docker

这时,就可以正常使用docker命令了。

那我们会问,如果不把用户加入docker组,不加sudo执行docker命令又会怎样呢?

1
2
docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/json: dial unix /var/run/docker.sock: connect: permission denied

会发现当我们连接socket的时候,没法访问/var/run/docker.sock这个文件。

原因是,这个文件所属用户和用户组都是root的。我们用普通用户是没法访问的。

1
2
ls -l /var/run/docker.sock
srw-rw----. 1 root root 0 Nov 21 21:27 /var/run/docker.sock

但是,当我们把普通用户加入到docker这个用户组以后,那生成的/var/run/docker.sock的用户组已经变成docker了,所以就可以连接了。

1
2
ls -l /var/run/docker.sock
srw-rw----. 1 root docker 0 Nov 22 09:48 docker.sock

当然,我们没有直接解决sudo: docker: command not found的问题,但我觉得以上是解决docker命令没法使用的最佳实践。

之前有介绍过,在MacOS下安装Docker Mac Docker安装配置 和在Ubuntu环境下安装Docker Ubuntu环境下安装Docker ,可以参考。

不过在生产环境,大多数是没有外网环境的,所以还是需要以二进制方式来安装。

这篇文章将介绍如何下载Docker二进制文件来安装配置Docker。

查看当前环境

1
2
uname -r
3.10.0-957.el7.x86_64

下载Docker安装包

Docker稳定版的程序可进入如下链接进行下载。

https://download.docker.com/linux/static/stable/x86_64/

1
wget https://download.docker.com/linux/static/stable/x86_64/docker-18.06.3-ce.tgz

解压和拷贝

1
2
tar -xvf docker-18.06.3-ce.tgz
cp docker/* /usr/local/bin

制作Docker.service

创建文件/usr/lib/systemd/system/docker.service并添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io

[Service]
ExecStart=/usr/local/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target

当然,里面还可能会有其他一些配置,大家可以根据自己需要来配置。

启动Docker

1
2
3
4
chmod +x /usr/lib/systemd/system/docker.service

sudo systemctl daemon-reload
sudo systemctl start docker

查看Docker版本信息

如果安装成功,则可以看到如下信息。

1
2
docker -v
Docker version 18.06.3-ce, build d7080c1

docker-compose二进制安装

下载docker-compose文件

1
2
curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

uname -s执行结果是Linux

uname -m执行结果是x86_64

查看docker-compose的版本信息

1
2
docker-compose -v
docker-compose version 1.25.0, build 0a186604

设置Docker镜像代理等信息

vi /etc/docker/daemon.json

1
2
3
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}

通过docker info来查看是否设置成功。

VMWare安装了Ubuntu1904虚拟机,空间不足了,此篇文章将讲解如何扩容磁盘。

查看磁盘使用情况

1
2
3
4
5
6
7
8
9
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 796M 1.9M 794M 1% /run
/dev/sda1 20G 18G 921M 96% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 796M 40K 796M 1% /run/user/1000

可以看出20G的磁盘使用率达到96%了,需要赶紧扩容了。

在VMWare中设置虚拟机属性

首先把Ubuntu虚拟机关机。

在VMWare中可以看到磁盘大小为20G。

点击”扩展”按钮。

磁盘从原始的20G扩展到30G

这里操作完成以后,还需要进入到Ubuntu系统中进行分区和扩展操作。

重启Ubuntu虚拟机

再次查看硬盘使用情况。

1
2
3
4
5
6
7
8
9
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 796M 1.8M 794M 1% /run
/dev/sda1 20G 18G 978M 95% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 796M 28K 796M 1% /run/user/1000

可以发现,实质上是没有变化的。但通过fdisk可以看到/dev/sda设备已经有30G了。

1
2
3
4
5
6
7
8
9
10
11
sudo fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa4334a8d

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 41940991 41938944 20G 83 Linux

备份数据

为了防止数据丢失,为虚拟机做一个快照吧。

扩容操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
sudo fdisk /dev/sda
[sudo] password for simon:

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m

Help:

DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag

Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
u change display/entry units
x extra functionality (experts only)

Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file

Save & Exit
w write table to disk and exit
q quit without saving changes

Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table


Command (m for help):

可以先打印分区表看看基本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
Command (m for help): p
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa4334a8d

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 41940991 41938944 20G 83 Linux

Command (m for help):

如果有交换分区(Linux swap),比如/dev/sdan,则需要关闭。

1
sudo swapoff /dev/sdan

然后删除分区

1
2
3
4
5
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help):

新建分区

1
2
3
4
5
6
7
8
9
10
11
12
13
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-62914559, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914559, default 62914559):

Created a new partition 1 of type 'Linux' and of size 30 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

打印看一下

1
2
3
4
5
6
7
8
9
10
11
Command (m for help): p
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa4334a8d

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 62914559 62912512 30G 83 Linux

确认无误

1
2
3
Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

写入

1
2
3
Command (m for help): w
The partition table has been altered.
Syncing disks.

重启虚拟机

1
sudo reboot

不要着急,这个时候查看磁盘,还是会显示20G的。

执行扩容操作

1
2
3
4
5
6
sudo resize2fs /dev/sda1
[sudo] password for simon:
resize2fs 1.44.6 (5-Mar-2019)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/sda1 is now 7864064 (4k) blocks long.

最后再次查看磁盘信息

1
2
3
4
5
6
7
8
9
10
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 796M 1.7M 794M 1% /run
/dev/sda1 30G 18G 11G 63% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 796M 1.2M 794M 1% /run/user/123
tmpfs 796M 0 796M 0% /run/user/1000

我们发现/dev/sda1已经有30G了,扩容成功。