Docker commands和Dockerfile

Docker commands和Dockerfile

標籤 : dockergit


[TOC]github


本文主要對Docker commands和Dockerfile的相關知識進行整理docker

Docker commands

官網傳送門:shell

首先,固然是配置命令自動補全,只須要把一個文件用curl下載copy到特定路徑便可,具體操做參考Command-line Completionbash

其實docker有很完備的命令幫助提示,對哪一個指令不清楚,只須要在後面加--help就能看到幫助說明。例如:app

  • 輸入docker --help能夠看到全部可執行的命令。
  • 隨便挑一個,好比run命令,則輸入docker run --help又能看到run的相關幫助了。

經常使用命令:curl

  • 查看本地images:
docker images
  • 運行image
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
//i.e.
docker run image
docker run -it image /bin/bash

經常使用的一些參數:ide

  • --rm:container退出後自動刪除post

  • -i-t經常一塊兒用,-it:以超級管理員權限打開一個命令行窗口網站

  • -d: 後臺運行container

  • --name:給container命名

  • 查看當前container

docker ps -a
  • 刪除全部狀態的container
docker rm $(docker ps -a -q)
  • 經過另外的tty查看已經運行的容器
docker exec -it ${container_id} /bin/bash
  • 查看容器的信息
docker inspect ${container_id}

另外, 在以上指令中,容器名和容器的container_id都是能夠使用的,若是用戶沒有指定容器名,docker會默認給每一個容器分配一個比較友好的隨機名稱,像fervent_perlman,high_galileo等等

Dockerfile

官網傳送門:

感受文檔裏說了很全了,這裏稍微提幾個容易困惑的點

1.exec form vs shell form

CMDENTRYPOINT都涉及到着兩種形式(CMD多一種徹底做爲參數的形式),例如:

  • CMD ["executable","param1","param2"](exec形式,推薦)
  • CMD command param1 param2 (shell形式)

至於兩種形式的區別,官方的幾點說明挺詳細的,主要就是變量替換,腳本環境等方面有差異:

  • Note: If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.
  • Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (「) around words not single-quotes (‘).
  • Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ].

2.ENTRYPOINT vs CMD

讀完官方的Understand how CMD and ENTRYPOINT interact ,以爲這二者特別類似,對這二者有什麼區別和聯繫仍是有些困惑,閱讀下面這篇文章:

Dockerfile: ENTRYPOINT vs CMD

簡而言之,ENTRYPOINT更像一個寫死的可執行指令,CMD更像默認的一個可選項。

一個image只作一個單一的用途,就像一個可執行的命令時,建議使用ENTRYPOINT,把CMD做爲默認參數(第三種形式CMD ["param1","param2"] (as default parameters to ENTRYPOINT))。由於通常而言,ENTRYPOINT是不被覆蓋的(除非在run時顯式使用--entrypoit),而CMD是defaults的選項,從前文的run命令格式docker run [OPTIONS] IMAGE [COMMAND] [ARG...]可知,用戶能夠在運行images時輸入本身的COMMAND來覆蓋默認的CMD。

3.ADD vs COPY

這兩個好像都是把東西從host拷貝到docker的container裏,官方比較以下:

Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. That’s because it’s more transparent than ADD. COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.

簡單來講,主要就兩點區別:

  • COPY只能把本地文件拷貝到container裏;ADD還支持從遠程拷貝(remote URL support)
  • ADD能夠自動解壓本地壓縮文件

官方建議用COPY(preferred)

參考連接


做者@brianway更多文章:我的網站 | CSDN | oschina

相關文章
相關標籤/搜索