Docker基礎

Docker經常使用命令

0x01 幫助命令

docker version

docker info

docker --help

0x02 鏡像命令

Docker images

列出本地主機上的鏡像名docker

Docker search 鏡像名

從dockerhub上搜索鏡像shell

docker pull 鏡像名

下載鏡像ubuntu

docker rmi 鏡像名ID

刪除鏡像bash

0x03 容器命令

前提:有鏡像才能建立容器服務器

新建並啓動容器

docker run [OPTIONS] IMAGE [Command] 參數

[OPTIONS]說明:tcp

​ --name=「容器名」:爲容器指定一個名稱;ide

​ -d:後臺運行容器。並返回容器ID(即啓動守護式容器);ui

​ -i:以交互模式運行容器命令行

​ -t:爲容器從新分配一個僞輸入終端3d

​ -P:隨機端口映射

​ -p:指定端口映射,有如下四種格式

​ ip:hostPort:containerPort

​ ip::containerPort

​ hostPort:containerPort

​ containerPort

列出當前全部正在運行的容器

docker ps [OPTIONS]

[OPTIONS]說明:

​ -a:列出當前全部正在運行的容器+歷史上運行過的

​ -l:顯示最近建立的容器

​ -n:顯示最近n個建立的容器

​ -q:靜默模式,只顯示容器編號

​ --no-trunc:不截斷輸出

退出容器

​ exit:容器中止退出

​ Ctrl+P+Q:容器不中止退出

啓動容器

docker start ContainerID

重啓容器

docker restart ContainerID

中止容器

docker stop ContainerID

強制中止容器

docker kill ContainerID

刪除已中止的容器

刪除單個容器:
docker rm ContainerID
刪除多個容器
docker rm -f$(docker ps -a -q)
docker ps -a -q | xargs docker rm

0x04 重要

啓動守護式容器:

docker run -d ContainerID

使用如下命令建立一個以進程方式運行的容器

runoob@runoob:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63

在輸出中,咱們沒有看到指望的 "hello world",而是一串長字符

2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63

這個長字符串叫作容器 ID,對每一個容器來講都是惟一的,咱們能夠經過容器 ID 來查看對應的容器發生了什麼。

首先,咱們須要確認容器有在運行,能夠經過 docker ps 來查看:

輸出詳情介紹:

CONTAINER ID: 容器 ID。

IMAGE: 使用的鏡像。

COMMAND: 啓動容器時運行的命令。

CREATED:容器的建立時間。

STATUS:容器狀態(狀態有7種):

  • created(已建立)
  • restarting(重啓中)
  • running(運行中)
  • removing(遷移中)
  • paused(暫停)
  • exited(中止)
  • dead(死亡)

PORTS: 容器的端口信息和使用的鏈接類型(tcp\udp)。

NAMES:自動分配的容器名稱。

查看容器日誌:

docker logs -f -t --tail 數字 ContainerID

​ -t:加入時間戳

​ -f:跟隨最新的日誌打印

​ --tail 數字:顯示最後多少條

查看容器內運行的進程:

docker top ContainerID

查看容器內部細節:

docker inspect ContainerID

進入正在運行的容器並以命令行交互:

docker exec -it ContainerID /bin/bash

docker attach ContainerID

上述兩個區別:

attach:直接進入容器啓動命令的終端,不會啓動新的進程(不作任何操做)

exec:是在容器中打開新的終端,而且能夠啓動新的進程(能夠作操做,返回結果)

從容器內拷貝文件到主機上

docker cp ContainerID:ContainerPath 宿主機Path

Docker鏡像

特色:Docker鏡像都是隻讀的,當容器啓動時,一個新的可寫層被加載到鏡像的頂部。這一層被稱做「容器層」,「容器層」之下的都叫「鏡像層」。

docker commit :從容器建立一個新的鏡像。

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

OPTIONS說明:

​ -a:提交鏡像的做者

​ -c:使用Dockerfile指令來建立鏡像;

​ -m:提交時的說明文字;

​ -p:在commit時,將容器暫停。

將鏡像推送到倉庫

一、登陸

docker login http://xxxxx.com

二、登陸私有hub建立項目

例如項目叫:abc-dev

三、給鏡像打tag

docker tag a29d376ad1b9 blackcicada/h4ck3r:1.0

  a29d376ad1b9:IMAGE ID,能夠用docker images 查看

  docker.io:私有hub域名

  blackcicada:項目名稱

  h4ck3r:鏡像名稱

  1.0:鏡像版本號

四、推送

  docker push blackcicada/h4ck3r:1.0

總結

attach Attach to a running container # 當前 shell 下 attach 鏈接指定運行鏡像

build Build an image from a Dockerfile # 經過 Dockerfile 定製鏡像

commit Create a new image from a container changes # 提交當前容器爲新的鏡像

cp Copy files/folders from the containers filesystem to the host path #從容器中拷貝指定文件或者目錄到宿主機中

create Create a new container # 建立一個新的容器,同 run,但不啓動容器

diff Inspect changes on a container's filesystem # 查看 docker 容器變化

events Get real time events from the server # 從 docker 服務獲取容器實時事件

exec Run a command in an existing container # 在已存在的容器上運行命令

export Stream the contents of a container as a tar archive # 導出容器的內容流做爲一個 tar 歸檔文件[對應 import ]

history Show the history of an image # 展現一個鏡像造成歷史

images List images # 列出系統當前鏡像

import Create a new filesystem image from the contents of a tarball # 從tar包中的內容建立一個新的文件系統映像[對應export]

info Display system-wide information # 顯示系統相關信息

inspect Return low-level information on a container # 查看容器詳細信息

kill Kill a running container # kill 指定 docker 容器

load Load an image from a tar archive # 從一個 tar 包中加載一個鏡像[對應 save]

login Register or Login to the docker registry server # 註冊或者登錄一個 docker 源服務器

logout Log out from a Docker registry server # 從當前 Docker registry 退出

logs Fetch the logs of a container # 輸出當前容器日誌信息

port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應的容器內部源端口

pause Pause all processes within a container # 暫停容器

pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務器拉取指定鏡像或者庫鏡像

push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務器

rm Remove one or more containers # 移除一個或者多個容器

rmi Remove one or more images # 移除一個或多個鏡像[無容器使用該鏡像纔可刪除,不然需刪除相關容器纔可繼續或 -f 強制刪除]

run Run a command in a new container # 建立一個新的容器並運行一個命令

save Save an image to a tar archive # 保存一個鏡像爲一個 tar 包[對應 load]

search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像

tag Tag an image into a repository # 給源中鏡像打標籤

top Lookup the running processes of a container # 查看容器中運行的進程信息

unpause Unpause a paused container # 取消暫停容器

wait Block until a container stops, then print its exit code # 截取容器中止時的退出狀態值

相關文章
相關標籤/搜索