我的技术笔记

记录技术历程

转载来源

Convert PEM to DER

1
openssl x509 -outform der -in certificate.pem -out certificate.der

Convert PEM to P7B

1
openssl crl2pkcs7 -nocrl -certfile certificate.cer -out certificate.p7b -certfile CACert.cer

Convert PEM to PFX

1
openssl pkcs12 -export -out certificate.pfx -in fullchain.pem -inkey privkey.pem

Convert DER to PEM

1
openssl x509 -inform der -in certificate.cer -out certificate.pem

Convert P7B to PEM

1
openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer

Convert P7B to PFX

1
2
3
4
openssl pkcs7 -print_certs -in certificate.p7b -out certificate.cer


openssl pkcs12 -export -in certificate.cer -inkey privateKey.key -out certificate.pfx -certfile CACert.cer

Convert PFX to PEM

1
openssl pkcs12 -in certificate.pfx -out certificate.cer -nodes

Docker 初始化部署

获取 docker jenkins 镜像

1
2
3
4
5
# 长期支持版本
docker pull jenkins/jenkins:lts

# 最新版本
docker pull jenkins/jenkins

初始 参数解释

docker 环境 目录 /var/jenkins_home 映射宿主目录 ~/jenkins
docker 环境 端口 8080 映射宿主端口 8002

1
2
3
4
5
# 后台方式运行
docker run -d -p 8002:8080 -v ~/jenkins:/var/jenkins_home --name jenkins --restart=always jenkins/jenkins

# 查看容器日志
docker logs -f jenkins

发现错误日志

1
2
touch: cannot touch '/var/jenkins_home/xxxx.log': Permission denied
Can not write to /var/jenkins_home/xxxx.log. Wrong volume permissions?

需要在 宿主 对目录授权 ~/jenkins

1
2
3
4
5
# 开启目录权限
sudo chmod a+rwx ~/jenkins

# 开启docker挂载权限
sudo chmod a+rw /var/run/docker.sock

Docker 中更新 jenkins

宿主下载 jenkins.war 目录 ~/jenkins,进入 docker shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 普通用户身份 进入容器 shell 其中 861b 为容器Id 861be1055500 的前4位,支持模糊查询
docker exec -it 861b /bin/bash

# root 身份 进入容器 shell 其中 861b 为容器Id 861be1055500 的前4位,支持模糊查询
docker exec -it -u root 861b /bin/bash

# 替换后
cp /var/jenkins_home/jenkins.war /usr/share/jenkins/jenkins.war
exit

# 重启容器
docker restart 861b

#下载 Android SDK 工具 并解压到 android.sdk
wget -O sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-6609375_latest.zip && unzip sdk.zip -d android.sdk && rm sdk.zip

Hello,World

Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。

eg:使用 Ubuntu 20.04 镜像, 输出 Hello World

1
docker run ubuntu:20.04 /bin/echo "Hello World From Ubuntu:20.04"

常用命令

查看容器

1
docker ps -a

关闭所有正在运行的容器

1
docker kill $(docker ps -a -q)

删除指定容器

1
docker rm -f 容器ID

清理所有 处于中止状态的容器

1
docker container prune

删除所有已经停止的容器

1
docker rm $(docker ps -a -q)

意外情况.删除容器。没能删除成功,出现 “Removal In Progress”

1
2
3
4
5
6
7
8
9
10
11
# 进入容器目录
cd /var/lib/docker/containers

# 找到上一步找到的hash值开头的文件夹,小心谨慎的删掉它
rm -rf xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# 重启服务
systemctl restart docker

# 核对检查
docker ps -a

删除所有未打 dangling 标签的镜像

1
docker rmi $(docker images -q -f dangling=true)

查看镜像

1
docker images

删除所有镜像

1
docker rmi $(docker images -q)

Dockerfile

1
2
# 通过当前目录下的 Dockerfile 创建镜像 xxx
docker build -t xxx .

初识 Docker

准备

查看 Ubuntu 是32位的还是64位的

1
getconf LONG_BIT

系统信息

1
lsb_release -a

查看操作系统架构

1
uname -a

安装

添加 Docker 官方 GPG KEY

1
2
3
4
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 国内阿里云版
sudo curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -

验证 KEY 的指纹

1
2
3
4
5
6
7
sudo apt-key fingerprint 0EBFCD88

# 输出为:
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ 未知 ] Docker Release (CE deb) <[email protected]>
sub rsa4096 2017-02-22 [S]

添加稳定版 repository

1
2
3
4
5
6
7
8
9
10
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

# 国内阿里云版
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"

更新

1
sudo apt-get update

安装最新版本的 Docker ce 和 Containerd

1
sudo apt-get install docker-ce docker-ce-cli containerd.io

安装指定版本的 Docker ce 和 Containerd,先查看可获取的版本

