2022年4月

以下内容结合docker官网文档及网上错误解决方案,在wsl2 Debian 10 上测试通过。

删除旧版本的docker

旧版本的docker名称可能叫docker, docker.io, 或者docker-engine, 如果你之前安装过旧的版本,需要首先卸载:

sudo apt remove docker docker-engine docker.io containerd runc
[sudo] password for david:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker-engine' is not installed, so not removed
E: Unable to locate package containerd

我们现在安装的Docker是社区版的,被称为docker-ce. 有很多种方法可以在 Debian 上安装 Docker-ce, 这里我们直接用Docker仓库(Docker repository)进行安装。

首先需要更新源,并安装依赖包:

sudo apt update
sudo apt install \

apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release

添加 Docker 的官方 GPG 密钥:

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
指定docker仓库性质为稳定版:

echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装docker:

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
运行测试出错

sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
排查过程:

sudo service docker start
grep: /etc/fstab: No such file or directory
[ ok ] Starting Docker: docker.
sudo service docker status [FAIL]
Docker is not running ... failed!
网上找到解决方案,方法如下:

sudo touch /etc/fstab
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
问题解决,运行hello world状况正常:

sudo service docker start
[ ok ] Starting Docker: docker.
sudo docker run 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/

转载链接

背景:

strtotime 是常用的一个函数,在计算增减时间的时候很方便, 比如 strotime("+1 day"),就能得到一个一天后的时间戳。 尽管官方手册不建议用来做运算(+1 day), 但是懒是天性, 能用一行代码解决的就不想再用其他办法, 于是就踩坑了。

测试代码:

php -r "var_dump(date('Y-m-d H:i:s', strtotime('+1 month', strtotime('2021-01-30'))));"

在2021-01-30 的基础上加一个月, 初始写代码的结果是期望得到 2021-02-28 号,然后进行后续逻辑。实际结果如下:
2022-04-01T06:51:34.png

变成了3月2号!!, 完全不是预期的结果!。 但是要是基准日期是7月份,结果又特么对了。 这还只是测试代码, 如果业务中是包装了好几层, 比如每月月底计算下月的数据,就有可能有的月份数据正常, 有的月份数据异常。

原因:
查了下相关资料, 这个是正确结果!!!

strtotime 执行增减日期的步骤:

1:先增减日期 ,2021-01-30 先+1 month 变成 2021-02-30, 2021-07-30 先+1 month 变成 2021-08-30

2:然后再日期规范化, 2021-02-30 不是合法日期, 就规范为 2021-03-02 (要是闰年2020 就是 2020-03-01), 2021-08-30 合法日期 不需要规范。

然后就得出结果。

解决方式:

传说中的优雅 (“first day of ”/"last day of")修正短语。
2022-04-01T06:53:00.png

如果想要循环获取接下来的每个月份,可以使用以下方法:

$date = '2021-01-31 12:00:01';
for ($i = 1; $i <= 24; $i ++)
{
    var_dump(getNextDate($date, $i));
}
function getNextDate($start_time, $month)
{
    $timestamp = strtotime($start_time);
    $next_timestamp = strtotime('+' . $month . ' month', $timestamp);

    $current_month = date('n', $timestamp);
    $should_month = bcmod(bcadd($current_month, $month), 12) ?: 12;
    $calculate_month = date('n', $next_timestamp);

    if ($should_month != $calculate_month) {
        $next_timestamp = strtotime($start_time  . 'last day of +' . $month . ' month');
    }

    return date('Y-m-d 00:00:00', $next_timestamp);
}

结果如下:
2022-04-01T06:56:06.png