docker learning 2-Quickly build centos7-python 3.6 environment

Preface

When we set up a python 3.6 environment on one computer, the next time we switch to a computer or a linux system, we have to rebuild it again, set up environment variables, download pip and so on.
Very easy to install, after a while the Scrips directory can not find pip.exe, one will prompt that pip is not an internal or external command, another will prompt that pip: command is not found, you want to die.
Setting up an environment has become a barrier for many small partners to learn. Start learning docker well today and say goodbye to you about the environment problem ~~

search Search Mirror

docker search: from Docker Hub( https://hub.docker.com ) Search for the specified image
For example, here I search for a version of Python 3.6 installed on centos7

  • NAME Mirror Repository Name
  • DESCRIPTION Mirror Description Information
  • Number of STARS Mirror Collections
  • Is OFFICIAL an official docker image
  • Whether AUTOMATED is an automated built image

For automated builds, you can view the official documentation: https://docs.docker.com/docker-hub/builds/#how-automated-builds-work

docker search python

[root@yoyo ~]# 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                                       
[root@yoyo ~]# 

pull download mirror

The mirror centos/python-36-centos7 found above that you want to download, then download it locally

docker pull centos/python-36-centos7

[root@yoyo ~]# 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
[root@yoyo ~]# 

images Local Mirror View

Use docker images to view locally downloaded images

docker images

[root@yoyo ~]# 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

Run an interactive container

Docker runs the process in an isolated container.When the docker run command is run, Docker starts a process and assigns its exclusive file system, network resources, and process groups that use the process as the root process.
At container startup, the mirror may have defined binaries to run, exposed network ports, etc., but the user can redefine them through the docker run command
The most basic docker run command is formatted as follows:

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

For example, I'm going to start centos7, go into interactive mode, and let the container where docker runs implement the ability to "talk" through its two parameters-i-t

  • t: Specify a pseudo terminal or terminal within the new container.
  • i: Allows you to interact with standard input (STDIN) in a container

docker run -i -t centos/python-36-centos7 /bin/bash

After entering the centos terminal as follows, enter the python interactive environment to print "hello world! I'm comming!!!" and exit with exit

[root@yoyo ~]# 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

Background mode start

Run plus -i-t is in interactive mode. If you don't want to execute scripts directly in interactive mode, you can use run directly, such as echo "hello world", and the screen will output "hello world"

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

If you don't want to execute in the foreground, we usually choose to hang in the background, add -d parameter.

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

[root@yoyo ~]# docker run centos/python-36-centos7 /bin/echo "hello world"
hello world
[root@yoyo ~]# docker run -d centos/python-36-centos7 /bin/echo "hello world"
1e5c22451bf2215f6c098e066b74363f1db9cde97e9ecea02947ccbbf2fa7e8f
[root@yoyo ~]# 

After executing in the -d background, you will find a long string below, which is the unique id of the container through which you can find the container

ps View Container

run down training/webapp first

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

Use docker ps to view running containers

docker ps

[root@yoyo ~]# 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
[root@yoyo ~]# 

The echo "hello world" above is just a simple output command that closes when executed, so the ps lookup cannot find what is running. You can add a -a parameter to show all containers, including those that are not running

ps Find Parameter Related Syntax

  • -a: Displays all containers, including those not running.
  • -f: Filter the displayed content according to the conditions.
  • --format: A template file specifying the return value.
  • -l: Displays containers created recently.
  • -n: Lists n containers recently created.
  • --no-trunc: Do not truncate the output.
  • -q: Quiet mode, showing only container number.
  • -s: Displays the total file size.

docker ps -a

[root@yoyo ~]# 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
[root@yoyo ~]# 

This will find the container id 1e5c22451bf2 above, but not that long

logs View Log

You can look up the running log through the container id

docker logs [container id]

[root@yoyo ~]# docker logs 1e5c22451bf2 
hello world
[root@yoyo ~]# 

You can also look at the name of the container, noting that this is the name of the container, not the mirror name.Container names are automatically assigned by the system, such as hopeful_poincare, the last NAMES value above

[root@yoyo ~]# docker logs hopeful_poincare
hello world

-f: Let docker logs output the standard output inside the container as tail-f does.

docker logs -f 1e5c22451bf2

Stop Container

You can view running containers with ps

docker ps

[root@yoyo ~]# 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 the container with the stop container id or container NAME name

docker stop c9e8a325b145

Or provide the container name type_kirch to stop

docker stop kind_kirch

Start Container

Provide start startup container

[root@yoyo ~]# docker start c9e8a325b145
c9e8a325b145
[root@yoyo ~]# 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
[root@yoyo ~]# 

A running container that can be restarted using the docker restart command

[root@yoyo ~]# docker restart c9e8a325b145
c9e8a325b145
[root@yoyo ~]# 

Delete Container

Use docker rm command to remove unneeded containers

[root@yoyo ~]# 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
[root@yoyo ~]# docker rm 1e5c22451bf2
1e5c22451bf2
[root@yoyo ~]# docker rm 1d14dd77352f
1d14dd77352f
[root@yoyo ~]# docker rm kind_kirch
Error response from daemon: You cannot remove a running container c9e8a325b14534f0b27cfd34e3ceefd16f6a6c9f136c0305d4e60de61f2badc3. Stop the container before attempting removal or force remove
[root@yoyo ~]# 

However, when rm is running on a container, it will fail and need to stop before rm can work

[root@yoyo ~]# docker stop kind_kirch
kind_kirch
[root@yoyo ~]# docker rm  kind_kirch
kind_kirch
[root@yoyo ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@yoyo ~]# 

Keywords: PHP Python Docker CentOS pip

Added by pelleas on Tue, 25 Jun 2019 19:41:30 +0300