2019-03-08

Docker 基本操作指令

markdown # 映像檔(Image) ## 從搜尋遠端倉庫中公開的映像檔 search ``` $ docker search ubuntu $ docker search --automated=true -s 3 ubuntu Flag --automated has been deprecated, use --filter=is-automated=true instead Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 275 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 205 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 158 [OK] ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 96 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 49 [OK] nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK] 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK] eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 8 [OK] codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 5 [OK] darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK] ``` ## 取得映像檔 pull ``` $ docker pull ubuntu $ docker pull ubuntu:latest $ docker pull ubuntu:18.04 ``` ## 查看映像檔訊息 images ``` $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 18.04 47b19964fb50 4 weeks ago 88.1MB ubuntu latest 47b19964fb50 4 weeks ago 88.1MB ``` ## 增加映像檔標籤 tag ``` $ docker tag ubuntu:latest newtagubuntu:latest $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 18.04 47b19964fb50 4 weeks ago 88.1MB ubuntu latest 47b19964fb50 4 weeks ago 88.1MB newtagubuntu latest 47b19964fb50 4 weeks ago 88.1MB ``` ## 查看詳細資訊 inspect ``` $ docker inspect ubuntu:18.04 [ { "Id": "sha256:47b19964fb500f3158ae57f20d16d8784cc4af37c52c49d3b4f5bc5eede49541", "RepoTags": [ "ubuntu:18.04", "ubuntu:latest" ], "RepoDigests": [ "ubuntu@sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210" ], "Parent": "", "Comment": "", "Created": "2019-02-06T03:37:51.599085524Z", "Container": "4fcabd24a4d0668c251f05105af1d986b1841713e7af90bf51df172ecefae2fa", "ContainerConfig": { "Hostname": "4fcabd24a4d0", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"/bin/bash\"]" ], "ArgsEscaped": true, "Image": "sha256:5bbf4643e91ac20eb0ec60930e6d56e36eedab0ae09930a4bd414bb227590d28", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "DockerVersion": "18.06.1-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "ArgsEscaped": true, "Image": "sha256:5bbf4643e91ac20eb0ec60930e6d56e36eedab0ae09930a4bd414bb227590d28", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 88140808, "VirtualSize": 88140808, "GraphDriver": { "Data": null, "Name": "aufs" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:bebe7ce6215aee349bee5d67222abeb5c5a834bbeaa2f2f5d05363d9fd68db41", "sha256:283fb404ea9415ab48456fd8a82b153b1a719491cdf7b806d1853b047d00f27f", "sha256:663e8522d78b5b767f15b2e43885da5975068e3195bbbfa8fc3a082297a361c1", "sha256:4b7d93055d8781d27259ba5780938e6a78d8ef691c94ee9abc3616c1b009ec4a" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ] ``` ## 查看建置歷史 history ``` $ docker history ubuntu:18.04 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.38/images/ubuntu:18.04/history: dial unix /var/run/docker.sock: connect: permission denied alger@ocr:~$ sudo docker history ubuntu:18.04 IMAGE CREATED CREATED BY SIZE COMMENT 47b19964fb50 4 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B 4 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B 4 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B 4 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B 4 weeks ago /bin/sh -c #(nop) ADD file:529264c6593975a61… 88.1MB ``` ## 刪除映像檔 rmi ``` $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 18.04 47b19964fb50 4 weeks ago 88.1MB ubuntu latest 47b19964fb50 4 weeks ago 88.1MB $ docker rmi ubuntu:latest $ docker rmi 47b19964fb50 ``` ## 建立映像檔 commit ### 1. 從現有映像檔建立 步驟是先啟動一個映像檔,在其中進行修改操作,然後退出;再來使用 commit 命令建立一個新映像檔。 ``` $ sudo docker run -it ubuntu:18.04 /bin/bash root@f4aa7204185f:/# cd ~ root@f4aa7204185f:~# ls root@f4aa7204185f:~# touch test root@f4aa7204185f:~# exit exit $ sudo docker commit -m "Added a new file" -a "cww" f4aa7204185f test:0.1 sha256:15917e0e189ccb879ef19989046f132b2ddadd1ddb0674ce397baafe6951e9d7 ``` commit 的選項: * -a, --author="": 作者資訊 * -c, --change=[]:commit 的時候執行 Dockerfile 指令,包括 CMD|ENTRYPOINT|ENV|EXPOSE|LABEL|ONBUILD|USER|VOLUME|WORKDIR 等 * -m, --message="":新增訊息 * -p, --pause=true:commit 時暫停容器運作 ### 2. 基於 Linux 容器 (LXC) 範例匯入 docker import [OPTIONS] file|URL|-[REPOSITORY[:tag]] 使用 OpenVZ 提供的範本來建立,OpenVZ 的範本下載位址 http://openvz.org/Download/templates/precreated ``` $ cat ubuntu-18.04-x86_64-minimal.tar.gz | docker import - ubuntu:18.04 或 $ docker import http://download.openvz.org/template/precreated/ubuntu-18.04-x86_64-minimal.tar.gz ubuntu:18.04 ``` 然後查看新匯入的映像檔,會看到已存在本地端了。 ``` $ docker images ``` ## 儲存和載入映像檔 save & load 本機上的 ubuntu:18.04 映像檔儲存為 ubuntu_18.04.tar 檔案 ``` $ docker save -o ubuntu_18.04.tar ubuntu:18.04 ``` 載入映像檔,將儲存的 tar 檔案匯入本機的映像檔中 ``` $ docker load --input ubuntu_18.04.tar 或 $ docker load < ubuntu_18.04.tar ``` > 可以透過 ssh 將映像檔傳送到遠端伺服器 >$ sudo docker save ubuntu:latest | ssh user@remote-server sudo docker load ## 上傳映像檔 push 指令語法 > docker push NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG] ### dockerhub 先在[docker hub](https://cloud.docker.com/)註冊帳號,我這裡注冊的名稱是 `alger23`,然後用命令方式登入 ``` $ docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: alger23 Password: Login Succeeded ``` 然後透過以下命令上傳映像檔到 docker hub 倉庫中 可先在 docker hub 網頁操作建立 Repository 後上傳到倉庫。 也可以不在網頁上建立倉庫,直接上傳。這裡先把要上傳的 test:映像檔建立新的 tag 名稱 `alger23/test:0.1`,然後再將用 push 指令上傳到倉庫。 ``` $ docker tag test:0.1 alger23/test:0.1 $ docker push alger23/test:0.1 ``` # 容器 (Container) ## 查看容器 ps 查看本機上存在的所有容器 ``` $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4aa7204185f ubuntu:18.04 "/bin/bash" 2 hours ago Exited (0) 2 hours ago epic_keller ``` ``` $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4aa7204185f ubuntu:18.04 "/bin/bash" 2 hours ago Exited (0) 2 hours ago epic_keller ``` ## 建立容器 create ``` $ docker create -it ubuntu:18.04 192aa1379e21446c1b2668fdb32bca24fef0dee2d847ed2afe84231cede38559 ``` 使用 docker create 命令建立的容器處於停止狀態,可以使用 docker start 指令來啟動它。 ``` $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 192aa1379e21 ubuntu:18.04 "/bin/bash" 11 seconds ago Created infallible_bohr $ docker start 192aa1379e21 192aa1379e21 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 192aa1379e21 ubuntu:18.04 "/bin/bash" About a minute ago Up 55 seconds infallible_bohr ``` ## 建立並啟動容器 run 利用 Image ubuntu:18.04 執行文字輸出 ``` $ docker run ubuntu:18.04 echo 'hello!' hello! ``` 添加 -d 參數讓容易背景執行 ``` $ docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" 264a8dce7deb5de83dcd3f198b56d1cba169988e6f1c214f43020633261e271c ``` 要查看容易的輸出資訊 ``` $ docker log 264a8dce7 hello world hello world ... ``` ## 停止容器 stop ``` $ docker stop 264a8dce7 ``` ## 重啟容器 restart ``` $ docker restart 264a8dce7 ``` ## 進入容器進行操作 ### 1. attach > 指令格式 > docker attach [--detach-keys[=[]]] [--no-stdin] [--sig-proxy[=true]] CONTAINER 使用範例 ``` $ sudo docker run -itd ubuntu b210e0160499a48494c0f0e04b5b927cd62477c2122ff66121ef3b64d0776b99 $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b210e0160499 ubuntu "/bin/bash" 6 seconds ago Up 4 seconds condescending_payne $ sudo docker attach condescending_payne root@b210e0160499:/# ``` 使用 attach 指令有時候並不方便,當多個視窗同時用 attach 到同一個容器時,所有視窗都會同步顯示。當某個視窗因命令雍塞時,其它視窗也無法執行操作。 ### 2. exec Docker 1.3.0 版起提供 exec 指令,可以在容器內直接執行任意命令 > 指令格式 > docker exec [-d|--detach] [--detach-keys[=[]]] [-i|--interactive] [--privileged] [-t|--tty] [-u|--user[=USER]] CONTAINER COMMAND [ARG...] 例如 ``` $ docker exec -it b210 /bin/bash root@b210e0160499:/# exit ``` ### 3. nsenter 在 util-linux 套件版本 2.23 之後包含 nsenter 工具。 要使用 nsenter 連接到容器,需要容器程序的 PID,透過以下指令取得: > PID=$(docker inspect --format "{{ .State.Pid }}" ) 完整範例 ``` $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b210e0160499 ubuntu "/bin/bash" 28 minutes ago Up 18 minutes condescending_payne $ docker inspect --format "{{ .State.Pid }}" b210e0160499 18903 $ nsenter --target 18903 --mount --uts --ipc --net --pid root@b210e0160499:/# ``` 查看使用者和程序資訊 ``` root@b210e0160499:/# w 07:04:48 up 20:35, 0 users, load average: 0.01, 0.01, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root@b210e0160499:/# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 06:44 pts/0 00:00:00 /bin/bash root 26 0 0 07:03 pts/0 00:00:00 -bash root 32 26 0 07:05 pts/0 00:00:00 ps -ef ``` ## 移除容器 > docker rm [-f|--force] [-l|--link] [-v|--volumes] CONTAINER [CONTAINER...] * -f, --force=false: 是否強制停止並刪除執行中的容器 * -l, --link=false: 刪除容器連接,但保留容器 * -v, --volumes=false: 刪除容器掛載的 volume ``` $ docker container rm f4aa7204185f f4aa7204185f 或 $ docker rm f4aa7204185f f4aa7204185f ``` ## 匯入和匯出容器 import & export 要將容器搬遷至另一個系統時可以使用Docker 的匯出和匯入功能。 ### 1. 匯出容器 export > docker export [-o|--output[=""]] CONTAINER 範例 ``` $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b210e0160499 ubuntu "/bin/bash" 41 minutes ago Up 31 minutes condescending_payne $ docker export -o test.tar b210 或 $ docker export b210 > test.tar ``` ### 2. 匯入容器 import > docker import [-c|--change[=[]]] [-m|--message[=MESSAGE]] file|URL|-[REPOSITORY[:TAG]] ``` $ cat test.tar | docker import - test/ubuntu:123 ``` # 倉庫 (Repository) ## 搜尋 ``` $ docker search ubuntu ```

沒有留言:

adsense