從零開始搭建服務器之更加優雅地部署項目

若是你須要常常性須要多處部署一樣的項目,若是你曾經也遇到過"明明在我電腦運行得好好的"問題,若是據說過 Docker 但還沒用過,若是你不肯定你到底需不須要 Docker ,那麼,但願你花時間閱讀一下這篇文章!html

由於 Docker 將幫助你輕鬆運行本身不熟悉語言編寫的開源項目,幫助你更加優雅地部署本身的項目,省去重複下載並配置環境的繁瑣過程...node

如今讓咱們先睹爲快,預覽一下基於 Docker 部署項目的實際效果,但願能讓你對 Docker 有個初步的印象!linux

  • Docker 部署的 nginx 做爲反向代理服務器,支持 https 訪問以及泛域名解析.

體驗地址: snowdreams1006.cn/nginx

docker-snowdreams1006.cn-nginx-https-preview.png

  • Docker 部署的 letsencrypt 免費製做泛域名證書並整合反向代理服務 nginx 實現 https 訪問.

體驗地址: www.snowdreams1006.cn/git

docker-snowdreams1006.cn-letsencrypt-https-preview.png

  • Docker 部署的 nginx 做爲靜態服務器,部署靜態網站用於演示靜態博客功能.

體驗地址: resume.snowdreams1006.cn/github

docker-snowdreams1006.cn-nginx-static-preview.png

  • Docker 部署的 bark 做爲後端服務器,部署開源項目用於充當消息推送服務器.

體驗地址: bark.snowdreams1006.cn/pingweb

docker-snowdreams1006.cn-bark-go-preview.png

  • Docker 部署的 webhook 做爲後端服務器,部署開源項目用於接收 Webhook 事件回調.

體驗地址: webhook.snowdreams1006.cn/hooks/githu…docker

docker-snowdreams1006.cn-webhook-github-preview.png

  • Docker 部署的 blog 做爲靜態服務器,基於 Github ActionWebhook 實現博客內容自動更新並推送消息.

Github 倉庫內容更新後觸發 Github Action 自動構建並部署遠程服務器靜態博客,同時發送的 Webhook 事件給 webhook 鉤子容器,緊接着調用 bark 消息推送容器,實現消息推送到微信消息以及 app 通知.json

docker-snowdreams1006.cn-blog-action-preview.png

Github 倉庫更新後自動運行 Github Action 源碼構建靜態博客並上傳到遠程服務器,blog 容器會當即重啓完成內容更新.ubuntu

docker-snowdreams1006.cn-blog-notice-preview.jpeg

Github 倉庫更新後發送 Webhooks 到遠程服務器,webhook 容器接收到請求後轉發給 bark 容器,進而推送給手機.

不管是熟悉的開源項目仍是陌生的開源項目,Docker 讓這些不同變得同樣,統一的管理方式使得使用成本大大下降,更加優雅地部署項目,真的不止是說說而已!

docker-logo-whale.jpg

前提條件

目前在 Linux 系統上安裝 Docker,對系統版本有如下要求:

  • CentOS : 7
  • Debian : 7.7(Wheezy LTS)、8.0(Jessie LTS)、9(Stretch)
  • Fedora : 2四、25
  • Ubuntu : 16.04(Xenial LTS)、14.04(Trusty LTS)、17.04(Zesty)

一方面上述前提條件基本上新服務器都會知足,另外一方面筆者對此並未深刻實驗,請讀者自行驗證,下面主要以 Centos7.6 爲例講解如何安裝 Docker .

驗證環境

對於新手來講,儘管安裝 Docker 很是簡單,可是老是不可避免地會遇到一些意外狀況,或許是安裝出錯須要從新安裝或者是不肯定遠程服務器是否已經安裝,因此開始安裝前仍是先看一下到底有沒有安裝過 Docker 吧!

  • 調用 docker 命令

首先鏈接到遠程服務器後運行 docker 命令,若是像下面那樣輸出一大堆用法介紹,那麼證實 Docker 已經成功安裝過,而且可能已經配置好相關環境了.

你如今惟一要作的就是學習一下 Docker 的基本用法,由於不用本身安裝 Docker 環境,基本上也能夠沒必要往下看了.

[root@snowdreams1006 ~]# docker

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images 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 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE 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 version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. 複製代碼

