docker container ls docker container ls -- all docker container ls -aq

#停止一个容器
docker container stop [CONTAINER ID]

通过修改容器来创建一个新镜像 (python2.7.5 + node.js11.10.0 + flask +项目应用demo)

步骤一:下载基础镜像----只包含centos的镜像(这个镜像里自带python2.7.5)

#查看官方https://hub.docker.com已有的centos镜像
docker search centos
#选择合适自己镜像拉取到本地
docker pull centos
[root@izwz92fbqxna6qt4ya9t4nz soft]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5205                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              120                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   106                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   80                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              50                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   47                              
[root@izwz92fbqxna6qt4ya9t4nz soft]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Image is up to date for centos:latest

步骤二:使用镜像centos创建并以后台模式运行该容器(不用-dit的话,运行之后容器状态不是up,注意-d和-dit不是一个命令)

docker run -dit centos

步骤三:找到容器并切入容器里

#查看运行中的容器
docker ps
#查看所有容器
docker ps -a
#切进容器
docker attach [CONTAINER ID 或 wizardly_kirch]
docker exec -it [CONTAINER ID] /bin/bash
#退出容器(退出之后容器也停止了)
exit
#退出容器(退出之后容器还在运行中)
先ctrl+p 再 ctrl+q
[root@izwz92fbqxna6qt4ya9t4nz soft]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                    NAMES
8bf91340323d        centos              "/bin/bash"              3 minutes ago       Up 3 minutes                                           wizardly_kirch
8c5d1bb19a3e        centos              "/bin/bash"              12 minutes ago      Exited (0) 3 minutes ago                               reverent_curran
[root@izwz92fbqxna6qt4ya9t4nz soft]# docker attach 8bf91340323d
[root@8bf91340323d /]# 

有的镜像我直接使用docker attach命令可以正常切入容器中,有时候切进去又无反应,但是使用docker exec -it可以正常切入

[root@localhost docker]# docker attach bfbcca966a49
read escape sequence
[root@localhost docker]# docker exec -it bfbcca966a49 /bin/bash
jenkins@bfbcca966a49:/$ 

ps:查看linux版本发现是centos7.6,python2.7.5

[root@8bf91340323d /]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core) 
[root@8bf91340323d /]# python  
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

步骤四:在该容器里安装和配置所需的环境

下载node.js的二进制包进行解压安装

#从主机拷贝到容器的指定目录
docker cp 要拷贝的文件路径 容器名:要拷贝到容器里面对应的路径
#从容器拷贝到主机 docker
cp 容器名:要拷贝到容器里面对应的路径 要拷贝的文件路径

安装pip和flask,将项目代码也复制到容器中

步骤五:从修改后的容器导出新的镜像centos:node_flask(镜像名:标签)

[root@izwz92fbqxna6qt4ya9t4nz soft]# docker commit 541aff7533d9 centos:node_flask
sha256:724f235e44ba18927651903e68f6802a455aa9b07b823acf48eb50865b8cce4a
[root@izwz92fbqxna6qt4ya9t4nz soft]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              node_flask          724f235e44ba        8 seconds ago       529MB
docker              latest              73d492654a09        13 days ago         170MB
hello-world         latest              fce289e99eb9        7 weeks ago         1.84kB
centos              latest              1e1148e4cc2c        2 months ago        202MB
[root@izwz92fbqxna6qt4ya9t4nz soft]# docker rmi docker