centos7.4安装docker_pythondocker

centos7.4安装docker_pythondocker前言当我们在一台电脑上搭建了python3.6的环境,下次换台电脑,又得重新搭建一次,设置环境变量等操作。好不容易安装好,一会提示pip不是内部或外部命令,一会又提示pip:commandno

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

前言

当我们在一台电脑上搭建了python3.6的环境,下次换台电脑,又得重新搭建一次,设置环境变量等操作。
好不容易安装好,一会提示pip不是内部或外部命令,一会又提示pip: command not found,想死的心都有了。
搭建环境是真的麻烦,不弄好又不行,从今天开始学好docker,环境问题从此和你说再见
 

docker search搜索镜像

docker search : 从Docker Hub(https://hub.docker.com)中搜索指定的镜像
比如我这里搜索一个基于centos7环境安装的python3.6版本

[root@jkc ~]# docker search python
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
python                           Python is an interpreted, interactive, objec…   5776                [OK]                
django                           Django is a free web application framework, …   1040                [OK]                
pypy                             PyPy is a fast, compliant alternative implem…   262                 [OK]                
nikolaik/python-nodejs           Python with Node.js                             57                                      [OK]
joyzoursky/python-chromedriver   Python with Chromedriver, for running automa…   57                                      [OK]
arm32v7/python                   Python is an interpreted, interactive, objec…   54                                      
circleci/python                  Python is an interpreted, interactive, objec…   42                                      
centos/python-35-centos7         Platform for building and running Python 3.5…   39                                      
centos/python-36-centos7         Platform for building and running Python 3.6…   30                                      
hylang                           Hy is a Lisp dialect that translates express…   29                  [OK]                
arm64v8/python                   Python is an interpreted, interactive, objec…   24                                      
revolutionsystems/python         Optimized Python Images                         18                                      
centos/python-27-centos7         Platform for building and running Python 2.7…   17                                      
bitnami/python                   Bitnami Python Docker Image                     10                                      [OK]
publicisworldwide/python-conda   Basic Python environments with Conda.           6                                       [OK]
d3fk/python_in_bottle            Simple python:alpine completed by Bottle+Req…   5                                       [OK]
dockershelf/python               Repository for docker images of Python. Test…   5                                       [OK]
clearlinux/python                Python programming interpreted language with…   4                                       
i386/python                      Python is an interpreted, interactive, objec…   3                                       
centos/python-34-centos7         Platform for building and running Python 3.4…   2                                       
ppc64le/python                   Python is an interpreted, interactive, objec…   2                                       
amd64/python                     Python is an interpreted, interactive, objec…   1                                       
s390x/python                     Python is an interpreted, interactive, objec…   0                                       
ccitest/python                   CircleCI test images for Python                 0                                       [OK]
saagie/python                    Repo for python jobs                            0   

下载pull镜像

上面找的想要下载的镜像centos/python-36-centos7,接下来下载到自己本地
如果下载速度缓慢,可以参考这篇博客https://www.cnblogs.com/jiakecong/p/14308660.html

docker pull centos/python-36-centos7
[root@jkc docker]# docker pull centos/python-36-centos7
Using default tag: latest
latest: Pulling from centos/python-36-centos7
75f829a71a1c: Pull complete 
e2c4942f4189: Pull complete 
f1498894b11c: Pull complete 
da56c9694723: Pull complete 
07fa76fc639e: Pull complete 
abab42dfb7f7: Pull complete 
dd73d54fbd62: Pull complete 
25966a5cbd52: Pull complete 
b947aec7d116: Pull complete 
Digest: sha256:ac50754646f0d37616515fb30467d8743fb12954260ec36c9ecb5a94499447e0
Status: Downloaded newer image for centos/python-36-centos7:latest
docker.io/centos/python-36-centos7:latest
[root@jkc docker]# 

 

安装其他python版本

如果有小伙伴想安装其他python版本,比如3.7或者3.8,去https://hub.docker.com搜索即可
centos7.4安装docker_pythondocker
进入后选择tags标签,进入找到想要的版本复制命令即可
centos7.4安装docker_pythondocker

 

docker images

使用docker images查看本地已经下载好的镜像

[root@jkc docker]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
mysql                      5.7                 ae0658fdbad5        2 months ago        449MB
centos/python-36-centos7   latest              602660fa9b4e        4 months ago        650MB
[root@jkc docker]# 

 

运行交互式的容器

Docker会在隔离的容器中运行进程。当运行docker run命令时,Docker会启动一个进程,并为这个进程分配其独占的文件系统、网络资源和以此进程为根进程的进程组。
在容器启动时,镜像可能已经定义了要运行的二进制文件、暴露的网络端口等,但是用户可以通过docker run命令重新定义
最基本的docker run命令的格式如下:

$ sudo docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]

