!!!!有鏡像才能建立容器!!!!!,記下來,要考的!!!
講容器操作指令之前先下載下傳一個centos的鏡像(這個指令在上篇《常用Docker鏡像操作指令》中有,不會的同學可以去看下)
//首先查一下centos
[root@iz2ze0lvzs71710m0jegmsz ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5627 [OK]
ansible/centos7-ansible Ansible on Centos7 125 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 114 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 99 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 63
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 57 [OK]
tutum/centos Simple CentOS docker image with SSH access 44
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 39
kinogmt/centos-ssh CentOS with SSH 29 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 26
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 10
guyton/centos6 From official centos6 container with full up… 9 [OK]
drecom/centos-ruby centos ruby 6 [OK]
centos/tools Docker image that has systems administration… 4 [OK]
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 3
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
miko2u/centos6 CentOS6 日本語環境 2 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 2
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 2
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
//選擇你要的鏡像和該鏡像版本(不寫版本預設是last),把他pull下來(我之前下過了)
[root@iz2ze0lvzs71710m0jegmsz ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
729ec3a6ada3: Already exists
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
//檢視一下本地的鏡像
[root@iz2ze0lvzs71710m0jegmsz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaowang/mytomcat 1.2 49733849ab9f 18 hours ago 507MB
mongo latest 191b28dbfefe 28 hours ago 363MB
tomcat latest 882487b8be1d 6 days ago 507MB
centos latest 0f3e07c0138f 3 weeks ago 220MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
一、建立并啟動容器的指令
文法及常用參數:
docker run [OPTIONS] IMAGE(可以是鏡像的image id 也可以是鏡像的REPOSITORY) [COMMAND]
OPTIONS說明(常用):有些是一個減号,有些是兩個減号
--name="容器新名字": 為容器指定一個名稱;
-d: 背景運作容器,并傳回容器ID,也即啟動守護式容器;
-i:以互動模式運作容器,通常與 -t 同時使;
-t:為容器重新配置設定一個僞輸入終端,通常與 -i 同時使用;
-P: 随機端口映射;
-p: 指定端口映射,有以下四種格式
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
舉個例子:
//啟動一個容器 這個容器裡面運作的是centos系統
#docker run -it centos
//使用鏡像centos:latest以互動模式啟動一個容器,在容器内執行/bin/bash指令。
#docker run -it centos /bin/bash
//使用鏡像tomcat:latest以互動模式啟動一個容器,主機端口7777,docker容器端口8080(這時候可以直接通路localhost:7777就可以進入tomcat首頁啦)
#docker run -it -p 7777:8080 tomcat
二、列出目前所有正在運作的容器
docker ps [OPTIONS]
OPTIONS說明(常用):
-a :列出目前所有正在運作的容器+曆史上運作過的
-l :顯示最近建立的容器。
-n:顯示最近n個建立的容器。
-q :靜默模式,隻顯示容器編号。
--no-trunc :不截斷輸出。
三、退出容器
兩種退出方式:
exit 容器停止并退出(自己操作一下:互動模式建立一個centos容器,然後輸入exit,再docker ps一下)
ctrl+p+q容器不停止退出(自己操作一下:互動模式建立一個centos容器,然後按ctrl+p+q,再docker ps一下)
四、啟動容器
docker start 容器ID或者容器名
五、重新開機容器
docker restart 容器ID或者容器名
六、停止容器
docker stop 容器ID或者容器名
七、強制停止容器
docker kill 容器ID或者容器名
八、删除已停止的容器
docker rm -f ${docker ps -a -q}
docker ps -a -q | xargs docker rm
重點内容:
1.啟動守護式容器:docker run -d 容器名
//使用鏡像centos:latest以背景模式啟動一個容器
#docker run -d centos
//然後docker ps -a 進行檢視, 會發現容器已經退出
#docker ps -a
是以總結一下:
很Docker容器背景運作,就必須有一個前台程序.
容器運作的指令如果不是那些一直挂起的指令(比如運作top,tail),就是會自動退出的。
這個是docker的機制問題,比如你的web容器,我們以nginx為例,正常情況下,我們配置啟動服務隻需要啟動響應的service即可。
例如service nginx start
但是,這樣做,nginx為背景程序模式運作,就導緻docker前台沒有運作的應用,
這樣的容器背景啟動後,會立即自殺因為他覺得他沒事可做了,
是以,最佳的解決方案是,将你要運作的程式以前台程序的形式運作。
2.檢視容器日志:docker logs -f -t --tail 容器ID
-t 是加入時間戳
-f 是随最新的日志列印
--tail 數字顯示最後多少條
3.檢視容器内運作的程序
docker top 容器ID

4.檢視容器内部細節
docker inspect 容器ID
5.進入正在運作的容器并以指令行互動(重點,記得敲一下)
docker exec -it 容器ID bashShell
[root@iz2ze0lvzs71710m0jegmsz ~]# docker exec -it 0a6da2eccfb4 ls -l /tmp
total 8
-rwx------ 1 root root 1379 Sep 27 17:13 ks-script-0n44nrd1
-rwx------ 1 root root 671 Sep 27 17:13 ks-script-w6m6m_20
重新進入: docker attach 容器ID
上述兩個的差別:attach--->直接進入容器啟動指令的終端。不會啟動新的程序
:exec---->是在容器中打開新的終端,并可以啟動新的程序
6.從容器内拷貝檔案到主機上
docker cp 容器ID:容器内路徑 目的主機路徑
[root@iz2ze0lvzs71710m0jegmsz ~]# docker cp 0a6da2eccfb4:/root/test.txt /root/o.txt