若是你輸入 docker 提示 command not found ,說明服務器極可能並無安裝 Docker 環境,下面就教你如何一步一步安裝 Docker 環境!

安裝 Docker

  1. Step 1 : 移除舊版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
複製代碼

這一步是可選的,是由於最新版 Docker 的名稱已經發生了變化,爲了保證安裝的是最新版的 Docker-CE ,因此首先卸載可能已經安裝過的舊版本.

  1. Step 2 : 安裝必要系統依賴
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
複製代碼

安裝一些必要依賴,跟着官方教程說明走就行了,即便系統已存在該環境也能夠再次運行,放心複製粘貼吧!

  1. Step 3 : 添加軟件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
複製代碼
  1. Step 4 : 更新 yum 緩存
sudo yum makecache fast
複製代碼
  1. Step 5 : 安裝 docker-ce
sudo yum -y install docker-ce
複製代碼

若是上述安裝過程當中沒有出現任何報錯,那麼如今已經安裝好基本的 Docker 環境!

啓動 Docker

  • 查看狀態
sudo systemctl status docker
複製代碼

初次安裝成功後默認是不會自動啓動 Docker 服務的,此時查看運行狀態的輸出結果不會包括 Active: active (running) 而是 Active: inactive (dead) .

  • 首次啓動
sudo systemctl start docker
複製代碼

安裝後默認是沒有啓動 Docker 服務的,所以安裝後須要先啓動 Docker 服務,再次查看運行狀態 sudo systemctl status docker 應該會出現正在運行 Active: active (running) .

  • 從新啓動
sudo systemctl restart docker
複製代碼

若是 Docker 服務已中止能夠從新啓動,若是已經啓動也能夠從新啓動.

  • 中止服務
sudo systemctl stop docker
複製代碼

若是正在運行的 Docker 存在問題須要中止維修,那麼能夠先中止 Docker 服務,待維修結束後能夠運行 sudo systemctl start docker 再次啓動服務.

  • 檢查自啓
systemctl list-unit-files | grep enabled | grep docker
複製代碼

檢查 Docker 服務是否會開機自啓,若是存在結果則表示會開機自啓,若是沒有結果則表示不會開機自啓.

  • 開機自啓
sudo systemctl enable docker
複製代碼

Docker 服務是很是重要的進程服務,通常須要開機自啓,保證意外關機後能自行恢復服務,推薦開機自啓.

  • 禁止自啓
sudo systemctl disable docker
複製代碼

若是不當心設置了開機自啓而你真的不打算開機自啓的話,那麼能夠禁用開機自啓功能,下次電腦重啓後不會自動啓動Docker 服務.

  • 查看版本
docker version
複製代碼

查看當前安裝的 Docker 版本信息,能夠看出來主要分爲兩部分: Client: Docker Engine - CommunityServer: Docker Engine - Community .

