docker学习2-快速搭建centos7-python3.6环境
Posted yoyoketang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker学习2-快速搭建centos7-python3.6环境相关的知识,希望对你有一定的参考价值。
前言
当我们在一台电脑上搭建了python3.6的环境,下次换了个电脑,或者换成linux的系统了,又得重新搭建一次,设置环境变量、下载pip等操作。
好不容易安装好,一会Scrips目录找不到pip.exe,一会提示pip不是内部或外部命令,一会又提示pip: command not found,想死的心都有了。
搭建环境成为不少小伙伴学习的障碍,从今天开始学好docker,环境问题从此和你说再见~~~
search搜索镜像
docker search : 从Docker Hub(https://hub.docker.com)中搜索指定的镜像
比如我这里搜索一个基于centos7环境安装的python3.6版本
- NAME 镜像仓库名称
- DESCRIPTION 镜像描述信息
- STARS 镜像收藏数
- OFFICIAL 是否为docker官方发布的镜像
- AUTOMATED 是否为自动化构建的镜像
关于自动化构建,可以查看官方文档:https://docs.docker.com/docker-hub/builds/#how-automated-builds-work
docker search python
[[email protected] ~]# docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 4288 [OK]
django Django is a free web application framework, … 847 [OK]
pypy PyPy is a fast, compliant alternative implem… 193 [OK]
kaggle/python Docker image for Python scripts run on Kaggle 123 [OK]
arm32v7/python Python is an interpreted, interactive, objec… 37
centos/python-35-centos7 Platform for building and running Python 3.5… 36
joyzoursky/python-chromedriver Python with Chromedriver, for running automa… 33 [OK]
circleci/python Python is an interpreted, interactive, objec… 29
nikolaik/python-nodejs Python with Node.js 18 [OK]
arm64v8/python Python is an interpreted, interactive, objec… 17
centos/python-36-centos7 Platform for building and running Python 3.6… 17
centos/python-27-centos7 Platform for building and running Python 2.7… 15
iron/python Tiny Python Microcontainer 9
publicisworldwide/python-conda Basic Python environments with Conda. 6 [OK]
dockershelf/python Repository for docker images of Python. Test… 4 [OK]
i386/python Python is an interpreted, interactive, objec… 3
bitnami/python Bitnami Python Docker Image 3 [OK]
komand/python-plugin DEPRECATED: Komand Python SDK 2 [OK]
centos/python-34-centos7 Platform for building and running Python 3.4… 2
muccg/python-base Base images that use python 1 [OK]
amd64/python Python is an interpreted, interactive, objec… 1
ccitest/python CircleCI test images for Python 0 [OK]
saagie/python Repo for python jobs 0
qbtrade/python python 3.6.5 with requirements last update s… 0
openshift/python-33-centos7 DEPRECATED: A Centos7 based Python v3.3 imag… 0
[[email protected] ~]#
pull下载镜像
上面找的想要下载的镜像centos/python-36-centos7
,接下来下载到自己本地
docker pull centos/python-36-centos7
[[email protected] ~]# 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
[[email protected] ~]#
images本地镜像查看
使用docker images查看本地已经下载好的镜像
docker images
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 4 years ago 349MB
运行交互式的容器
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退出
[[email protected] ~]# docker run -i -t centos/python-36-centos7 /bin/bash
(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("hello world! I’m comming!!!")
hello world! I’m comming!!!
>>> exit()
(app-root) exit
后台模式启动
run加上-i -t是进交互模式,如果不想进交互模式直接执行脚本,可以直接使用run,比如echo "hello world",屏幕会输出"hello world"
docker run centos/python-36-centos7 /bin/echo "hello world"
如果不想在前台执行,一般我们运行环境会选择挂后台,加个-d 参数即可
docker run centos/python-36-centos7 /bin/echo "hello world"
[[email protected] ~]# docker run centos/python-36-centos7 /bin/echo "hello world"
hello world
[[email protected] ~]# docker run -d centos/python-36-centos7 /bin/echo "hello world"
1e5c22451bf2215f6c098e066b74363f1db9cde97e9ecea02947ccbbf2fa7e8f
[[email protected] ~]#
使用-d后台执行后,会发现下面多了一长串,这个就是容器的唯一id,可以通过这个id找到容器
ps查看容器
先run下 training/webapp
docker run -d -p 5000:5000 training/webapp python app.py
使用docker ps查看正在运行的容器
docker ps
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
c9e8a325b145 training/webapp "python app.py" 16 hours ago Up 16 hours 0.0.0.0:32768->5000/tcp
[[email protected] ~]#
上面的echo "hello world"只是一个很简单的输出指令,执行完就关闭了,所以ps查找正在运行的查不到,可以加个-a参数,显示所有的容器,包括未运行的
ps 查找参数相关语法
- -a :显示所有的容器,包括未运行的。
- -f :根据条件过滤显示的内容。
- --format :指定返回值的模板文件。
- -l :显示最近创建的容器。
- -n :列出最近创建的n个容器。
- --no-trunc :不截断输出。
- -q :静默模式,只显示容器编号。
- -s :显示总的文件大小。
docker ps -a
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e5c22451bf2 centos/python-36-centos7 "container-entrypoin…" 10 minutes ago Exited (0) 10 minutes ago hopeful_poincare
1d14dd77352f centos/python-36-centos7 "container-entrypoin…" 12 minutes ago Exited (0) 12 minutes ago nervous_visvesvaraya
fefdcbb9c662 centos/python-36-centos7 "container-entrypoin…" 13 minutes ago Exited (0) 13 minutes ago quirky_cray
9df329b5effd centos/python-36-centos7 "container-entrypoin…" 13 minutes ago Exited (0) 13 minutes ago nifty_roentgen
c9e8a325b145 training/webapp "python app.py" 16 hours ago Up 16 hours 0.0.0.0:32768->5000/tcp kind_kirch
[[email protected] ~]#
这样就能找到上面的容器id 1e5c22451bf2
,不过没那么长
logs查看日志
可以通过容器id去查运行的日志
docker logs [容器id]
[[email protected] ~]# docker logs 1e5c22451bf2
hello world
[[email protected] ~]#
也可以通过容器的名称去查看,注意这里是容器的名称,不是镜像名称。容器名称是系统自动分配的,如上面的最后一项NAMES 值hopeful_poincare
[[email protected] ~]# docker logs hopeful_poincare
hello world
-f: 让 docker logs 像使用 tail -f 一样来输出容器内部的标准输出。
docker logs -f 1e5c22451bf2
停止容器
可以用ps查看正在运行的容器
docker ps
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9e8a325b145 training/webapp "python app.py" 17 hours ago Up 17 hours 0.0.0.0:32768->5000/tcp kind_kirch
停止容器的话,可以用stop容器的id或者容器NAME名称
docker stop c9e8a325b145
或者提供容器名称kind_kirch来停止
docker stop kind_kirch
启动容器
提供start启动容器
[[email protected] ~]# docker start c9e8a325b145
c9e8a325b145
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9e8a325b145 training/webapp "python app.py" 17 hours ago Up 5 seconds 0.0.0.0:32769->5000/tcp kind_kirch
[[email protected] ~]#
正在运行的容器,可以使用 docker restart 命令来重启
[[email protected] ~]# docker restart c9e8a325b145
c9e8a325b145
[[email protected] ~]#
删除容器
使用 docker rm 命令来删除不需要的容器
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e5c22451bf2 centos/python-36-centos7 "container-entrypoin…" 24 minutes ago Exited (0) 24 minutes ago hopeful_poincare
1d14dd77352f centos/python-36-centos7 "container-entrypoin…" 27 minutes ago Exited (0) 27 minutes ago nervous_visvesvaraya
fefdcbb9c662 centos/python-36-centos7 "container-entrypoin…" 27 minutes ago Exited (0) 27 minutes ago quirky_cray
9df329b5effd centos/python-36-centos7 "container-entrypoin…" 27 minutes ago Exited (0) 27 minutes ago nifty_roentgen
c9e8a325b145 training/webapp "python app.py" 17 hours ago Up 41 seconds 0.0.0.0:32770->5000/tcp kind_kirch
[[email protected] ~]# docker rm 1e5c22451bf2
1e5c22451bf2
[[email protected] ~]# docker rm 1d14dd77352f
1d14dd77352f
[[email protected] ~]# docker rm kind_kirch
Error response from daemon: You cannot remove a running container c9e8a325b14534f0b27cfd34e3ceefd16f6a6c9f136c0305d4e60de61f2badc3. Stop the container before attempting removal or force remove
[[email protected] ~]#
但是rm 正在运行的容器时候,它会报错,需先stop,才能rm
[[email protected] ~]# docker stop kind_kirch
kind_kirch
[[email protected] ~]# docker rm kind_kirch
kind_kirch
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[[email protected] ~]#
以上是关于docker学习2-快速搭建centos7-python3.6环境的主要内容,如果未能解决你的问题,请参考以下文章
jenkins学习1-docker快速搭建jenkins环境
本地使用 docker 快速搭建一个 PHP7.4 学习环境 | Laravel China 社区