search搜索鏡像
docker search : 從Docker Hub(https://hub.docker.com)中搜索指定的鏡像
比如我這里搜索一個基於centos7環境安裝的python3.6版本
- NAME 鏡像倉庫名稱
- DESCRIPTION 鏡像描述信息
- STARS 鏡像收藏數
- OFFICIAL 是否為docker官方發布的鏡像
- AUTOMATED 是否為自動化構建的鏡像
docker search python[root@bogon ~]# docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 4469 [OK]
django Django is a free web application framework, … 872 [OK]
pypy PyPy is a fast, compliant alternative implem… 204 [OK]
kaggle/python Docker image for Python scripts run on Kaggle 128 [OK]
arm32v7/python Python is an interpreted, interactive, objec… 38
centos/python-35-centos7 Platform for building and running Python 3.5… 36
joyzoursky/python-chromedriver Python with Chromedriver, for running automa… 35 [OK]
circleci/python Python is an interpreted, interactive, objec… 31
hylang Hy is a Lisp dialect that translates express… 23 [OK]
centos/python-36-centos7 Platform for building and running Python 3.6… 21
nikolaik/python-nodejs Python with Node.js 19 [OK]
arm64v8/python Python is an interpreted, interactive, objec… 17
centos/python-27-centos7 Platform for building and running Python 2.7… 15
publicisworldwide/python-conda Basic Python environments with Conda. 6 [OK]
bitnami/python Bitnami Python Docker Image 4 [OK]
dockershelf/python Repository for docker images of Python. Test… 4 [OK]
i386/python Python is an interpreted, interactive, objec… 3
komand/python-plugin DEPRECATED: Komand Python SDK 2 [OK]
ppc64le/python Python is an interpreted, interactive, objec… 2
centos/python-34-centos7 Platform for building and running Python 3.4… 2
amd64/python Python is an interpreted, interactive, objec… 1
qbtrade/python python 3.6.5 with requirements last update s… 0
ccitest/python CircleCI test images for Python 0 [OK]
openshift/python-33-centos7 DEPRECATED: A Centos7 based Python v3.3 imag… 0
saagie/python Repo for python jobs
pull下載鏡像
上面找的想要下載的鏡像centos/python-36-centos7
,接下來下載到自己本地
docker pull centos/python-36-centos7
[root@bogon ~]# docker pull centos/python-36-centos7
Using default tag: latest
latest: Pulling from centos/python-36-centos7
8ba884070f61: Pull complete
c3dca185eb14: Pull complete
ee720ba20823: Pull complete
497ef6ea0fac: Pull complete
ebf1fb961f61: Pull complete
b8249f70ce00: Pull complete
ebd817e2efe7: Pull complete
d3d10dd0937c: Pull complete
a8ad47ec8182: Pull complete
Digest: sha256:d10c46b6db436357965a96716e9f5d230d9b1a58c6db1f0c4f43c1fb1994cd79
Status: Downloaded newer image for centos/python-36-centos7:latest
docker.io/centos/python-36-centos7:latest
[root@bogon ~]#
images本地鏡像查看
使用docker images查看本地已經下載好的鏡像
docker images
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/python-36-centos7 latest b8d15efaa8ec 5 months ago 651MB
[root@bogon ~]#
運行交互式的容器
Docker會在隔離的容器中運行進程。當運行docker run命令時,Docker會啟動一個進程,並為這個進程分配其獨占的文件系統、網絡資源和以此進程為根進程的進程組。
在容器啟動時,鏡像可能已經定義了要運行的二進制文件、暴露的網絡端口等,但是用戶可以通過docker run命令重新定義
最基本的docker run命令的格式如下:
$ sudo docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]
比如我這里要啟動centos7,進入交互模式,通過docker的兩個參數 -i -t,讓docker運行的容器實現"對話"的能力
- t: 在新容器內指定一個偽終端或終端。
- i: 允許你對容器內的標准輸入 (STDIN) 進行交互
docker run -i -t centos/python-36-centos7 /bin/bash
如下進入centos終端后,進python交互環境打印"hello world! I’m comming!!!",最后用exit退出
[root@bogon ~]# docker run -i -t centos/python-36-centos7 /bin/bash
(app-root)
(app-root) python
Python 3.6.3 (default, Mar 20 2018, 13:50:41)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("呵呵噠")
呵呵噠
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
(app-root) exit
exit
后台模式啟動
run加上-i -t是進交互模式,如果不想進交互模式直接執行腳本,可以直接使用run,比如echo "hello world",屏幕會輸出"hello world"
docker run centos/python-36-centos7 /bin/echo "hello world"
如果不想在前台執行,一般我們運行環境會選擇掛后台,加個-d 參數即可
docker run -d centos/python-36-centos7 /bin/echo "hello world"
[root@bogon ~]# docker run centos/python-36-centos7 /bin/echo "heheda"
heheda
[root@bogon ~]# docker run -d centos/python-36-centos7 /bin/echo "heheda"
6b09d3db0408847210ad193631d5a99d958827e4036e97cd57f791d0381afd7b
[root@bogon ~]#
使用-d后台執行后,會發現下面多了一長串,這個就是容器的唯一id,可以通過這個id找到容器
ps查看容器
先run下 training/webapp
docker run -d -p 5000:5000 training/webapp python app.py
使用docker ps查看正在運行的容器
docker ps
[root@bogon ~]# docker run -d -p 5000:5000 training/webapp python app.py
Unable to find image 'training/webapp:latest' locally
latest: Pulling from training/webapp
[DEPRECATION NOTICE] registry v2 schema1 support will be removed in an upcoming release. Please contact admins of the docker.io registry NOW to avoid future disruption.
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
a4473d7b0f6df84f73d5e88f887a7aab47715a388347b6b0ac870ccab403b35b
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4473d7b0f6d training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp exciting_vaughan
[root@bogon ~]# ls
上面的echo "hello world"只是一個很簡單的輸出指令,執行完就關閉了,所以ps查找正在運行的查不到,可以加個-a參數,顯示所有的容器,包括未運行的
ps 查找參數相關語法
- -a :顯示所有的容器,包括未運行的。
- -f :根據條件過濾顯示的內容。
- --format :指定返回值的模板文件。
- -l :顯示最近創建的容器。
- -n :列出最近創建的n個容器。
- --no-trunc :不截斷輸出。
- -q :靜默模式,只顯示容器編號。
- -s :顯示總的文件大小。
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4473d7b0f6d training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp exciting_vaughan
6b09d3db0408 centos/python-36-centos7 "container-entrypoin…" 10 minutes ago Exited (0) 10 minutes ago objective_swirles
1d5973e54fea centos/python-36-centos7 "container-entrypoin…" 11 minutes ago Exited (0) 11 minutes ago inspiring_haslett
f4bcc4c41af9 centos/python-36-centos7 "container-entrypoin…" 13 minutes ago Exited (0) 13 minutes ago funny_jang
[root@bogon ~]#
這樣就能找到上面的容器id 1d5973e54fea,不過沒那么長
logs查看日志
可以通過容器id去查運行的日志
docker logs [容器id]
[root@bogon ~]# docker logs 1d5973e54fea heheda [root@bogon ~]#
也可以通過容器的名稱去查看,注意這里是容器的名稱,不是鏡像名稱。容器名稱是系統自動分配的,如上面的最后一項NAMES 值 inspiring_haslett
[root@bogon ~]# docker logs inspiring_haslett
heheda
[root@bogon ~]# ^C
[root@bogon ~]
-f: 讓 docker logs 像使用 tail -f 一樣來輸出容器內部的標准輸出。
docker logs -f 1d5973e54fea
停止容器
可以用ps查看正在運行的容器
docker ps
[root@bogon ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a4473d7b0f6d training/webapp "python app.py" 50 minutes ago Up 49 minutes 0.0.0.0:5000->5000/tcp exciting_vaughan [root@bogon ~]#
停止容器的話,可以用stop容器的id或者容器NAME名稱
docker stop a4473d7b0f6d
或者提供容器名稱kind_kirch來停止
docker stop exciting_vaughan
啟動容器
提供start啟動容器
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4473d7b0f6d training/webapp "python app.py" 53 minutes ago Exited (137) About a minute ago exciting_vaughan
6b09d3db0408 centos/python-36-centos7 "container-entrypoin…" About an hour ago Exited (0) About an hour ago objective_swirles
1d5973e54fea centos/python-36-centos7 "container-entrypoin…" About an hour ago Exited (0) About an hour ago inspiring_haslett
f4bcc4c41af9 centos/python-36-centos7 "container-entrypoin…" About an hour ago Exited (0) About an hour ago funny_jang
[root@bogon ~]# docker start a4473d7b0f6d
a4473d7b0f6d
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4473d7b0f6d training/webapp "python app.py" 53 minutes ago Up 6 seconds 0.0.0.0:5000->5000/tcp exciting_vaughan
正在運行的容器,可以使用 docker restart 命令來重啟
刪除容器
使用 docker rm 命令來刪除不需要的容器
[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4473d7b0f6d training/webapp "python app.py" 56 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp exciting_vaughan
6b09d3db0408 centos/python-36-centos7 "container-entrypoin…" About an hour ago Exited (0) About an hour ago objective_swirles
1d5973e54fea centos/python-36-centos7 "container-entrypoin…" About an hour ago Exited (0) About an hour ago inspiring_haslett
f4bcc4c41af9 centos/python-36-centos7 "container-entrypoin…" About an hour ago Exited (0) About an hour ago funny_jang
[root@bogon ~]# docker rm 6b09d3db0408
6b09d3db0408
[root@bogon ~]#
但是rm 正在運行的容器時候,它會報錯,需先stop,才能rm
[root@bogon ~]# docker rm a4473d7b0f6d
Error response from daemon: You cannot remove a running container a4473d7b0f6df84f73d5e88f887a7aab47715a388347b6b0ac870ccab403b35b. Stop the container before attempting removal or force remove
[root@bogon ~]#