Docker learning 2- quickly build a centos7-python3.6 environment

Foreword

When we set up a python3.6 environment on a computer, next time we change a computer or switch to a linux system, we have to build it again, set environment variables, download pip and other operations.
After a while, pip.exe could not be found in the Scrips directory, and pip is not an internal or external command, and pip: command not found. If the environment is installed too many, it is a bitter tear.
Setting up an environment has become an obstacle for many small partners to learn. Starting from today to learn docker, I will say goodbye to you for environmental issues~~~

search search mirror

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

For automated builds, you can check 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.5with 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 image centos/python-36-centos7 found above that you want to download, then download it to your local

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
8 ba884070f61: Pull complete 
c3dca185eb14: Pull complete 
ee720ba20823: Pull complete 
497 ef6ea0fac: 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 the locally downloaded image

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 will run processes in isolated containers. When running the docker run command, Docker will start a process and allocate its exclusive file system, network resources, and process group with this process as the root process for this process.
When the container is started, the image may have defined the binary file to be run, the exposed network port, etc., but the user can redefine it through the docker run command
The format of the most basic docker run command is as follows:

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

For example, I want to start centos7 here, enter the interactive mode, and use the two parameters -i -t of docker to enable the container running by docker to achieve the ability to "talk"

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

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

[ root@yoyo ~]# docker run -i -t centos/python-36-centos7 /bin/bash(app-root) python
Python 3.6.3(default, Mar 202018,13:50:41)[GCC 4.8.520150623(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

Start in background mode

Run plus -i -t is to enter the interactive mode. If you don't want to enter the interactive mode to execute the script directly, you can use run directly, such as echo "hello world", 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, generally our operating environment will choose to hang the background, just add a -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"
1 e5c22451bf2215f6c098e066b74363f1db9cde97e9ecea02947ccbbf2fa7e8f
[ root@yoyo ~]#

After using the -d background execution, you will find a long list below, this is the unique id of the container, you can find the container through this id

ps view container

Run training/webapp first

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

Use docker ps to view the running container

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 above echo "hello world" is just a very simple output command, it will be closed after execution, so ps can not find what is running, you can add a -a parameter to display all containers, including those that are not running

ps Find parameter related syntax

docker ps -a

[ root@yoyo ~]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                      PORTS                     NAMES
1 e5c22451bf2        centos/python-36-centos7   "container-entrypoin…"10 minutes ago      Exited(0)10 minutes ago                             hopeful_poincare
1 d14dd77352f        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
9 df329b5effd        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 it is not that long

logs view log

You can check the running log by the container id

docker logs [container id]

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

You can also view it by the name of the container. Note that this is the name of the container, not the image name. The container name is automatically assigned by the system, such as the last NAMES value hopeful_poincare above

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

**- f: Let docker logs output the standard output inside the container like tail -f. **

docker logs -f 1e5c22451bf2

Stop the container

You can use ps to view the running container

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

To stop the container, you can use the id of the stop container or the name of the container

docker stop c9e8a325b145

Or provide the container name kind_kirch to stop

docker stop kind_kirch

Start the container

Provide start to start the 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 ~]#

The running container can be restarted with the docker restart command

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

Delete container

Use the docker rm command to delete unnecessary containers

[ root@yoyo ~]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                      PORTS                     NAMES
1 e5c22451bf2        centos/python-36-centos7   "container-entrypoin…"24 minutes ago      Exited(0)24 minutes ago                             hopeful_poincare
1 d14dd77352f        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
9 df329b5effd        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
1 e5c22451bf2
[ root@yoyo ~]# docker rm 1d14dd77352f
1 d14dd77352f
[ 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 ~]#

But when rm is running in the container, it will report an error, it needs to stop before rm

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

Recommended Posts

Docker learning 2- quickly build a centos7-python3.6 environment
Build docker environment under Centos6.5
Ubuntu18.04LTS quickly build CUDA environment
Build a LAMP development environment on Ubuntu 16.04
(1) Centos7 installation to build a cluster environment
[PHP] Build a PHP operating environment under CentOS
Build a python development environment under Ubuntu
Python3 development environment to build a detailed tutorial
How to build a LAMP environment on centos7.2
Build a basic environment for Java development under Centos7
CentOS6.7 build LNMP environment
Centos7.6 build LNMP environment
CentOS 7 build LNMP environment