[root@snowdreams1006 ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:25:41 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:24:18 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
複製代碼

如今並沒必要關心具體的版本信息,只要運行 docker version 命令後可以輸出相似信息便可,接下來開始真正的表演!

鏡像加速

Docker 服務已經安裝並啓動,接下來咱們能夠基於 Docker 部署應用了,固然如今離真正部署本身的應用還有不小距離,可是咱們能夠運行公開的應用啊!

學習任何新語言的第一件事就是運行 hello world ,學習 Docker 容器化部署也不例外,咱們也運行 Docker 版本的 hello world !

當咱們敲入 docker run hello-world 命令後,終端會輸出下列內容,只要輸出 Hello from Docker! 字樣就證實環境已經搭建完畢!

[root@snowdreams1006 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@snowdreams1006 ~]# 
複製代碼

若是你的網速比較慢,上述過程可能有一些耗時,可是若是你的網速通常而上述過程異常慢,極可能是由於你沒有配置鏡像!

由於 Docker 默認是先從國外下載項目到本地,而後再運行服務的,正如咱們平時訪問 Github 同樣,那網速不是通常的慢!

Github 沒有鏡像加速地址並不能爲咱們加速訪問,可是 Docker 項目倉庫是有鏡像倉庫的,國內提供這種鏡像服務的有很多,基本上都須要註冊帳號獲取鏡像地址之類的.

這裏提供一下網易的鏡像倉庫地址 http://hub-mirror.c.163.com 以及阿里雲的我的鏡像倉庫地址 https://8upnmlh3.mirror.aliyuncs.com .

只要將鏡像地址配置給 Docker ,下一次再下載項目時速度應該就會獲得明顯提高!

首選打開並編輯 /etc/docker/daemon.json 文件,若是沒有的話就新建該文件,內容以下:

{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
複製代碼

保存後重啓 Docker 服務,試一試運行 docker pull nginx 會不會很快呢?

卸載 docker

假如發現意外想要從新安裝 Docker 服務或者就是想要卸載 Docker ,那麼只須要簡單運行下列命令就能清除掉 Docker 環境!

sudo yum remove docker-ce
sudo rm -rf /var/lib/docker
複製代碼

此時再次運行 docker 命令就會提示 command not found ,期待下一次相見會讓人面目一新!

基本命令

正如初次見面的那樣,當咱們成功安裝 Docker 後控制檯輸出了一大堆關於用法的介紹,只不過當時年少輕狂並不在意,驀然回首,竟發現如此有用!

[root@snowdreams1006 ~]# docker

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images 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 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE 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 version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. 複製代碼

自我介紹

用法介紹的第一段就是自我介紹,用法是: docker [OPTIONS] COMMAND ,即 docker + 可選選項 + 必選命令.

表示的含義就是 A self-sufficient runtime for containers 爲容器提供一個自包含的運行環境!

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers
複製代碼

Docker 相似於生活中超級貨輪,運輸着統一規格的集裝箱,而集裝箱裝着各類各樣的貨物,開往不一樣的目的地.

容器則是集裝箱,貨輪爲集裝箱提供了自包含的環境,集裝箱之間是相互獨立的,這也是對第一段話的簡單解釋.

選項配置

下面咱們繼續看第二段內容,主要解釋了有哪些配置項以及這些配置項背後表示的具體含義.

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
複製代碼

只要有必定英語基礎的人應該都能看懂其中的意思,若是對我的細節不是很清楚的話,能夠複製粘貼到瀏覽器在線翻譯,這裏就不全文解釋了.

不消息看到了最後一個 -v, --version 選項,表示的意思是打印版本信息而且退出.

看到這裏咱們就明白了,原來以前運行的 docker version 和這裏的 --version 並非一回事啊!

[root@snowdreams1006 ~]# docker -v
Docker version 19.03.5, build 633a0ea
[root@snowdreams1006 ~]# docker --version
Docker version 19.03.5, build 633a0ea
複製代碼

單純從輸出結果來講,docker --version 更加簡潔,若是隻是驗證環境安裝是否成功,仍是運行docker --version 比較簡單明瞭.

管理命令

第三部分是 Docker 支持的管理命令,如今不去深究細節,只要有印象就行,注意這裏有個關於鏡像的命令 docker image

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes
複製代碼

由於自我介紹中關於用法是 docker [OPTIONS] COMMAND ,而中括號 [] 表示該內容是可選的,因此不加任何選項的基本用法就是 docker COMMAND ,所以其中關於 image 命令的完整用法就是: docker image .

[root@snowdreams1006 ~]# docker image

Usage:	docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
複製代碼

別有洞天,管理命令中還有子命令,大概用法和以前介紹的內容大體相同,基本用法是: docker image COMMAND .

其中支持的命令中有 ls ,所以調用 ls 命令的最終完整命令就是: docker image ls .

# docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              fce289e99eb9        11 months ago       1.84kB
複製代碼

服務器已安裝的 image 中就包括咱們熟悉的 hello-world ,至於什麼是 REPOSITORY ,什麼是 IMAGE 暫時也不用深究,只須要知道如何無文檔使用這些命令便可!

若是用心留意的話,能夠看到 Run 'docker image COMMAND --help' for more information on a command. 這麼一句話,看來咱們有現成的幫助文檔供咱們學習啊!

仍是以 ls 命令爲例,演示一下如何使用 docker image COMMAND --help 查看幫助文檔.

[root@snowdreams1006 ~]# docker image ls --help

Usage:	docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  ls, images, list

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output -q, --quiet Only show numeric IDs 複製代碼

麻雀雖小五臟俱全,沒想到 ls 命令還有更加細粒度的用法說明,支持可選參數和 [REPOSITORY[:TAG]] ,除此以外還有 ls, images, list 別名!

若是 lsimageslist 別名,那麼豈不是意味着 docker image ls 等價於 docker image imagesdocker image list ?

[root@snowdreams1006 ~]# docker image list
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              fce289e99eb9        11 months ago       1.84kB
[root@snowdreams1006 ~]# docker image images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              fce289e99eb9        11 months ago       1.84kB
複製代碼

從上述輸出結果來看,三者的運行效果確實是同樣的,看來又發現了新大陸!

普通命令

回到 docker 命令的主線,除了管理命令外仍是普通命令,這部分命令也是常常性使用到的命令也是重點學習掌握的命令!

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images 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 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE 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 version Show the Docker version information wait Block until one or more containers stop, then print their exit codes 複製代碼

命令雖好但不可貪多,仍是找到最簡單剛剛用過的 docker rundocker version 命令吧!

  • docker run : Run a command in a new container

表示在新的容器內運行命令,翻譯成生活語言就是在集裝箱內作着不可告人的神祕操做!

  • docker version : Show the Docker version information

顯示 Docker 版本信息,還記得 docker --version 嗎?

忘記了的話,往上翻翻看,--version 的描述是 Print version information and quit ,是一種更加簡單的版本信息.

不管是管理命令仍是普通命令,直接輸入命令後都會有相應的用法說明以及幫助信息,一樣地追加 --help 便可!

[root@snowdreams1006 ~]# docker run
"docker run" requires at least 1 argument.
See 'docker run --help'.

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container
複製代碼

幫助信息

最後的纔是亮點,在命令結尾處追加 --help 能夠獲取更加詳細的幫助信息,這一點不只適合一級命令 docker image --help 還適合二級子命令 docker image ls --help .

Run 'docker COMMAND --help' for more information on a command.
複製代碼

因此,遇到不懂的或者陌生的命令請必定要記住 --help 幫助命令,這是 docker 所有命令中最重要的一點!

回憶總結

Docker 是一種規範化的部署運維新方式,相對於傳統打包部署的來講,更加統一規範化,貨物是各類各樣的正如開發語言的多樣性同樣,可是集裝箱的出現卻顛覆了物流運輸,帶來了巨大的進步!

若是你是 Java 後臺開發,或多或少確定有着本身獨立部署項目的經歷,先登陸服務器裝個 Java 環境再裝個 Tomcat 環境,最後在上傳本身的 War 包到 Tomcat 部署目錄,如此重複繁瑣的勞動還不必定能保證一次性成功!

由於有時你的代碼中頗有可能有些絕對路徑,部署到服務器確定會報錯,若是缺乏了我的文件也會報錯等等,這時候就出現了經典的對話:明明在個人電腦運行地好好的啊!

Docker 的出如今必定程度上解決了這種問題,將應用打包到集裝箱,Docker 做爲超級貨輪承載着集裝箱安全快速地運送到目的地,集裝箱內的環境是自給自足的封閉環境,全部的相關依賴一次性所有都給你.

不管是本機運輸這個封閉的集裝箱仍是遠程服務器運輸這個集裝箱結果都是同樣的,不再會出現環境不一致而致使的相互埋怨狀況的發生了!

那麼問題來了,若是給你一個集裝箱,你能安全快速運輸到目的地嗎?若是你手頭上已經有一批貨須要這種集裝箱服務,如何快速封裝成集裝箱呢?

對於第一個問題,本文已經給出答案,那就是 docker + docker COMMAND --help 查詢支持的命令以及查看命令的幫助文檔.

[root@snowdreams1006 ~]# docker

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default
                           "/root/.docker")
  -c, --context string     Name of the context to use to connect to the
                           daemon (overrides DOCKER_HOST env var and
                           default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default
                           "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default
                           "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images 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 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE 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 version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. 複製代碼

對於第二個問題,請先預習 docker 相關命令,下一次將實例分享如何使用 docker 運輸集裝箱,感謝你的閱讀!

若是想要更方便地查看系列文章,歡迎訪問個人網站 snowdreams1006.tech/ 或者 blog.snowdreams1006.cn/ ,喜歡就點個贊再走唄!

參考資料

若是你以爲本文對你有所幫助,歡迎點贊留言告訴我,你的鼓勵是我繼續創做的動力,不妨順便關注下我的公衆號「雪之夢技術驛站」,按期更新優質文章喲!

雪之夢技術驛站.png
相關文章
相關標籤/搜索