1. 程式人生 > >docker 的容器入門

docker 的容器入門

Linux Namespace

LXC所實現的隔離性主要是來自kernel的namespace, 其中pid, net, ipc, mnt, uts 等namespace將container的程序, 網路, 訊息, 檔案系統和hostname 隔離開。

Control Groups

cgroups 實現了對資源的配額和度量。 cgroups 的使用非常簡單,提供類似檔案的介面,在 /cgroup目錄下新建一個資料夾即可新建一個group,在此資料夾中新建task檔案,並將pid寫入該檔案,即可實現對該程序的資源控制。具體的資源配置選項可以在該資料夾中新建子 subsystem ,{子系統字首}.{資源項} 是典型的配置方法。 安裝docker
[[email protected] ~]# yum -y install epel-release
[[email protected] ~]# yum -y install docker
[[email protected] ~]# systemctl start docker
[[email protected] ~]# docker images   檢視存在的映象
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[
[email protected]
~]# docker pull tomcat 下載映象 Using default tag: latest Trying to pull repository docker.io/library/tomcat ... latest: Pulling from docker.io/library/tomcat 54f7e8ac135a: Pull complete d6341e30912f: Pull complete 087a57faf949: Pull complete 95065f220961: Pull complete 0887630ce576: Pull complete c375d1959fab: Pull complete e00a5e6055cc: Pull complete 8319f5fb56cf: Pull complete 258c74eb25ab: Pull complete 6eb86d11d371: Pull complete adb5a4a6adf5: Pull complete 4685b434f297: Pull complete Digest: sha256:e394c2f94eee344300e68d7183f3f19d272316f9691b280ac0e3911ea610e590 Status: Downloaded newer image for docker.io/tomcat:latest [
[email protected]
~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/tomcat latest 78b258e36eed 13 days ago 463 MB
[[email protected] ~]# docker run -it -d -p 8888:8080 tomcat:latest 後臺啟動容器將容器的8080與本機的8888埠進行繫結 d588baa6a123a47913a112a5c3ee3f52f0a9f2c2c412ec1a46a527f32222ab6a

前端訪問http://192.168.10.31:8888/

 

檢視執行容器

[[email protected] ~]# ss -lntp
State       Recv-Q Send-Q                                                  Local Address:Port                                                                 Peer Address:Port              
LISTEN      0      128                                                                 *:22                                                                              *:*                   
users:(("sshd",pid=1000,fd=3))LISTEN      0      100                                                         127.0.0.1:25                                                                              *:*                   
users:(("master",pid=1159,fd=13))LISTEN      0      128                                                                :::22                                                                             :::*                   
users:(("sshd",pid=1000,fd=4))LISTEN      0      128                                                                :::8888                                                                           :::*                   
users:(("docker-proxy-cu",pid=2762,fd=4))LISTEN      0      100                                                               ::1:25                                                                             :::*                   
users:(("master",pid=1159,fd=14))
[[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 967a88f8ea47 tomcat:latest "catalina.sh run" 3 minutes ago Up 3 minutes 0.0.0.0:8888->8080/tcp wonderful_swanson

停止容器停止

[[email protected] ~]# docker stop 967a88f8ea47 停止容器
967a88f8ea47
[[email protected] ~]# docker ps  檢視執行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[[email protected] ~]# docker ps -a  檢視所有容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
967a88f8ea47        tomcat:latest       "catalina.sh run"   7 minutes ago       Exited (143) 7 seconds ago                       wonderful_swanson