Docker:修订间差异
| (未显示同一用户的16个中间版本) | |||
| 第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" | ||
| 第4行: | 第36行: | ||
| [[/container_name|container name]] || 示例 || 示例 | | [[/container_name|container name]] || 示例 || 示例 | ||
|} | |} | ||
== 权限 == | == 权限 == | ||
查看权限 | 查看权限 | ||
| 第129行: | 第162行: | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
|} | |} | ||
| 第175行: | 第202行: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[/nginxnode|nginxnode]]|| [[/centos|centos]] || | | [[/nginxnode|nginxnode]]|| [[/centos|centos]] || [[/yapi|yapi]] | ||
|- | |- | ||
| 示例 || 示例 || 示例 | | 示例 || 示例 || 示例 | ||
| 第491行: | 第518行: | ||
sudo docker exec -it mysql-56 /bin/bash</syntaxhighlight>|$|/home/kangkk}} | 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" | ||
| 第508行: | 第538行: | ||
* Back-off restarting failed container | * Back-off restarting failed container | ||
: 实例事件中一直出现Back-off restarting failed container的Warning事件,一般是由于通过指定的镜像启动容器后,容器内部没有常驻进程,导致容器启动成功后即退出,从而进行了持续的重启。 | : 实例事件中一直出现Back-off restarting failed container的Warning事件,一般是由于通过指定的镜像启动容器后,容器内部没有常驻进程,导致容器启动成功后即退出,从而进行了持续的重启。 | ||
== 日志 == | == 日志 == | ||
{{Shc_out|<syntaxhighlight lang="bash"> | {{Shc_out|<syntaxhighlight lang="bash"> | ||
| 第686行: | 第717行: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
| [[编程语言/Drone]] | | [[编程语言/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}} | |||
== 常见问题 == | == 常见问题 == | ||
2025年4月13日 (日) 19:32的最新版本
安装
| macos | 示例 | 示例 |
Ubuntu
/home/kangkk
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;
网传另一种使用阿里源的方法,这里不做推荐,仅供参考
/home/kangkk
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"
Compose
/home/kangkk
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
/home/kangkk
docker-compose --version
源
docker
| container name | 示例 | 示例 |
权限
查看权限
[jian@kang /home/kangkk]$
ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 Nov 21 15:42 /var/run/docker.sock
从 docker.sock 文件的状态信息中提取所有者的组 ID
[jian@kang /home/kangkk]$
stat -c '%g' /var/run/docker.sock
992
添加当前用户到 docker 组。
sudo groupadd docker
sudo usermod -aG docker ${USER}
sudo su
su kangjian
/home/kangkk
gpasswd -a $USER docker
docker.sock
如果 docker.sock 没有生效,可能会输出如下信息。
[jian@kang /home/kangkk]$
curl -s --unix-socket /var/run/docker.sock http:/containers/json | jq .
{
"message": "page not found"
}
正常信息如下
[
{
"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"
}
]
}
]
参数
--data-root
设置镜像存储路径。
dockerfile
| dockerfile |
常见
| 多指令 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
login
/home/kangkk
docker login -u username -p password
[jian@kang /home/kangkk]$
echo 'D&u******&a7i' | docker login https://index.docker.io -u kangkk --password-stdin
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/
应用
| elasticsearch | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
数据卷
指定目录挂载。
docker run -it -v 主机目录:容器内目录
匿名挂载。
docker run -it -v 容器内目录 # 主机目录默认为 /var/lib/docker/volumes/<id>/_data
具名挂载。
docker run -it -v 自定义卷名:容器内目录 # 主机目录默认为 /var/lib/docker/volumes/<id>/_data
参数
--no-cache
命令
| if | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
docker-compose
| 安装docker-compose | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
镜像
镜像列表
[jian@kang /home/kangkk]$
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node lts-alpine b1ca7421d2e7 2 days ago 112MB
alpine 3.13.5 6dbb9cc54074 16 months ago 5.61MB
镜像重命名
/home/kangkk
docker image tag node:lts-alpine kangkk.com/node:lts-alpine
# or
docker image tag b1ca7421d2e7 kangkk.com/node:lts-alpine
再次查看镜像列表就可以看到新增的重命名之后的镜像了。
[jian@kang /home/kangkk]$
docker images
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
制作镜像
| nginxnode | centos | yapi |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
导出镜像
/home/kangkk
docker save -o /root/images/image_name.tar image_name:latest
导入镜像
/home/kangkk
docker load < image_name.tar
##
docker load –i image_name.tar
##
docker load –input image_name.tar
环境
| 区分环境 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
镜像
进入镜像。
/home/kangkk
docker run -it --entrypoint sh <镜像名称>
并挂载镜像地址(--privileged 设置 root 权限)
/home/kangkk
docker run -it --privileged -v <本地目录绝对路径>:<docker路径> --entrypoint sh <镜像名称>
开启一些服务会遇到 Failed to connect to bus: Host is down 错误。改为如下:
/home/kangkk
docker run -itd --privileged -v <本地目录绝对路径>:<docker路径> --name <自定义服务名称> <镜像名称> /usr/sbin/init \
docker exec -it <自定义服务名称> /bin/bash
下载镜像中的文件到本地磁盘。
/home/kangkk
docker cp <容器id>:<容器内地址> <本地地址>
示例。
/home/kangkk
docker cp ed8c1251c61c:/opt/app/src /Users/kangjian/Downloads
重新打包
/home/kangkk
docker commit -m "build nginx image" -a "kangkk" 5a9aa788a91e qnpm-nginx
在重新打包的过程中,需要保持容器运行状态?
运行容器
/home/kangkk
docker run -it d66f22a3c894 -i /bin/bash
另起 tab 进入容器
/home/kangkk
docker exec -it 38de0181d5b4 bash
最后 commit
查看
我开始查询当前容器。但是抛出 docker 并未运行。
[jian@kang /home/kangkk]$
docker container ls -all
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
启动 docker 服务。
[jian@kang /home/kangkk]$
sudo systemctl start docker.service
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:
再次查看 docker 的状态。
[jian@kang /home/kangkk]$
systemctl status docker.service
● 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
再次查询,这个时候提示需要 root 权限。
[jian@kang /home/kangkk]$
docker container ls -all
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
[jian@kang /home/kangkk]$
sudo docker container ls -all
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
删除当前容器
/home/kangkk
docker container rm 容器名
进入容器
/home/kangkk
docker exec -it elasticsearch /bin/bash
查看容器日志地址
/home/kangkk
sudo docker inspect --format '{{.LogPath}}' 5f3d48017cd3
启动
启动全部
[jian@kang /home/kangkk]$
sudo docker start $(sudo docker ps -a | awk '{ print $1}' | tail -n +2)
34b847559496
1aa50bf020ef
关闭
关闭全部
[jian@kang /home/kangkk]$
sudo docker stop $(sudo docker ps -a | awk '{ print $1}' | tail -n +2)
34b847559496
1aa50bf020ef
搜索
docker search [option] keyword
添加镜像之前,我需要先查找镜像,如我现在需要安装 mysql。
[jian@kang /home/kangkk]$
sudo docker search mysql
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
可以通过子命令进行过滤,比如只搜索查看官方镜像。
[jian@kang /home/kangkk]$
sudo docker search --filter=is-official=true mysql
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]
搜索历史版本。
[jian@kang /home/kangkk]$
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)}}'
% 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
[jian@kang /home/kangkk]$
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)}}'
% 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
添加
docker pull [options] name[:tag|@digest]
我需要先拉取镜像。
[jian@kang /home/kangkk]$
sudo docker pull mysql:5.6
[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
查看已经下载到本地的镜像。
[jian@kang /home/kangkk]$
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0 562c9bc24a08 4 days ago 521MB
mysql 5.6 dd3b2a5dcb48 3 months ago 303MB
删除
删除镜像
/home/kangkk
docker rmi [image]
/home/kangkk
docker image rm [image]
- -f, -force: 强制删除镜像,即便有容器引用该镜像;
- -no-prune: 不要删除未带标签的父镜像;
删除所有镜像。
/home/kangkk
sudo docker rmi $(sudo docker images | awk '{print $3}' |tail -n +2)
删除容器
删除所有容器。
/home/kangkk
sudo docker rm $(sudo docker ps -a | awk '{ print $1}' | tail -n +2)
宿主机创建数据目录和配置文件
/home/kangkk
sudo mkdir -p /mysql_data/mysql56/data /mysql_data/mysql56/log /mysql_data/mysql56/conf
sudo chmod -R 755 /mysql_data
添加配置文件。
[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/
启动创建容器。
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
[jian@kang /home/kangkk]$
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
1aa50bf020ef42ff18d4da3076aea89c5d5f4950d8b6ccde9d9ac1c03b329f9f
进入容器。
/home/kangkk
sudo docker exec -it mysql-56 /bin/bash
清理
/home/kangkk
docker system prune
error
| 端口冲突 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |
- Error response from daemon: manifest for elasticsearch:latest not found: manifest unknown: manifest unknown
- 需要指定版本号
/home/kangkk
sudo docker pull elasticsearch:7.6.2
- 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事件,一般是由于通过指定的镜像启动容器后,容器内部没有常驻进程,导致容器启动成功后即退出,从而进行了持续的重启。
日志
[jian@kang /home/kangkk]$
sudo docker container ls -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8edadee2b3f8 mysql:8.0 "docker-entrypoint..." 8 minutes ago Exited (1) 8 minutes ago mysql-80
查看容器启动日志。
[jian@kang /home/kangkk]$
sudo docker logs 8edadee2b3f8
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.
inspect
[jian@kang /home/kangkk]$
sudo docker inspect elasticsearch
[
{
"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
}
}
}
}
]
IPAddress
[jian@kang /home/kangkk]$
sudo docker inspect elasticsearch | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
网络
查看网络状态。
[jian@kang /home/kangkk]$
sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
2423ab85dd92 bridge bridge local
d05fc848b239 host host local
9213fe5ad3df none null local
参数:
- bridge:桥接 docker
- none:不配置网络
- host:和宿主机共享网络
- container:容器网络连通
bridge network
桥接网络
overlay network
MACVLAN network
创建自定义网络
[jian@kang /home/kangkk]$
sudo docker network create --driver bridge --subnet 192.168.1.0/16 --gateway 192.168.1.0 kangkknet
1087cad22931daafb73b814cb28f531f57889ce5fbd85061cbc3ada7cf729272
参数:
- --driver bridge 表示使用桥接模式
- --subnet 192.168.1.0/16 表示子网ip,可以分配 192.168.1.2 到 192.168.255.255
- --gateway 192.168.1.0 表示网关
- kangkknet 表示网络名
检查添加状态。
[jian@kang /home/kangkk]$
sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
2423ab85dd92 bridge bridge local
d05fc848b239 host host local
1087cad22931 kangkknet bridge local
9213fe5ad3df none null local
网络相关信息。
[jian@kang /home/kangkk]$
sudo docker network inspect kangkknet
[
{
"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": {}
}
]
存储空间映射
| 应用 | 配置文件 | 数据 | 日志 | 参数 |
|---|---|---|---|---|
| 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 |
文件
从复制容器内的文件到外面。
/home/kangkk
docker cp f6bbd9850c15:/usr/src/app /home/kangjian/data
构建
| 编程语言/Drone |
Volume
类型
- host volume:在宿主机上,可从容器内访问
- named volume:docker 管理 volume 位置,存在命名的名称
- anonymous volume:docker 管理 volume 位置,不存在命名的名称
type
bind
volume
tmpfs
如果是存储容器生成的非持久性状态数据,可以使用 tmpfs,在容器运行的整个生命周期内数据是有效的,可以避免写入容器的可写层来提高容器的性能,当容器重启后数据会消失。
创建卷
/home/kangkk
sudo docker volume create vol
tmpfs 卷
/home/kangkk
docker volume create -d local -o type=tmpfs -o device=tmpfs -o o=size=20480m,uid=1000 --name verdaccio_storage
查看卷列表
/home/kangkk
sudo docker volume ls
属性 描述 Driver 卷驱动 Labels 标签 Mountpoint 宿主机挂载点 Name 名称 Options 挂载参数 Scope 作用域
使用格式化参数过滤信息
/home/kangkk
sudo docker volume ls --format "{{.Name}}: {{.Driver}}"
卷详细信息
/home/kangkk
sudo docker volume inspect vol
删除卷
/home/kangkk
sudo docker volume rm vol
删除所有未在使用中的卷
/home/kangkk
sudo docker volume prune
代理
源于广义上存在的墙(或者两侧墙),Docker 非官方源不完成的情况下,只好使用代理进行镜像拉取。
编辑 Docker 源,设置为官方源。
{
"registry-mirrors": ["https://hub.docker.com/"]
}
编辑代理文件。
[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"
重启 docker 后检查是否生效。
/home/kangkk
# 加载配置
systemctl daemon-reload
# 重启docker
systemctl restart docker
# 查看代理配置是否生效
systemctl show --property=Environment docker
构建过程中代理
/home/kangkk
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
创建容器过程中代理容器的网络环境。
/home/kangkk
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常见问题
| 端口 | 复制 | 示例 |
| 示例 | 示例 | 示例 |
| 示例 | 示例 | 示例 |