Docker:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第94行: 第94行:
{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
docker run -it --entrypoint sh <镜像名称></syntaxhighlight>|$|/home/kangkk}}
docker run -it --entrypoint sh <镜像名称></syntaxhighlight>|$|/home/kangkk}}
下载镜像中的文件到本地磁盘。
{{shc|<syntaxhighlight lang="bash">
docker cp ed8c1251c61c:/opt/app/src /Users/kangjian/Downloads</syntaxhighlight>|$|/home/kangkk}}


== 查看 ==
== 查看 ==

2022年6月7日 (二) 07:21的版本

docker

container name 示例 示例

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 示例 示例
示例 示例 示例
示例 示例 示例

参数

--no-cache

命令

if 示例 示例
示例 示例 示例
示例 示例 示例

安装

macos 示例 示例

docker-compose

安装docker-compose 示例 示例
示例 示例 示例
示例 示例 示例

制作镜像

nginxnode centos 示例
示例 示例 示例
示例 示例 示例

环境

区分环境 示例 示例
示例 示例 示例
示例 示例 示例

镜像

进入镜像。

/home/kangkk
docker run -it --entrypoint sh <镜像名称>

下载镜像中的文件到本地磁盘。

/home/kangkk
docker cp ed8c1251c61c:/opt/app/src /Users/kangjian/Downloads

查看

我开始查询当前容器。但是抛出 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

添加

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
mkdir -p 递归创建目录,如若上级目录不存在,按照目录层级自动创建目录。

添加配置文件。

/mysql_data/mysql56/conf/my.cnf
[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

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

参数:

  1. bridge:桥接 docker
  2. none:不配置网络
  3. host:和宿主机共享网络
  4. 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

参数:

  1. --driver bridge 表示使用桥接模式
  2. --subnet 192.168.1.0/16 表示子网ip,可以分配 192.168.1.2 到 192.168.255.255
  3. --gateway 192.168.1.0 表示网关
  4. 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

常见问题

端口 复制 示例
示例 示例 示例
示例 示例 示例