【docker】docker試煉日(2)搜尋與備份(Search and Backup)

#目的
當我們使用docker後需要使用一些特定環境中的鏡像,網路上已經有很多現成的鏡像提供我們使用與`搜尋`,
使用後我們可能會做很多的改變,且這個改變之後也還需要再`備份`使用


#搜尋

`KEYWORD`即你想要搜尋的關鍵字

```
docker search [OPTIONS] KEYWORD
```

假設我要跑一個magento,我可以先看線上有什麼樣的鏡可以使用,也有星星等級提供你選擇

```
P-MP15:magentodf puwu$ docker search magento
NAME                                         DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
alexcheng/magento2                           Docker image for Magento 2                      144                                     [OK]
alexcheng/magento                            Docker image for Magento                        103                                     [OK]
bitnami/magento                              Bitnami Docker Image for Magento                57                                      [OK]
magento/magento2devbox-web                   Official Web container with Magento instance    55                                      [OK]
mageinferno/magento2-php                     This image is built from the official php re…   40                                      [OK]
rafaelcgstz/magento2                         Docker Image to Magento 2 local development …   28                                      [OK]
mageinferno/magento2-nginx                   This image is built from nginx and contains …   26                                      [OK]
magento/magento-cloud-docker-php             Magento Cloud Docker - PHP                      19                                      [OK]
meanbee/magento                              A PHP container with Magento dependencies       18                                      [OK]
magento/magento-cloud-docker-nginx           Magento Cloud Docker - Nginx                    10                                      [OK]
markoshust/magento-php                       PHP images for Magento 1 and Magento 2          8                                       [OK]
magento/magento-cloud-docker-varnish         Magento Cloud Docker - Varnish                  7                                       [OK]
markoshust/magento-nginx                     Nginx image for Magento 1 and Magento 2         6                                       [OK]
meanbee/magento2-nginx                       A nginx container configured to run in tande…   6                                       [OK]
meanbee/magento2-php                         A PHP container, available as CLI or FPM, wi…   6                                       [OK]
magento/magento-cloud-docker-elasticsearch   Magento Cloud Docker - Elasticsearch            5                                       [OK]
fballiano/magento2-apache-php                PHP7 image specifically created for Magento2.   4                                       [OK]
magento/magento-cloud-docker-tls             Magento Cloud Docker - TLS termination proxy    3
pinedamg/magento                             Magento                                         0                                       [OK]
fballiano/magento2-cron                      Basic image pre configured for running magen…   0                                       [OK]
magespecialist/magento2-php                  A PHP-FPM 7.0 image customized for running M…   0
meanbee/magento2-data                        Magento 2 source code data images with bi-di…   0
xantek/magento2devbox-web                    Magento 2 Devbox Web updated for Magento 2.3…   0
bakerdist/magento2-php                       Base image for Magento 2                        0                                       [OK]
uqppa47/magento2                             Magento 2 Docker Images                         0
```

| Name, shorthand  |  Default | Description   |
|---               |---       |---            |
| --automated      |          |  Only show automated builds(僅顯示自動構建)      |   
| --filter , -f    |          |  Filter output based on conditions provided
(搜尋條件)      |   
| --limit          | 25       |  Max number of search results(顯示數量上限)   |
| --no-trunc       |          |  Don’t truncate output(當離開時是否刪除)  | 
| --stars , -s     |          |  Only displays with at least x stars(僅顯示至少評價大於X顆星)  |                  


以下範例為搜尋三顆星評價且結束時不刪除的magento 映像檔

```
docker search --filter=stars=3 --no-trunc magento 
```

挑選好以後就可以透過之前教學方式跑一個容器起來,以下提供完整進階的magento啟動指令(bitnami/magento)

```
docker run -d --name magento -p 80:80 -p 443:443 \
 -e MAGENTO_DATABASE_USER=bn_magento \
 -e MAGENTO_DATABASE_PASSWORD=your_password \
 -e MAGENTO_DATABASE_NAME=bitnami_magento \
 --net magento-tier \
 --volume magento_data:/bitnami \
 bitnami/magento:latest
```

#備份

當我們操作完一陣子後把所有預設環境都處理完成後,想做一個備份檔案時怎麼處理呢？
其實也很簡單,可以使用 docker export 命令

```
P-MP15:magento_bk puwu$ docker ps -l
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS                NAMES
4aa7e364306e        alexcheng/magento:1.8.1.0   "/sbin/my_init"     24 hours ago        Up 24 hours         0.0.0.0:80->80/tcp   magentodf_web_1
P-MP15:magento_bk puwu$ docker export 4aa7e364306e > magentobk200304.tar
P-MP15:magento_bk puwu$ ls -la
total 1705792
drwxr-xr-x  3 puwu  staff         96 Mar  4 18:44 .
drwxr-xr-x  7 puwu  staff        224 Mar  4 18:43 ..
-rw-r--r--  1 puwu  staff  866451456 Mar  4 18:44 magentobk200304.tar
```

之後我們可以將快照透過docker import導入為鏡像` puwutest/magento:v1 `

```
P-MP15:magento_bk puwu$ docker export 4aa7e364306e > magentobk200304.tar
P-MP15:magento_bk puwu$ ls
magentobk200304.tar
P-MP15:magento_bk puwu$ cat magentobk200304.tar | docker import - puwutest/magento:v1
sha256:c28dd8792d9f1f763eb780ef8e37d891fb2204fcaa4e5ae190af32cb71375837
P-MP15:magento_bk puwu$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
puwutest/magento           v1                  c28dd8792d9f        43 seconds ago      827MB
puwu/centos                6.7                 abc52a93fcd3        2 weeks ago         191MB
test/ubuntu                v1                  23e1a666432c        3 weeks ago         64.2MB
ubuntu                     18.04               ccc6e87d482b        6 weeks ago         64.2MB
ubuntu                     latest              ccc6e87d482b        6 weeks ago         64.2MB
phpmyadmin/phpmyadmin      latest              c24a75debb40        7 weeks ago         469MB
trafex/alpine-nginx-php7   latest              dc238a17e89e        3 months ago        110MB
centos                     6.7                 9f1de3c6ad53        11 months ago       191MB
hello-world                latest              fce289e99eb9        14 months ago       1.84kB
alexcheng/magento          1.8.1.0             a6ba5fd7a8e3        24 months ago       944MB
ubuntu                     15.10               9b9cb95443b5        3 years ago         137MB
training/webapp            latest              6fae60ef3446        4 years ago         349MB
mysql                      5.6.23              0bd9ef0792fa        4 years ago         283MB
```

如此就可以做一個簡單循環的簡單環境佈建操作了


參考資料：
https://www.runoob.com/docker/docker-container-usage.html
        https://docs.docker.com/engine/reference/commandline/search/