1
apt-cache madison docker-ce

安装指定版本

1
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

验证安装版本

1
docker --version

用户授权

将非root用户加入docker组,以允许免sudo执行docker

1
sudo gpasswd -a 用户名 docker

重启服务并刷新 docker组成员

1
2
3
sudo service docker restart

newgrp - docker

设置开机启动 Docker-ce

安装成功后默认已设置并启动,可忽略

1
2
sudo systemctl enable docker
sudo systemctl start docker

升级版本

安装上述流程,安装新版本即可

卸载

方案1

1
sudo apt-get remove docker docker-engine docker.io containerd runc

/var/lib/docker 的内容,包括镜像、容器、卷和网络,可以保留也可以删除。

但仍能看到docker版本

1
docker --version

方案2

1
2
3
sudo apt-get purge docker
sudo apt-get purge docker-ce
sudo apt-get remove -y docker-*

慎重,不要误删!!!包括镜像、容器、卷和网络

1
sudo rm -rf /var/lib/docker

会删除软件包而保留软件的配置文件

1
apt-get remove 

会同时清除软件包和软件的配置文件

1
apt-get purge

Docker 镜像加速

修改配置文件 /etc/docker/daemon.json 即可

1
sudo vim /etc/docker/daemon.json

加载重启docker

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

查看是否成功

1
docker info

查看镜像列表

1
docker images

删除镜像

1
2
3
4
docker rmi xxxx

# 强制删除
docker rmi -f xxxxx

容器列表

1
docker ps -a 

进入容器 bash

1
2
3
4
5
6
7


# attach 进入 容器
docker attach xxxx

# exec 进入一个已经在运行的容器
docker exec -i -t xxxx /bin/bash

删除容器

1
2
3
4
5
6
docker container rm -f xxx

# -f, --force 是够强制终止并删除一个运行中的容器;
# --help 帮助信息;
# -l, --link 删除容器的链接,但是保留容器;
# -v, --volumes 删除容器挂载的数据卷。

Nginx 默认编译配置参数

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
--with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-5J5hor/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' \
--prefix=/usr/share/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--modules-path=/usr/lib/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-debug \
--with-compat \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module \

Nginx 默认编译配置参数 - 支持

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
DEFAULT_BUILD_SRC_DIR=/opt/nginx-src

NGINX_GZ=${DEFAULT_BUILD_SRC_DIR}/nginx-1.18.0.tar.gz
NGINX_SRC=${DEFAULT_BUILD_SRC_DIR}/nginx-1.18.0
NGINX_GZ_SHA256=4C373E7AB5BF91D34A4F11A0C9496561061BA5EEE6020DB272A17A7228D35F99

NGINX_GZ_DOWN_LOAD=0
NGINX_GZ_OK=0

if [ ! -d "${DEFAULT_BUILD_SRC_DIR}" ]; then
mkdir "${DEFAULT_BUILD_SRC_DIR}"
fi

if [ -f "${NGINX_GZ}" ]; then
sha256sum_results=$(sha256sum $NGINX_GZ)
sha256sum_results=${sha256sum_results^^}
if [[ $sha256sum_results =~ "${NGINX_GZ_SHA256}" ]]; then
NGINX_GZ_DOWN_LOAD=0
NGINX_GZ_OK=1
else
NGINX_GZ_DOWN_LOAD=1
fi
unset sha256sum_results
else
NGINX_GZ_DOWN_LOAD=1
fi


if [ $NGINX_GZ_DOWN_LOAD -eq 1 ]; then
# Download Nginx 1.18.0
wget -O $NGINX_GZ http://nginx.org/download/nginx-1.18.0.tar.gz
if [ -f "${NGINX_GZ}" ]; then
sha256sum_results=$(sha256sum $NGINX_GZ)
sha256sum_results=${sha256sum_results^^}
if [[ $sha256sum_results =~ "${NGINX_GZ_SHA256}" ]]; then
NGINX_GZ_OK=1
fi
unset sha256sum_results
fi
fi
unset NGINX_GZ_DOWN_LOAD

if [ -d "${NGINX_SRC}" ]; then
rm -rf "${NGINX_SRC}"
fi

echo "FILE OK:${NGINX_GZ_OK}"
if [ $NGINX_GZ_OK -eq 1 ]; then
# 安装依赖
apt-get install openssl libssl-dev
apt-get install libpcre3 libpcre3-dev #>/dev/null
tar xvf $NGINX_GZ -C $DEFAULT_BUILD_SRC_DIR >/dev/null 2>&1
# 解压成功
cd $NGINX_SRC >/dev/null 2>&1
# echo $(pwd)
./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-5J5hor/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' \
--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' \
--prefix=/usr/share/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--modules-path=/usr/lib/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
# --with-debug \
# --with-compat \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-mail=dynamic \
--with-mail_ssl_module
fi
0%