操作命令
Docker提供了一組功能強大的操作命令,通過它們可以完成管理、操作等任務。
命令格式:
docker [option] [command] [arguments]
命令說明:
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 docker-subcommand --help
使用鏡像
Docker 容器是根據 Docker Image構建的。 默認情況下,Docker 從 Docker Hub 中提取這些鏡像,Docker 是 Docker 項目背後的公司,管理着 Docker 註冊中心。 任何人都可以在 Docker Hub 上託管他們的 Docker 映像,所以你需要的大多數應用程序和 Linux 發行版都會在那裏託管映像。
測試是否可用
docker run hello-world
Docker 最初無法在本地找到 hello-world 映像,因此它從 Docker Hub (默認存儲庫)下載了這個映像。 下載IMage後,Docker 根據Image和執行的容器內的應用程序創建一個容器,顯示消息。
常用命令
搜索鏡像
magc@magc-ThinkPad-T480:~$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 10811 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 419 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 244 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 214 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 106 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 98 [OK]
neurodebian NeuroDebian provides neuroscience research s… 68 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=m… 44 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images w… 24 [OK]
i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 19
1and1internet/ubuntu-16-apache-php-5.6 ubuntu-16-apache-php-5.6 14 [OK]
1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]
eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 12 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 11 [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 7 [OK]
1and1internet/ubuntu-16-apache-php-7.1 ubuntu-16-apache-php-7.1 6 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 5 [OK]
pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 4
1and1internet/ubuntu-16-nginx-php-7.0 ubuntu-16-nginx-php-7.0 4 [OK]
pivotaldata/ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2
smartentry/ubuntu ubuntu with smartentry 1 [OK]
1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK]
pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1
1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
更換國內Docker倉庫
在服務器本機的/etc/docker/下新建daemon.json,並輸入以下內容:如果需要更換不同的鏡像地址,直接替換掉“https://alzgoonw.mirror.aliyuncs.com”即可
{
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"]
}
重啓並加載配置:
systemctl daemon-reload //載入daemon.json
systemctl restart docker //重啓docker
查看是否生效:
通過docker info命令,可以在底部看到倉庫地址已經更新了。
下載鏡像
docker pull ubuntu
在下載了一個映像之後,您可以使用 run 子命令使用下載的映像運行一個容器。 正如您在 hello-world 示例中看到的那樣,如果使用 run 子命令執行 Docker 時沒有下載映像,Docker 客戶機將首先下載映像,然後使用它運行一個容器。
查看本地鏡像
magc@magc-ThinkPad-T480:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 4e5021d210f6 4 weeks ago 64.2MB
本地的鏡像可以運行形成容器,它可以被修改,生成新的鏡像文件,
運行容器
使用鏡像啓動容器
作爲一個例子,讓我們使用最新的 Ubuntu 映像運行一個容器。 I 和 t 開關的組合使您可以對容器進行交互式 shell 訪問:
docker run -it ubuntu
注:其中i和t參數的組合會使你可以使用與容器進行交互式的Shell
啓動後,將得到類似下面的終端提示:
root@d9b100f2f636:/#
注意命令提示符中的容器 id。 在這個示例中,它是 d9b100f2f636。 稍後您將需要該容器 ID 來標識需要移除的容器。
現在,您可以在容器內運行任何命令。 例如,讓我們更新容器內的包數據庫。 你不需要給任何命令加上 sudo 的前綴,因爲你是以 root 用戶的身份在容器中操作的:
apt-get update
這裏試着安裝一下nodejs:
sudo apt-get install nodejs
安裝之後,查看Nodejs的版本:
node -v
注:在容器中所作的修改只適用於當前容器。直接退出後將消失。
退出容器
使用exit命令來退出當前容器
exit
管理Docker
查看容器
使用 Docker 一段時間後,您的計算機上將有許多活動(正在運行)和非活動容器。 要查看活動的文件,請使用:
# 查看活動的容器
docker ps
# 查看所有容器(活動的和非活動的)
docker ps -a
# 查看最新創建的容器
docker ps -l
例如:
magc@magc-ThinkPad-T480:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb997f81f82c ubuntu "/bin/bash" 2 minutes ago Exited (127) 30 seconds ago charming_dewdney
6e5462d590b6 ubuntu "/bin/bash" 8 minutes ago Exited (0) 3 minutes ago modest_blackburn
magc@magc-ThinkPad-T480:~$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb997f81f82c ubuntu "/bin/bash" 4 minutes ago Exited (127) 2 minutes ago charming_dewdney
啓動容器
若要啓動已停止的容器,請使用 docker start,後跟容器 ID 或容器名稱。 讓我們啓動 ID 爲 cb997f81f82c的基於 ubuntu 的容器:
magc@magc-ThinkPad-T480:~$ docker start cb997f81f82c
cb997f81f82c
magc@magc-ThinkPad-T480:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb997f81f82c ubuntu "/bin/bash" 7 minutes ago Up 12 seconds charming_dewdney
注: 啓動新容器時,可以通過參數 --name 來指定一個名字, 還可以使用–rm參數,代表該容器停止時就自動刪除
連接到正在運行的容器
先找到要連接的容器的ID,
docker exec -it /bin/bash
停止容器
若要停止正在運行的容器,請使用 docker stop,後跟容器 ID 或名稱。
magc@magc-ThinkPad-T480:~$ docker stop cb997f81f82c
cb997f81f82c
magc@magc-ThinkPad-T480:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
刪除容器
當您決定不再需要容器時,使用 docker rm 命令移除它,同樣使用容器 ID 或者名稱。 使用 docker ps-a 命令查找與 hello-world 映像關聯的容器的容器 ID 或名稱並將其刪除。
注:容器可以轉換成IMage,您可以使用這些IMage構建新的容器。
保存容器的修改到鏡像中
啓動 Docker 映像時,您可以像使用虛擬機一樣創建、修改和刪除文件。 您所做的更改只適用於該容器。 您可以啓動和停止它,但是一旦使用 docker rm 命令銷燬它,更改將永遠丟失。
在這裏,我們將展示如何將容器中修改保存到鏡像中,以便下次直接使用最新的鏡像內容。
例如,在上面我們在ubuntu的一個容器中安裝了Nodejs,此時容器的狀態與原始鏡像已經不同,如果我們想將最新的變更保存到鏡像中,以便後面可以直接使用成果,可以用下面的辦法,生成一個新的鏡像:
magc@magc-ThinkPad-T480:~$ docker commit -m "added Nodejs" -a "magc" 6e5462d590b6 magc/ubuntu-nodejs
sha256:824e7cdb2d5020ea901474b6b5d764ae5dc97c23223ee95c0bf17f60f1943ab1
查看本地的鏡像:
magc@magc-ThinkPad-T480:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
magc/ubuntu-nodejs latest 824e7cdb2d50 28 seconds ago 153MB
ubuntu latest 4e5021d210f6 4 weeks ago 64.2MB
可以發現,其中magc/ubuntu-nodejs 就是新生成的鏡像文件,大小已經比原來ubuntu鏡像大了不少。