歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> docker-3 基礎命令,docker-3命令

docker-3 基礎命令,docker-3命令

日期:2017/3/3 18:09:36   编辑:學習Linux

docker-3 基礎命令,docker-3命令

docker-3 基礎命令,docker-3命令


創建鏡像

創建鏡像的方法有三種:

  • 基於已有的容器創建
  • 基於本地模板導入
  • 基於dockerfile

基於已有的容器創建

主要使用docker commit 命令,命令格式:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:tag]],主要包括:

-a ,--author="" 作者信息

-m,--message=""提交消息

-p,--pause=true 提交時暫停容器

例如:

# docker run -it centos /bin/bash
[root@d7e7ac1cbca2 /]# touch test

[root@d7e7ac1cbca2 /]# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test tmp usr var

# docker commit -m "add a file" -a "kafeikele" de6 centos_copy
5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB

基於本地模板導入

推薦使用openVZ提供的模板來創建

https://openvz.org/Download/templates/precreated

#cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import - centos:latest

存出和導入鏡像

# docker images

centos 7.1.1503 47a77536ad4c 8 weeks ago 212.1 MB

# docker save -o centos_7.1.tar centos:7.1.1503

# docker load --input centos_7.1.tar

# docker load < centos_7.1.tar

基於dockerfile

之後的內容詳細介紹

運行第一個docker容器

# docker run centos echo "hello world"
Unable to find image 'centos:latest' locally
latest: Pulling from centos
47d44cb6f252: Pull complete
168a69b62202: Pull complete
812e9d9d677f: Pull complete
4234bfdd88f8: Pull complete
ce20c473cd8a: Pull complete
centos:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide 
security.
Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5
Status: Downloaded newer image for centos:latest
hello world

命令說明:

docker run :標准容器啟動命令

centos: 鏡像名稱,默認是latest

echo和後面的內容:容器啟動後執行的命令

啟動一個交互式容器

docker run -it centos /bin/bash

*注:-t標示在心容器內指定一個偽終端或終端,-i標示允許我們對容器內的STDIN進行交互

以服務方式啟動一個docker容器

如果你實際測試,估計也發現了,第一個“hello world”容器啟動後執行完echo命令就退出了,而第二個交互式的容器,只要用戶退出當前容器的bash,容器也退出了。這明顯不能滿足一個服務長時間運行的要求,好找docker run提供了‘-d’參數,可以實現將容器以守護進程方式啟動。

docker  run -d  centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; 
179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33

這個長的字符串叫做容器ID。它是容器的唯一標識,所以我們可以使用它來操作容器,比如查看日志、停止或刪除容器等。

dock logs 179fc7f17c358834364d

而為什麼使用一個死循環來輸出呢?

因為如果不是死循環,一次輸出後,容器中的進程就結束了。容器的唯一進程都結束了,容器就停止了。因此如果要在容器中運行具體的服務,這項服務本身在容器中也必須是已守護進程方式運行的。

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
主要選項:
-d : 以後台進行方式運行容器
-t : 提供一個偽終端
-i : 提供交互輸入,一般與“-t”一起使用,如果只提供“-i”選項,則容器啟動後是無法退出的
-v : 映射一個volume給容器,如: -p /data/www:/var/www/html
-p : 將容器的端口映射給宿主機,如: -p 8080:80

  

更多命令操作

# docker images 列出本地所有鏡像

# docker search centos  從默認鏡像倉庫搜索鏡像

NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2767 [OK] 
ansible/centos7-ansible Ansible on Centos7 90 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 42 [OK]
jdeathe/centos-ssh-apache-php CentOS-6 6.8 x86_64 - Apache / PHP / PHP M... 21 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 17 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 14 [OK]

#docker pull centos  下載鏡像到本地

#docker create -it  ubuntu:latest   創建一個容器

Unable to find image 'ubuntu:latest' locally
latest: Pulling from ubuntu
58488e45273c: Pull complete 
25810b66099e: Pull complete 
6571ba684f54: Pull complete 
6ed49a73d8f0: Pull complete 
c53777cbfc31: Pull complete 
56465e1e45d2: Pull complete 
Digest: sha256:312986132029d622ae65423ca25d3a3cf4510de25c47b05b6819d61e2e2b5420
Status: Downloaded newer image for ubuntu:latest
1330233e50aba7fca99e5914bd28dd89321bc86ec35fb36b4775d3424337c190

docker create 命令創建的容器處於停止狀態,需要用docker start 把它啟動

# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1330233e50ab ubuntu:latest "/bin/bash" About a minute ago happy_engelbart

docker run 命令等價於先執行docker create命令, 然後再執行docker start 命令

# docker run ubuntu /bin/echo "hello world"
hello world

 進入容器

方法一:

# docker attach a54615a88787 後面跟的是容器名或者id,退出後docker容器也退出,不常用

方法二:

# docker exec -it a54615a88787 /bin/bash 跟的是容器名或者id

方法三:

yum -y install util-linux

# docker inspect --format "{{.State.Pid}}" stupefied_cray 最後面跟的是容器的名稱
4899
# nsenter --target 4899 --mount --uts --ipc --net --pid

腳本

#!/bin/bash

CNAME=$1

CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME)

nsenter --target $CPID --mount --uts --ipc --net –pid

 2016-10-28 01:00:26 

  

http://xxxxxx/Linuxjc/1168791.html TechArticle

Copyright © Linux教程網 All Rights Reserved