比如我这里要启动centos7,进入交互模式,通过docker的两个参数 -i -t,让docker运行的容器实现”对话”的能力

  • -t: 在新容器内指定一个伪终端或终端。
  • -i: 允许你对容器内的标准输入 (STDIN) 进行交互
  • centos/python-36-centos7:镜像名称
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
docker run -i -t centos/python-36-centos7 /bin/bash

进入centos终端后,进python交互环境打印”hello”,最后用exit退出

[root@jkc docker]# docker run -i -t centos/python-36-centos7 /bin/bash
(app-root) python
Python 3.6.9 (default, Nov 11 2019, 11:24:16) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello
>>> exit()
(app-root) 

 

后台模式启动

run加上-i -t是进交互模式,如果不想进交互模式直接执行脚本,可以直接使用run,比如echo “hello world”,屏幕会输出”hello world”

docker run centos/python-36-centos7 /bin/echo "hello world"

如果不想在前台执行,一般我们运行环境会选择挂后台,加个-d 参数即可

[root@jkc docker]# docker run centos/python-36-centos7 /bin/echo "hello world"
hello world
[root@jkc docker]# docker run -d centos/python-36-centos7 /bin/echo "hello world"
8c7c7449d8596d0d2b7c6d5672ec68d9da73733984232377fb35b30b876faaf5

使用-d后台执行后,会发现下面多了一长串,这个就是容器的唯一id,可以通过这个id找到容器
 

docker ps查看容器

先run下 training/webapp

docker run -d -p 5000:5000 training/webapp python app.py

使用docker ps查看正在运行的容器

