Docker是一個用了一種新穎方式實現的超輕量虛擬機,在實現的原理和應用上還是和VM有巨大差別,專業的叫法是應用容器(Application Container)。(我個人還是喜歡稱虛擬機)
1. 安裝
1.1 在 Ubuntu 14.04 上安裝 Docker
前提要求:
內核版本必須是3.10或者以上
依次執行下面的步驟:
sudo apt-get updatesudo apt-get install apt-transport-https ca-certificatessudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D編輯 /etc/apt/sources.list.d/docker.list 文件,添加 deb https://apt.dockerproject.org/repo ubuntu-trusty mainsudo apt-get updatesudo apt-get purge lxc-dockerapt-cache policy docker-engineapt-get upgradesudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtualsudo apt-get install docker-engine
至此,安裝過程完成。
運行 sudo service docker start 啟動 Docker 守護進程。
運行 docker version 查看 Docker 版本
root@devstack:/home/sammy# docker --versionDocker version 1.12.1, build 23cf638
啟動第一個容器:
啟動第一個Docker 容器 docker run hello-world
root@devstack:/home/sammy# docker run hello-worldHello from Docker!This message shows that your installation appears to be working correctly.
它的運行成功也表明前面的安裝步驟都運行正確了。
以上內容參考自 Docker 官網:https://docs.docker.com/engine/installation/linux/ubuntulinux/
1.2 Docker 到目前(2016/09/16)為止的版本歷史
2. Docker 的基本操作
2.1 Docker 容器的狀態機
一個容器在某個時刻可能處于以下幾種狀態之一:
created:已經被創建 (使用 docker ps -a 命令可以列出)但是還沒有被啟動 (使用 docker ps 命令還無法列出) running:運行中 paused:容器的進程被暫停了 restarting:容器的進程正在重啟過程中 exited:上圖中的 stopped 狀態,表示容器之前運行過但是現在處于停止狀態(要區別于 created 狀態,它是指一個新創出的尚未運行過的容器)。可以通過 start 命令使其重新進入 running 狀態 destroyed:容器被刪除了,再也不存在了你可以在 docker inspect 命令的輸出中查看其詳細狀態:
"State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 4597, "ExitCode": 0, "Error": "", "StartedAt": "2016-09-16T08:09:34.53403504Z", "FinishedAt": "2016-09-16T08:06:44.365106765Z" }2.2 Docker 命令概述
我們可以把Docker 的命令大概地分類如下:
鏡像操作: build Build an image from a Dockerfile commit Create a new image from a container's changes images List images load Load an image from a tar archive or STDIN pull Pull an image or a repository from a registry push Push an image or a repository to a registry rmi Remove one or more images search Search the Docker Hub for images tag Tag an image into a repository save Save one or more images to a tar archive (streamed to STDOUT by default) history 顯示某鏡像的歷史 inspect 獲取鏡像的詳細信息 容器及其中應用的生命周期操作: create Create a new container (創建一個容器) kill Kill one or more running containers inspect Return low-level information on a container, image or task pause Pause all processes within one or more containers ps List containers rm Remove one or more containers (刪除一個或者多個容器) rename Rename a container restart Restart a container run Run a command in a new container (創建并啟動一個容器) start Start one or more stopped containers (啟動一個處于停止狀態的容器) stats Display a live stream of container(s) resource usage statistics (顯示容器實時的資源消耗信息) stop Stop one or more running containers (停止一個處于運行狀態的容器) top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until a container stops, then print its exit code attach Attach to a running container exec Run a command in a running container port List port mappings or a specific mapping for the container logs 獲取容器的日志 容器文件系統操作: cp Copy files/folders between a container and the local filesystem diff Inspect changes on a container's filesystem export Export a container's filesystem as a tar archive import Import the contents from a tarball to create a filesystem image Docker registry 操作: login Log in to a Docker registry. logout Log out from a Docker registry. Volume 操作 volume Manage Docker volumes 網絡操作 network Manage Docker networks Swarm 相關操作 swarm Manage Docker Swarm service Manage Docker services node Manage Docker Swarm nodes 系統操作: version Show the Docker version information events Get real time events from the server (持續返回docker 事件) info Display system-wide information (顯示Docker 主機系統范圍內的信息)
新聞熱點
疑難解答