Docker:修订间差异
来自康健生活
| (未显示同一用户的104个中间版本) | |||
| 第1行: | 第1行: | ||
== 安装 == | |||
{| class="wikitable" | |||
|- | |||
| [[/macos|macos]] || 示例 || 示例 | |||
|} | |||
=== Ubuntu === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo apt remove docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc | |||
sudo apt install ca-certificates curl gnupg lsb-release | |||
export http_proxy=http://192.168.51.34:808 https_proxy=http://192.168.51.34:808 | |||
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg | |||
sudo echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null | |||
sudo apt clean && apt update | |||
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin | |||
unset http_proxy; unset https_proxy;</syntaxhighlight>|$|/home/kangkk}} | |||
网传另一种使用阿里源的方法,这里不做推荐,仅供参考 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |||
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"</syntaxhighlight>|$|/home/kangkk}} | |||
=== Compose === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo curl -SL https://github.com/docker/compose/releases/download/v2.29.1/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose | |||
sudo chmod +x /usr/local/bin/docker-compose</syntaxhighlight>|$|/home/kangkk}} | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker-compose --version</syntaxhighlight>|$|/home/kangkk}} | |||
== 源 == | |||
== docker == | == docker == | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/container_name|container name]] || 示例 || 示例 | | [[/container_name|container name]] || 示例 || 示例 | ||
|} | |||
== 权限 == | |||
查看权限 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
ls -l /var/run/docker.sock</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
srw-rw---- 1 root docker 0 Nov 21 15:42 /var/run/docker.sock | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
从 docker.sock 文件的状态信息中提取所有者的组 ID | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
stat -c '%g' /var/run/docker.sock</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
992 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
添加当前用户到 docker 组。 | |||
<blockquote><pre> | |||
sudo groupadd docker | |||
sudo usermod -aG docker ${USER} | |||
sudo su | |||
su kangjian | |||
</pre></blockquote> | |||
{{shc|<syntaxhighlight lang="bash"> | |||
gpasswd -a $USER docker</syntaxhighlight>|$|/home/kangkk}} | |||
== docker.sock == | |||
如果 docker.sock 没有生效,可能会输出如下信息。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
curl -s --unix-socket /var/run/docker.sock http:/containers/json | jq .</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
{ | |||
"message": "page not found" | |||
} | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
正常信息如下 | |||
<pre> | |||
[ | |||
{ | |||
"Id": "b94a464760c7ef1f63486500b8e1f24c34ae934b6dac02d19336850faef8a543", | |||
"Names": [ | |||
"/runner1" | |||
], | |||
"Image": "drone/drone-runner-docker:1", | |||
"ImageID": "sha256:37113502465f8d42b091f08e7e25b48c613918ba810703565d0e59cd81d9c4d0", | |||
"Command": "/bin/drone-runner- | |||
... | |||
"RW": true, | |||
"Propagation": "rprivate" | |||
}, | |||
{ | |||
"Type": "bind", | |||
"Source": "/var/www/cii.qiwoo.org/log", | |||
"Destination": "/var/log/nginx", | |||
"Mode": "rw", | |||
"RW": true, | |||
"Propagation": "rprivate" | |||
} | |||
] | |||
} | |||
]</pre> | |||
== 参数 == | |||
=== --data-root === | |||
设置镜像存储路径。 | |||
== dockerfile == | |||
{| class="wikitable" | |||
|- | |- | ||
| | | [[/dockerfile|dockerfile]] | ||
|} | |} | ||
== 常见 == | == 常见 == | ||
{| class="wikitable" | {| class="wikitable" | ||
| 第17行: | 第113行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
== | |||
=== login === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker login -u username -p password</syntaxhighlight>|$|/home/kangkk}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
echo 'D&u******&a7i' | docker login https://index.docker.io -u kangkk --password-stdin</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
Login Succeeded | |||
Logging in with your password grants your terminal complete access to your account. | |||
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/ | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 应用 == | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/ | | [[/elasticsearch|elasticsearch]] || 示例 || 示例 | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
| 第27行: | 第136行: | ||
|} | |} | ||
== | == 数据卷 == | ||
指定目录挂载。 | |||
<blockquote><syntaxhighlight lang="bash"> | |||
docker run -it -v 主机目录:容器内目录</syntaxhighlight></blockquote> | |||
匿名挂载。 | |||
<blockquote><syntaxhighlight lang="bash"> | |||
docker run -it -v 容器内目录 | |||
# 主机目录默认为 /var/lib/docker/volumes/<id>/_data</syntaxhighlight></blockquote> | |||
具名挂载。 | |||
<blockquote><syntaxhighlight lang="bash"> | |||
docker run -it -v 自定义卷名:容器内目录 | |||
# 主机目录默认为 /var/lib/docker/volumes/<id>/_data</syntaxhighlight></blockquote> | |||
== 参数 == | |||
=== --no-cache === | |||
== 命令 == | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/ | | [[/if|if]] || 示例 || 示例 | ||
|- | |||
| 示例 || 示例 || 示例 | |||
|- | |||
| 示例 || 示例 || 示例 | |||
|} | |} | ||
| 第42行: | 第173行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
== 镜像 == | |||
=== 镜像列表 === | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
docker images</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
REPOSITORY TAG IMAGE ID CREATED SIZE | |||
node lts-alpine b1ca7421d2e7 2 days ago 112MB | |||
alpine 3.13.5 6dbb9cc54074 16 months ago 5.61MB | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== 镜像重命名 === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker image tag node:lts-alpine kangkk.com/node:lts-alpine | |||
# or | |||
docker image tag b1ca7421d2e7 kangkk.com/node:lts-alpine</syntaxhighlight>|$|/home/kangkk}} | |||
再次查看镜像列表就可以看到新增的重命名之后的镜像了。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
docker images</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
REPOSITORY TAG IMAGE ID CREATED SIZE | |||
kangkk.com/node lts-alpine b1ca7421d2e7 2 days ago 112MB | |||
node lts-alpine b1ca7421d2e7 2 days ago 112MB | |||
alpine 3.13.5 6dbb9cc54074 16 months ago 5.61MB | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 制作镜像 == | == 制作镜像 == | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/nginxnode|nginxnode]]|| [[/centos|centos]] || | | [[/nginxnode|nginxnode]]|| [[/centos|centos]] || [[/yapi|yapi]] | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
| 第51行: | 第208行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
==== 导出镜像 ==== | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker save -o /root/images/image_name.tar image_name:latest</syntaxhighlight>|$|/home/kangkk}} | |||
==== 导入镜像 ==== | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker load < image_name.tar | |||
## | |||
docker load –i image_name.tar | |||
## | |||
docker load –input image_name.tar</syntaxhighlight>|$|/home/kangkk}} | |||
== 环境 == | == 环境 == | ||
| 第61行: | 第230行: | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
== 镜像 == | |||
进入镜像。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker run -it --entrypoint sh <镜像名称></syntaxhighlight>|$|/home/kangkk}} | |||
并挂载镜像地址(--privileged 设置 root 权限) | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker run -it --privileged -v <本地目录绝对路径>:<docker路径> --entrypoint sh <镜像名称></syntaxhighlight>|$|/home/kangkk}} | |||
开启一些服务会遇到 <code>Failed to connect to bus: Host is down</code> 错误。改为如下: | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker run -itd --privileged -v <本地目录绝对路径>:<docker路径> --name <自定义服务名称> <镜像名称> /usr/sbin/init \ | |||
docker exec -it <自定义服务名称> /bin/bash | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
下载镜像中的文件到本地磁盘。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker cp <容器id>:<容器内地址> <本地地址></syntaxhighlight>|$|/home/kangkk}} | |||
示例。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker cp ed8c1251c61c:/opt/app/src /Users/kangjian/Downloads</syntaxhighlight>|$|/home/kangkk}} | |||
=== 重新打包 === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker commit -m "build nginx image" -a "kangkk" 5a9aa788a91e qnpm-nginx</syntaxhighlight>|$|/home/kangkk}} | |||
在重新打包的过程中,需要保持容器运行状态? | |||
运行容器 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker run -it d66f22a3c894 -i /bin/bash</syntaxhighlight>|$|/home/kangkk}} | |||
另起 tab 进入容器 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker exec -it 38de0181d5b4 bash</syntaxhighlight>|$|/home/kangkk}} | |||
最后 commit | |||
== 查看 == | |||
我开始查询当前容器。但是抛出 docker 并未运行。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
docker container ls -all</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
启动 docker 服务。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo systemctl start docker.service</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
We trust you have received the usual lecture from the local System | |||
Administrator. It usually boils down to these three things: | |||
#1) Respect the privacy of others. | |||
#2) Think before you type. | |||
#3) With great power comes great responsibility. | |||
[sudo] password for kangjian: | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
再次查看 docker 的状态。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
systemctl status docker.service</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
● docker.service - Docker Application Container Engine | |||
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) | |||
Active: active (running) since Wed 2022-03-23 14:22:44 CST; 11s ago | |||
Docs: https://docs.docker.com | |||
Main PID: 24342 (dockerd) | |||
Memory: 38.4M | |||
CGroup: /system.slice/docker.service | |||
└─24342 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
再次查询,这个时候提示需要 root 权限。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
docker container ls -all</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
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.24/containers/json?all=1&limit=1: dial unix /var/run/docker.sock: connect: permission denied | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker container ls -all</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |||
1aa50bf020ef mysql:5.6 "docker-entrypoint.s…" 34 seconds ago Up 31 seconds 0.0.0.0:3306->3306/tcp mysql-56 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
删除当前容器 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker container rm 容器名</syntaxhighlight>|$|/home/kangkk}} | |||
进入容器 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker exec -it elasticsearch /bin/bash</syntaxhighlight>|$|/home/kangkk}} | |||
查看容器日志地址 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker inspect --format '{{.LogPath}}' 5f3d48017cd3</syntaxhighlight>|$|/home/kangkk}} | |||
== 启动 == | |||
=== 启动全部 === | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker start $(sudo docker ps -a | awk '{ print $1}' | tail -n +2)</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
34b847559496 | |||
1aa50bf020ef | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 关闭 == | |||
=== 关闭全部 === | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker stop $(sudo docker ps -a | awk '{ print $1}' | tail -n +2)</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
34b847559496 | |||
1aa50bf020ef | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 搜索 == | |||
<blockquote><pre> | |||
docker search [option] keyword</pre></blockquote> | |||
添加镜像之前,我需要先查找镜像,如我现在需要安装 mysql。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker search mysql</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
NAME DESCRIPTION STARS OFFICIAL AUTOMATED | |||
mysql MySQL is a widely used, open-source relation… 12288 [OK] | |||
mariadb MariaDB Server is a high performing open sou… 4726 [OK] | |||
mysql/mysql-server Optimized MySQL Server Docker images. Create… 911 [OK] | |||
percona Percona Server is a fork of the MySQL relati… 572 [OK] | |||
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 476 [OK] | |||
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 93 | |||
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92 | |||
bitnami/mysql Bitnami MySQL Docker Image 67 [OK] | |||
databack/mysql-backup Back up mysql databases to... anywhere! 58 | |||
circleci/mysql MySQL is a widely used, open-source relation… 25 | |||
ubuntu/mysql MySQL open source fast, stable, multi-thread… 25 | |||
mysql/mysql-router MySQL Router provides transparent routing be… 23 | |||
centos/mysql-56-centos7 MySQL 5.6 SQL database server 22 | |||
google/mysql MySQL server for Google Compute Engine 20 [OK] | |||
vmware/harbor-db Mysql container for Harbor 10 | |||
mysqlboy/mydumper mydumper for mysql logcial backups 3 | |||
mysqlboy/docker-mydumper docker-mydumper containerizes MySQL logical … 3 | |||
bitnami/mysqld-exporter 2 | |||
ibmcom/mysql-s390x Docker image for mysql-s390x 1 | |||
cimg/mysql 0 | |||
ibmcom/tidb-ppc64le TiDB is a distributed NewSQL database compat… 0 | |||
mysqlboy/elasticsearch 0 | |||
mysqleatmydata/mysql-eatmydata 0 | |||
mysql/mysql-operator MySQL Operator for Kubernetes 0 | |||
mirantis/mysql 0 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
可以通过子命令进行过滤,比如只搜索查看官方镜像。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker search --filter=is-official=true mysql</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
NAME DESCRIPTION STARS OFFICIAL AUTOMATED | |||
mysql MySQL is a widely used, open-source relation… 12288 [OK] | |||
mariadb MariaDB Server is a high performing open sou… 4726 [OK] | |||
percona Percona Server is a fork of the MySQL relati… 572 [OK] | |||
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 476 [OK] | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
搜索历史版本。{{Warning|已经失效}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
curl https://registry.hub.docker.com/v1/repositories/mysql/tags\ | |||
| tr -d '[\[\]" ]' | tr '}' '\n'\ | |||
| awk -F: -v image='mysql' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
% Total % Received % Xferd Average Speed Time Time Time Current | |||
Dload Upload Total Spent Left Speed | |||
100 4654 0 4654 0 0 1271 0 --:--:-- 0:00:03 --:--:-- 1271 | |||
mysql:latest | |||
mysql:5 | |||
mysql:5-oracle | |||
mysql:5.5 | |||
mysql:5.5.40 | |||
... | |||
mysql:5.6 | |||
... | |||
mysql:5.6.51 | |||
... | |||
mysql:8 | |||
mysql:8-debian | |||
mysql:8-oracle | |||
mysql:8.0 | |||
... | |||
mysql:8.0.28 | |||
mysql:8.0.28-debian | |||
mysql:8.0.28-oracle | |||
mysql:8.0.3 | |||
mysql:8.0.4 | |||
mysql:8.0.4-rc | |||
mysql:debian | |||
mysql:oracle | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
curl https://registry.hub.docker.com/_/mysql/tags | tr -d '[\[\]" ]' | tr '}' '\n'| awk -F: -v image='mysql' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
% Total % Received % Xferd Average Speed Time Time Time Current | |||
Dload Upload Total Spent Left Speed | |||
100 2271 100 2271 0 0 2042 0 0:00:01 0:00:01 --:--:-- 2055 | |||
mysql:300,400,400i,600,600i,700 | |||
mysql:400,500,600,700 | |||
mysql:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&family=Roboto | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 添加 == | |||
<blockquote><pre> | |||
docker pull [options] name[:tag|@digest]</pre></blockquote> | |||
我需要先拉取镜像。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker pull mysql:5.6</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[sudo] password for kangjian: | |||
5.6: Pulling from library/mysql | |||
35b2232c987e: Pull complete | |||
fc55c00e48f2: Pull complete | |||
0030405130e3: Pull complete | |||
e1fef7f6a8d1: Pull complete | |||
1c76272398bb: Pull complete | |||
f57e698171b6: Pull complete | |||
f5b825b269c0: Pull complete | |||
dcb0af686073: Pull complete | |||
27bbfeb886d1: Pull complete | |||
6f70cc868145: Pull complete | |||
1f6637f4600d: Pull complete | |||
Digest: sha256:20575ecebe6216036d25dab5903808211f1e9ba63dc7825ac20cb975e34cfcae | |||
Status: Downloaded newer image for mysql:5.6 | |||
docker.io/library/mysql:5.6 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
查看已经下载到本地的镜像。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker images</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
REPOSITORY TAG IMAGE ID CREATED SIZE | |||
mysql 8.0 562c9bc24a08 4 days ago 521MB | |||
mysql 5.6 dd3b2a5dcb48 3 months ago 303MB | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 删除 == | == 删除 == | ||
=== 删除镜像 === | === 删除镜像 === | ||
| 第71行: | 第473行: | ||
* -f, -force: 强制删除镜像,即便有容器引用该镜像; | * -f, -force: 强制删除镜像,即便有容器引用该镜像; | ||
* -no-prune: 不要删除未带标签的父镜像; | * -no-prune: 不要删除未带标签的父镜像; | ||
删除所有镜像。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker rmi $(sudo docker images | awk '{print $3}' |tail -n +2)</syntaxhighlight>|$|/home/kangkk}} | |||
=== 删除容器 === | === 删除容器 === | ||
删除所有容器。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker rm $(sudo docker ps -a | awk '{ print $1}' | tail -n +2)</syntaxhighlight>|$|/home/kangkk}} | |||
== 宿主机创建数据目录和配置文件 == | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo mkdir -p /mysql_data/mysql56/data /mysql_data/mysql56/log /mysql_data/mysql56/conf | |||
sudo chmod -R 755 /mysql_data</syntaxhighlight>|$|/home/kangkk}} | |||
<div class="pageBoxGreen">mkdir -p 递归创建目录,如若上级目录不存在,按照目录层级自动创建目录。</div> | |||
添加配置文件。 | |||
{{Hc|/mysql_data/mysql56/conf/my.cnf|<syntaxhighlight lang="javascript"> | |||
[client] | |||
default-character-set = utf8mb4 | |||
[mysqld] | |||
datadir = /usr/mysql/data | |||
general_log = 1 | |||
general_log_file = /var/log/mysql/access.log | |||
log-error = /var/log/mysql/error.log | |||
character_set_server = utf8mb4 | |||
collation_server = utf8mb4_bin | |||
secure-file-priv= NULL | |||
symbolic-links=0 | |||
!includedir /etc/mysql/conf.d/</syntaxhighlight>}} | |||
启动创建容器。 | |||
<blockquote><pre> | |||
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]</pre></blockquote> | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker run -itd --name=mysql-56 -v /mysql_data/mysql56/conf/my.cnf:/etc/mysql/my.cnf -v /mysql_data/mysql56/data:/usr/mysql/data -v /mysql_data/mysql56/log:/var/log/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=kangjian mysql:5.6</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
1aa50bf020ef42ff18d4da3076aea89c5d5f4950d8b6ccde9d9ac1c03b329f9f | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
进入容器。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker exec -it mysql-56 /bin/bash</syntaxhighlight>|$|/home/kangkk}} | |||
== 清理 == | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker system prune</syntaxhighlight>|$|/home/kangkk}} | |||
== error == | == error == | ||
{| class="wikitable" | {| class="wikitable" | ||
| 第85行: | 第532行: | ||
* Error response from daemon: manifest for elasticsearch:latest not found: manifest unknown: manifest unknown | * Error response from daemon: manifest for elasticsearch:latest not found: manifest unknown: manifest unknown | ||
: 需要指定版本号 {{shc|<syntaxhighlight lang="bash"> | |||
sudo docker pull elasticsearch:7.6.2</syntaxhighlight>|$|/home/kangkk}} | sudo docker pull elasticsearch:7.6.2</syntaxhighlight>|$|/home/kangkk}} | ||
* docker: Error response from daemon: Conflict. The container name "/elasticsearch" is already in use by container "bad7c467154756bdd651080e6d223fa2c380662e6c91af83b9c3c8f958d8f103". You have to remove (or rename) that container to be able to reuse that name. | |||
=== 部署过程中 === | |||
* Back-off restarting failed container | |||
: 实例事件中一直出现Back-off restarting failed container的Warning事件,一般是由于通过指定的镜像启动容器后,容器内部没有常驻进程,导致容器启动成功后即退出,从而进行了持续的重启。 | |||
== 日志 == | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker container ls -all</syntaxhighlight>|<syntaxhighlight lang="text"> | |||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |||
8edadee2b3f8 mysql:8.0 "docker-entrypoint..." 8 minutes ago Exited (1) 8 minutes ago mysql-80 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
查看容器启动日志。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker logs 8edadee2b3f8 | |||
</syntaxhighlight>|<syntaxhighlight lang="text"> | |||
2022-04-12 11:17:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. | |||
2022-04-12 11:17:55+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' | |||
2022-04-12 11:17:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started. | |||
2022-04-12 11:17:55+00:00 [Note] [Entrypoint]: Initializing database files | |||
2022-04-12T11:17:55.534138Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. | |||
2022-04-12T11:17:55.534234Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. | |||
2022-04-12T11:17:55.534250Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.28) initializing of server in progress as process 41 | |||
2022-04-12T11:17:55.548592Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. | |||
2022-04-12T11:17:56.758459Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. | |||
2022-04-12T11:17:59.081271Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. | |||
2022-04-12 11:18:03+00:00 [Note] [Entrypoint]: Database files initialized | |||
2022-04-12 11:18:03+00:00 [Note] [Entrypoint]: Starting temporary server | |||
mysqld will log errors to /opt/datas/docker/mysql/data/4a0c074b7272.err | |||
mysqld is running as pid 90 | |||
2022-04-12 11:18:04+00:00 [Note] [Entrypoint]: Temporary server started. | |||
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. | |||
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. | |||
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. | |||
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. | |||
2022-04-12 11:18:06+00:00 [Note] [Entrypoint]: Stopping temporary server | |||
2022-04-12 11:18:08+00:00 [Note] [Entrypoint]: Temporary server stopped | |||
2022-04-12 11:18:08+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up. | |||
2022-04-12T11:18:09.290345Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. | |||
2022-04-12T11:18:09.290508Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. | |||
2022-04-12T11:18:09.290561Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1 | |||
2022-04-12T11:18:09.303870Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. | |||
2022-04-12T11:18:09.579432Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. | |||
2022-04-12T11:18:09.831051Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. | |||
2022-04-12T11:18:09.831094Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel. | |||
2022-04-12T11:18:09.861635Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock | |||
2022-04-12T11:18:09.861684Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== inspect == | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker inspect elasticsearch</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[ | |||
{ | |||
"Id": "a9580fa1184b9cb5a6f945f4ee3235033699244903cb625694d2a0fc715abad9", | |||
"Created": "2021-05-30T07:36:52.122183052Z", | |||
"Path": "/usr/local/bin/docker-entrypoint.sh", | |||
"Args": [ | |||
"eswrapper" | |||
], | |||
... | |||
"GlobalIPv6PrefixLen": 0, | |||
"MacAddress": "02:42:ac:11:00:02", | |||
"DriverOpts": null | |||
} | |||
} | |||
} | |||
} | |||
] | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
=== IPAddress === | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker inspect elasticsearch | grep IPAddress</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
"SecondaryIPAddresses": null, | |||
"IPAddress": "172.17.0.2", | |||
"IPAddress": "172.17.0.2", | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 网络 == | |||
查看网络状态。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker network ls</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
NETWORK ID NAME DRIVER SCOPE | |||
2423ab85dd92 bridge bridge local | |||
d05fc848b239 host host local | |||
9213fe5ad3df none null local | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
参数: | |||
# bridge:桥接 docker | |||
# none:不配置网络 | |||
# host:和宿主机共享网络 | |||
# container:容器网络连通 | |||
=== bridge network === | |||
桥接网络 | |||
=== overlay network === | |||
=== MACVLAN network === | |||
=== 创建自定义网络 === | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker network create --driver bridge --subnet 192.168.1.0/16 --gateway 192.168.1.0 kangkknet</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
1087cad22931daafb73b814cb28f531f57889ce5fbd85061cbc3ada7cf729272 | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
参数: | |||
# --driver bridge 表示使用桥接模式 | |||
# --subnet 192.168.1.0/16 表示子网ip,可以分配 192.168.1.2 到 192.168.255.255 | |||
# --gateway 192.168.1.0 表示网关 | |||
# kangkknet 表示网络名 | |||
检查添加状态。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker network ls</syntaxhighlight>|<syntaxhighlight lang="bash" highlight="4"> | |||
NETWORK ID NAME DRIVER SCOPE | |||
2423ab85dd92 bridge bridge local | |||
d05fc848b239 host host local | |||
1087cad22931 kangkknet bridge local | |||
9213fe5ad3df none null local | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
网络相关信息。 | |||
{{Shc_out|<syntaxhighlight lang="bash"> | |||
sudo docker network inspect kangkknet</syntaxhighlight>|<syntaxhighlight lang="bash"> | |||
[ | |||
{ | |||
"Name": "kangkknet", | |||
"Id": "1087cad22931daafb73b814cb28f531f57889ce5fbd85061cbc3ada7cf729272", | |||
"Created": "2022-04-28T17:08:54.05829233+08:00", | |||
"Scope": "local", | |||
"Driver": "bridge", | |||
"EnableIPv6": false, | |||
"IPAM": { | |||
"Driver": "default", | |||
"Options": {}, | |||
"Config": [ | |||
{ | |||
"Subnet": "192.168.1.0/16", | |||
"Gateway": "192.168.1.0" | |||
} | |||
] | |||
}, | |||
"Internal": false, | |||
"Attachable": false, | |||
"Ingress": false, | |||
"ConfigFrom": { | |||
"Network": "" | |||
}, | |||
"ConfigOnly": false, | |||
"Containers": {}, | |||
"Options": {}, | |||
"Labels": {} | |||
} | |||
] | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
== 存储空间映射 == | |||
{| class="wikitable" | |||
|- | |||
! 应用 !! 配置文件 !! 数据 !! 日志 !! 参数 | |||
|- | |||
| redis || /usr/local/etc/redis || /data || /opt/redis || | |||
|- | |||
| mongo || /etc/mongo || /data/db || /var/log/mongodb || | |||
|- | |||
| mysql || /etc/mysql/conf.d || /var/lib/mysql || /var/log/mysql || 1. MYSQL_ROOT_PASSWORD | |||
|- | |||
| gitlab || /etc/gitlab || /var/opt/gitlab || /var/log/gitlab || | |||
|- | |||
| postgresql || /etc/postgresql || /var/lib/postgresql/data || /var/log/postgresql || 1. POSTGRES_PASSWORD | |||
|} | |||
== 文件 == | |||
从复制容器内的文件到外面。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker cp f6bbd9850c15:/usr/src/app /home/kangjian/data</syntaxhighlight>|$|/home/kangkk}} | |||
== 构建 == | |||
{| class="wikitable" | |||
|- | |||
| [[编程语言/Drone]] | |||
|} | |||
== Volume == | |||
=== 类型 === | |||
* host volume:在宿主机上,可从容器内访问 | |||
* named volume:docker 管理 volume 位置,存在命名的名称 | |||
* anonymous volume:docker 管理 volume 位置,不存在命名的名称 | |||
=== type === | |||
==== bind ==== | |||
==== volume ==== | |||
==== tmpfs ==== | |||
如果是存储容器生成的非持久性状态数据,可以使用 [https://docs.docker.com/engine/storage/tmpfs/ tmpfs],在容器运行的整个生命周期内数据是有效的,可以避免写入容器的可写层来提高容器的性能,当容器重启后数据会消失。 | |||
=== 创建卷 === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker volume create vol</syntaxhighlight>|$|/home/kangkk}} | |||
tmpfs 卷 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker volume create -d local -o type=tmpfs -o device=tmpfs -o o=size=20480m,uid=1000 --name verdaccio_storage</syntaxhighlight>|$|/home/kangkk}} | |||
查看卷列表 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker volume ls</syntaxhighlight>|$|/home/kangkk}} | |||
<pre> | |||
属性 描述 | |||
Driver 卷驱动 | |||
Labels 标签 | |||
Mountpoint 宿主机挂载点 | |||
Name 名称 | |||
Options 挂载参数 | |||
Scope 作用域</pre> | |||
使用格式化参数过滤信息 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker volume ls --format "{{.Name}}: {{.Driver}}"</syntaxhighlight>|$|/home/kangkk}} | |||
卷详细信息 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker volume inspect vol</syntaxhighlight>|$|/home/kangkk}} | |||
删除卷 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker volume rm vol</syntaxhighlight>|$|/home/kangkk}} | |||
删除所有未在使用中的卷 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker volume prune</syntaxhighlight>|$|/home/kangkk}} | |||
== 代理 == | |||
源于广义上存在的墙(或者两侧墙),Docker 非官方源不完成的情况下,只好使用代理进行镜像拉取。 | |||
编辑 Docker 源,设置为官方源。 | |||
{{hc|head=/etc/docker/daemon.json|output=<syntaxhighlight lang="bash"> | |||
{ | |||
"registry-mirrors": ["https://hub.docker.com/"] | |||
} | |||
</syntaxhighlight>}} | |||
编辑代理文件。 | |||
{{hc|head=/etc/systemd/system/docker.service.d/proxy.conf|output=<syntaxhighlight lang="bash"> | |||
[Service] | |||
Environment="HTTP_PROXY=http://192.168.51.34:808" | |||
Environment="HTTPS_PROXY=http://192.168.51.34:808" | |||
Environment="NO_PROXY=localhost,127.0.0.1,.example.com" | |||
</syntaxhighlight>}} | |||
重启 docker 后检查是否生效。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
# 加载配置 | |||
systemctl daemon-reload | |||
# 重启docker | |||
systemctl restart docker | |||
# 查看代理配置是否生效 | |||
systemctl show --property=Environment docker | |||
</syntaxhighlight>|$|/home/kangkk}} | |||
构建过程中代理 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
docker build . \ | |||
--build-arg "HTTP_PROXY=http://192.168.51.34:808/" \ | |||
--build-arg "HTTPS_PROXY=http://192.168.51.34:808/" \ | |||
--build-arg "NO_PROXY=localhost,127.0.0.1,.example.com" \ | |||
-t your/image:tag</syntaxhighlight>|$|/home/kangkk}} | |||
创建容器过程中代理容器的网络环境。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
sudo docker run \ | |||
--restart=always \ | |||
--name=gogs \ | |||
--env HTTP_PROXY="http://192.168.51.34:808/" \ | |||
--env HTTPS_PROXY="http://192.168.51.34:808/" \ | |||
--env NO_PROXY="localhost,127.0.0.1,192.168.51.34" \ | |||
-p 10022:22 \ | |||
-p 10880:3000 \ | |||
-v /home/kangjian/gogs:/data \ | |||
-d gogs/gogs</syntaxhighlight>|$|/home/kangkk}} | |||
== 常见问题 == | == 常见问题 == | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/端口|端口]] || | | [[/端口|端口]] || [[复制]] || 示例 | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||