[root@jkc docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a67640969dd9        training/webapp     "python app.py"     31 seconds ago      Up 31 seconds       0.0.0.0:5000->5000/tcp   admiring_mahavira
[root@jkc docker]# 

上面的echo “hello world”只是一个很简单的输出指令,执行完就关闭了,所以ps查找正在运行的查不到,可以加个-a参数,显示所有的容器,包括未运行的
ps 查找参数相关语法

  • -a :显示所有的容器,包括未运行的。
  • -f :根据条件过滤显示的内容。
  • –format :指定返回值的模板文件。
  • -l :显示最近创建的容器。
  • -n :列出最近创建的n个容器。
  • –no-trunc :不截断输出。
  • -q :静默模式,只显示容器编号。
  • -s :显示总的文件大小。
docker ps -a
[root@jkc docker]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                      PORTS                    NAMES
a67640969dd9        training/webapp            "python app.py"          2 minutes ago       Up 2 minutes                0.0.0.0:5000->5000/tcp   admiring_mahavira
8c7c7449d859        centos/python-36-centos7   "container-entrypoin…"   4 minutes ago       Exited (0) 4 minutes ago                             nostalgic_mccarthy
9df0d290b4e6        centos/python-36-centos7   "container-entrypoin…"   5 minutes ago       Exited (0) 5 minutes ago                             wonderful_cohen
c01f59e13a3a        centos/python-36-centos7   "container-entrypoin…"   5 minutes ago       Exited (0) 5 minutes ago                             wonderful_hamilton
3c2d40166a8f        centos/python-36-centos7   "container-entrypoin…"   12 minutes ago      Exited (0) 5 minutes ago                             pedantic_engelbart
713e29359fcd        centos/python-36-centos7   "container-entrypoin…"   14 minutes ago      Exited (0) 12 minutes ago                            priceless_allen
29980b073576        mysql:5.7                  "docker-entrypoint.s…"   7 weeks ago         Exited (0) 2 hours ago                               jkcmysql
[root@jkc docker]# 

这样就能找到上面的容器id 8c7c7449d859 ,不过没那么长
 

docker logs查看日志

可以通过容器id去查运行的日志

docker logs [容器id]
[root@jkc docker]# docker logs 8c7c7449d859
hello world
[root@jkc docker]# 

也可以通过容器的名称去查看,注意这里是容器的名称,不是镜像名称。容器名称是系统自动分配的,如上面的最后一项NAMES 值hopeful_poincare

[root@jkc docker]# docker logs nostalgic_mccarthy
hello world

-f: 让 docker logs 像使用 tail -f 一样来输出容器内部的标准输出。

[root@jkc docker]# docker logs -f 8c7c7449d859
hello world

 

停止容器

可以用ps查看正在运行的容器

docker ps
[root@jkc docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a67640969dd9        training/webapp     "python app.py"     9 minutes ago       Up 9 minutes        0.0.0.0:5000->5000/tcp   admiring_mahavira

停止容器的话,可以用docker stop容器的id或者容器NAME名称

docker stop a67640969dd9

或者提供容器名称admiring_mahavira来停止

docker stop admiring_mahavira

 

启动容器

docker start 容器id

[root@jkc docker]# docker start  a67640969dd9
a67640969dd9
[root@jkc docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a67640969dd9        training/webapp     "python app.py"     13 minutes ago      Up 6 seconds        0.0.0.0:5000->5000/tcp   admiring_mahavira

正在运行的容器,可以使用 docker restart 命令来重启

[root@jkc docker]# docker restart  a67640969dd9
a67640969dd9

 

进入容器

在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过以下指令进入:

  • docker attach
  • docker exec:推荐大家使用 docker exec 命令,因为此退出容器终端,不会导致容器的停止。
     

attach 命令

下面演示了使用 docker attach 命令。

docker attach 1e560fca3906 

注意: 如果从这个容器退出,会导致容器的停止。
 

exec 命令

下面演示了使用 docker exec 命令。

[root@jkc easymock]# docker exec -it 25743368a66d /bin/bash
[root@jkc easymock]# docker exec -it 25743368a66d /bin/bash
root@25743368a66d:/opt/webapp# exit
exit
[root@jkc easymock]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
25743368a66d        training/webapp           "python app.py"          4 minutes ago       Up 4 minutes        5000/tcp                 keen_aryabhata

注意: 如果从这个容器退出,容器不会停止,这就是为什么推荐大家使用 docker exec 的原因。

更多参数说明请使用 docker exec –help 命令查看。
 

删除容器

使用 docker rm 命令来删除不需要的容器

[root@jkc docker]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                      PORTS                    NAMES
a67640969dd9        training/webapp            "python app.py"          14 minutes ago      Up About a minute           0.0.0.0:5000->5000/tcp   admiring_mahavira
8c7c7449d859        centos/python-36-centos7   "container-entrypoin…"   17 minutes ago      Exited (0) 17 minutes ago                            nostalgic_mccarthy
9df0d290b4e6        centos/python-36-centos7   "container-entrypoin…"   18 minutes ago      Exited (0) 18 minutes ago                            wonderful_cohen
c01f59e13a3a        centos/python-36-centos7   "container-entrypoin…"   18 minutes ago      Exited (0) 18 minutes ago                            wonderful_hamilton
3c2d40166a8f        centos/python-36-centos7   "container-entrypoin…"   25 minutes ago      Exited (0) 18 minutes ago                            pedantic_engelbart
713e29359fcd        centos/python-36-centos7   "container-entrypoin…"   26 minutes ago      Exited (0) 25 minutes ago                            priceless_allen
29980b073576        mysql:5.7                  "docker-entrypoint.s…"   7 weeks ago         Exited (0) 3 hours ago                               jkcmysql
[root@jkc docker]# docker rm 8c7c7449d859
8c7c7449d859
[root@jkc docker]# docker rm 9df0d290b4e6
9df0d290b4e6
[root@jkc docker]# docker rm admiring_mahavira
Error response from daemon: You cannot remove a running container a67640969dd99d6f26810857d29dd47a43088ea927c03b36dd04dddaf18a4dcf. Stop the container before attempting removal or force remove
[root@jkc docker]# 

但是rm 正在运行的容器时候,它会报错,需先stop,才能rm

[root@jkc docker]# docker stop admiring_mahavira
admiring_mahavira
[root@jkc docker]# docker rm admiring_mahavira
admiring_mahavira
[root@jkc docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@jkc docker]# 

 

删除已经停止运行的容器

如果你跟博主一样有强迫症,在使用docker ps -a发现里面有许多状态为停止的容器,那么可以用下面的命令,将其全部删除

docker container prune
[root@jkc easymock]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS                          PORTS                    NAMES
9e610e19c9c0        training/webapp            "/bin/bash"              About a minute ago   Exited (0) 16 seconds ago                                nervous_robinson
9b977a64610b        training/webapp            "/bin/bash"              About a minute ago   Exited (0) About a minute ago                            jovial_almeida
becfbf291128        training/webapp            "/bin/bash"              2 minutes ago        Exited (0) 2 minutes ago                                 compassionate_germain
25743368a66d        training/webapp            "python app.py"          46 minutes ago       Exited (137) 40 minutes ago                              keen_aryabhata
4d7286571786        redis:4.0.6                "docker-entrypoint.s…"   48 minutes ago       Up 48 minutes                   6379/tcp                 easymock_redis_1
4dc2b090da1b        easymock/easymock:1.6.0    "/bin/bash -c 'npm s…"   48 minutes ago       Up 48 minutes                   0.0.0.0:7300->7300/tcp   easymock_web_1
64cfd7181b19        mongo:3.4.1                "/entrypoint.sh mong…"   48 minutes ago       Up 48 minutes                   27017/tcp                easymock_mongodb_1
713e29359fcd        centos/python-36-centos7   "container-entrypoin…"   24 hours ago         Exited (0) 24 hours ago                                  priceless_allen
29980b073576        mysql:5.7                  "docker-entrypoint.s…"   8 weeks ago          Exited (0) 26 hours ago                                  jkcmysql
[root@jkc easymock]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
9e610e19c9c0389b23b3d4e1bc35023483d8e88ec4733b6f0e639596f02cd246
9b977a64610bf22f051d4bc14c08c430b3508a630cd5d3f69cb228fe368b80a0
becfbf29112833f627e7d1751ef08184ffa5a7f51f38a810281f110813935588
25743368a66d395c2a1204d798ac0e53297ad1d25c13aa5ce0ff409daf62d2bd
713e29359fcd4b2f089df0246b17b24ae0f20a315c81ccbe285e207ff920f8d2
29980b0735760a3c0a878c500b85e91f1d4e804c13d87073e07ace27bdcda7ce

Total reclaimed space: 85B
[root@jkc easymock]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
4d7286571786        redis:4.0.6               "docker-entrypoint.s…"   51 minutes ago      Up 51 minutes       6379/tcp                 easymock_redis_1
4dc2b090da1b        easymock/easymock:1.6.0   "/bin/bash -c 'npm s…"   51 minutes ago      Up 51 minutes       0.0.0.0:7300->7300/tcp   easymock_web_1
64cfd7181b19        mongo:3.4.1               "/entrypoint.sh mong…"   51 minutes ago      Up 51 minutes       27017/tcp                easymock_mongodb_1
[root@jkc easymock]# 

上面留下的都是正在运行的容器状态为up
 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/165296.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号