docker学习2-搭建centos7-python3.6环境

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 ~]#

 

转载于:https://www.cnblogs.com/leslie003/p/11452066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值