Docker 常用命令

Docker logo

Docker 安装

curl -sSL https://get.daocloud.io/docker | sh

Docker-Compose 安装并设置权限

curl -L https://get.daocloud.io/docker/compose/releases/download/v2.3.3/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

配置 Docker 镜像源

创建 daemon.json 文件

sudo mkdir -p /etc/docker

写入镜像源

sudo tee /etc/docker/daemon.json
{
  "registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn","https://registry.docker-cn.com"]
}

赋予普通用户执行 Docker 命令

gpasswd -a username docker

Docker 使用

搜索镜像

docker search debian

拉取镜像

docker pull debian

查看镜像文件

docker images

查看镜像层级关系

docker images tree

查看 Docker 所有进程

docker ps -a

查看 Docker 内存占用

docker stats

开启容器

docker start debian

开启所有容器

docker start $(docker ps -aq)

进入正在运行中的容器

docker exec -it debian /bin/bash

重启容器

docker restart 容器 ID

文件传输

docker cp 本地文件路径 ID全称:容器路径
或
docker cp ID全称:容器文件路径 本地路径

卸载

停止容器

docker stop debian

停止所有容器

docker stop $(docker ps -aq)

删除指定容器

docker rm 容器 ID

删除所有已停止容器

docker rm $(docker ps -aq)

删除所有镜像

docker rmi 镜像 ID

Linux init 详解

什么是 init

init 进程是一个由内核启动的用户级进程

内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序 init 的方式,完成引导进程。所以,init 始终是第一个进程(其进程编号始终为1)。

内核会在过去曾使用过init的几个地方查找它,它的正确位置(对 Linux 系统来说)是 /sbin/init 。如果内核找不到 init,它就会试着运行 /bin/sh ,如果运行失败,系统的启动也会失败。

运行级别

init 一共分为 7 个级别,这 7 个级别的所代表的含义如下:

#init 0 - 停机(千万不能把initdefault 设置为 0)
#init 1 - 单用户模式,只由 root 用户进行维护
#init 2 - 多用户,不能使用NFS(Net File System)不联网
#init 3 - 完全多用户模式(标准的运行级)
#init 4 - 安全模式
#init 5 - X11 (xwindow) 图形化界面模式
#init 6 - 重新启动 (千万不要把 initdefault 设置为6 )

开机默认模式设置方法

系统下有个根文件目录:/etc/inittab

.  # inittab       This file describes how the INIT process should set up
.  #               the system in a certain run-level.
.  #
.  # Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
.  #               Modified for RHS Linux by Marc Ewing and Donnie Barnes
.  #
.  # Default runlevel. The runlevels used by RHS are:
.  #   0 - halt (Do NOT set initdefault to this)
.  #   1 - Single user mode
.  #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
.  #   3 - Full multiuser mode
.  #   4 - unused
.  #   5 - X11
.  #   6 - reboot (Do NOT set initdefault to this)
.  #
.  id:3:initdefault:

如果设置为 id:3:initdefault: 这代表默认启动为命令行模式。如果设置为 id:5:initdefault: 这代表默认 xwindow 图形化界面模式

Debian 系没有 /etc/inittab 文件

使用命令实现,使用 apt install sysv-rc-conf 安装

然后再次执行命令运行:sysv-rc-conf

如何查看当前运行级别

使用 runlevel 命令查看

root@debian:~# runlevel
N 5

MySQL 5.7 安装

MySQL logo

配置环境变量

变量名为:MYSQL_HOME,添加你的 MySQL 文件夹所在位置

编辑 Path,复制 ;%MYSQL_HOME%\bin 到原有值的后面

配置 my.ini 文件

在 MySQL 目录下新建 my.ini 文件,my.ini 文件的内容为:

[mysqld]
#端口号
port = 3306
#mysql-5.7.27-winx64的路径
basedir=D:\Program Files\mysql-5.7.36-winx64
#mysql-5.7.27-winx64的路径+\data
datadir=D:\Program Files\mysql-5.7.36-winx64\data 
#最大连接数
max_connections=200
#编码
character-set-server=utf8
default-storage-engine=INNODB
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysql]

#编码
default-character-set=utf8 

安装 MySQL

以管理员身份运行命令提示符,一定要以管理员的身份运行,否则在安装过程中会出现因为管理权限不够而导致的 Install/Remove of the Service Denied!(安装/卸载服务被拒绝)

在命令提示符中进入到 D:\Program Files\mysql-5.7.36-winx64\bin 目录

输入安装命令:mysqld -install

若出现 Service successfully installed,证明安装成功;如出现 Install of the Service Denied,则说明没有以管理员权限来运行命令提示符

然后继续输入命令:mysqld --initialize,此时不会有任何提示

再输入启动命令:net start mysql,出现提示证明 MySQL 启动成功

D:\Program Files\mysql-5.7.36-winx64\bin>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。

设置 MySQL 密码

在这里设置密码主要是为了解决 ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO) 的问题

首先停止 MySQL 服务,输入命令行 net stop mysql

D:\Program Files\mysql-5.7.36-winx64\bin>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。

在 D:\Program Files\mysql-5.7.36-winx64 目录下找到 my.ini,在 [mysqld] 字段下任意一行添加 skip-grant-tables ,保存即可

重启 MySQL,输入启动命令:net start mysql,出现提示证明 MySQL 启动成功

D:\Program Files\mysql-5.7.36-winx64\bin>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。

在输入命令 mysql -u root -p,不需要输入密码,直接回车

D:\Program Files\mysql-5.7.36-winx64\bin>mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

输入命令行 use mysql,进入数据库

mysql> use mysql
Database changed

输入命令行 update user set authentication_string=password("××××××") where user="root"; ××××××是你设置的新密码,敲击回车后若出现信息,证明修改成功

mysql> update user set authentication_string=password("123456") where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

启动任务管理器,在“服务”中找到 MySQL,点击右键,然后点击停止即可

然后在刚刚的 my.ini 文件中删除 skip-grant-tables 这一行并保存关闭

再次以管理员身份运行命令提示符,输入启动命令:net start mysql,再输入 mysql -u root -p,再输入刚刚设置的密码,出现以下信息证明设置成功

D:\Program Files\mysql-5.7.36-winx64\bin>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

输入命令行 use mysql 验证一下,结果报错

mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

既然没有重置密码,那就重置密码,键入命令行 alter user user() identified by "××××××";

mysql> alter user user() identified by "123456";
Query OK, 0 rows affected (0.00 sec)

再次输入命令行 use mysql 验证,成功

mysql> use mysql
Database changed

至此,MySQL 5.7 安装及配置完成

Debian Bullseye 更新源

Debian Bullseye 网易镜像站

#http://mirrors.163.com/debian/为软件源也可以为其他的 bullseye为版本代号 main non-free contrib区别如下
deb https://mirrors.163.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.163.com/debian/ bullseye main non-free contrib
deb https://mirrors.163.com/debian-security/ bullseye-security main
deb-src https://mirrors.163.com/debian-security/ bullseye-security main
deb https://mirrors.163.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.163.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.163.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.163.com/debian/ bullseye-backports main non-free contrib

Debian Bullseye 腾讯云镜像站

deb https://mirrors.tencent.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.tencent.com/debian/ bullseye main non-free contrib
deb https://mirrors.tencent.com/debian-security/ bullseye-security main
deb-src https://mirrors.tencent.com/debian-security/ bullseye-security main
deb https://mirrors.tencent.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.tencent.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.tencent.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.tencent.com/debian/ bullseye-backports main non-free contrib

Debian Bullseye 阿里云镜像站

deb https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye main non-free contrib
deb https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb-src https://mirrors.aliyun.com/debian-security/ bullseye-security main
deb https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib

Debian Bullseye 华为云镜像站

deb https://mirrors.huaweicloud.com/debian/ bullseye main non-free contrib
deb-src https://mirrors.huaweicloud.com/debian/ bullseye main non-free contrib
deb https://mirrors.huaweicloud.com/debian-security/ bullseye-security main
deb-src https://mirrors.huaweicloud.com/debian-security/ bullseye-security main
deb https://mirrors.huaweicloud.com/debian/ bullseye-updates main non-free contrib
deb-src https://mirrors.huaweicloud.com/debian/ bullseye-updates main non-free contrib
deb https://mirrors.huaweicloud.com/debian/ bullseye-backports main non-free contrib
deb-src https://mirrors.huaweicloud.com/debian/ bullseye-backports main non-free contrib

Debian Bullseye 清华大学镜像站

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

nginx 初始配置文件

nginx.conf 配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

default.conf 配置文件

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

Docker 安装部署教程

Docker logo

Docker 安装

curl -sSL https://get.daocloud.io/docker | sh

Docker-Compose 安装并设置权限

curl -L https://get.daocloud.io/docker/compose/releases/download/v2.3.3/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

配置 Docker 镜像源

创建 daemon.json 文件

sudo mkdir -p /etc/docker

写入镜像源

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn","https://registry.docker-cn.com"]
}
EOF

赋予普通用户执行 Docker 命令

gpasswd -a username docker

重启 Docker 服务

sudo service docker restart

部署 MySQL 容器

docker run --name mysql --restart=always -d -p port:3306 -v $PWD/mysql/data:/var/lib/mysql -v $PWD/mysql/conf/conf.d:/etc/mysql/conf.d -v $PWD/mysql/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD="××××××××××" mysql

部署 phpMyAdmin 容器

docker run --name phpmyadmin --restart=always -d --link mysql:db -p port:80 phpmyadmin/phpmyadmin

部署 WordPress 容器

docker run --name wordpress --restart=always -d -p port:80 --link mysql:db -v $PWD/wordpress:/var/www/html wordpress

部署 Nginx 容器

docker run -p port:80 -p port:443 --name nginx --restart=always -v $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf -v $PWD/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v $PWD/nginx/log:/var/log/nginx -v $PWD/nginx/html:/usr/share/nginx/html -v $PWD/nginx/cert:/etc/nginx/cert -d nginx

Nginx 初始配置文件

Nginx 初始配置文件

部署 NextCloud 容器

docker run -d --name nextcloud --restart=always -p port:80 --link wordpress_mysql:db -v $PWD/nextcloud/:/var/www/html/ nextcloud

以 www-data 身份运行 crontab

crontab -u www-data -e
*/5 * * * * docker exec --user www-data ${container id} php cron.php