前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >docker-3 基础命令

docker-3 基础命令

作者头像
py3study
发布2020-01-14 15:47:47
3110
发布2020-01-14 15:47:47
举报
文章被收录于专栏:python3python3

创建镜像

创建镜像的方法有三种:

  • 基于已有的容器创建
  • 基于本地模板导入
  • 基于dockerfile

基于已有的容器创建

主要使用docker commit 命令,命令格式:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:tag]],主要包括:

-a ,--author=""  作者信息

-m,--message=""提交消息

-p,--pause=true 提交时暂停容器

例如:

代码语言:javascript
复制
# docker run -it centos /bin/bash[root@d7e7ac1cbca2 /]# touch test
[root@d7e7ac1cbca2 /]# ls 
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test tmp usr var
# docker commit -m "add a file" -a "kafeikele" de6 centos_copy
5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e
# docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB

基于本地模板导入

推荐使用openVZ提供的模板来创建

https://openvz.org/Download/templates/precreated

代码语言:javascript
复制
#cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import - centos:latest

存出和导入镜像

代码语言:javascript
复制
# docker p_w_picpaths
centos   7.1.1503      47a77536ad4c        8 weeks ago         212.1 MB
# docker save -o centos_7.1.tar centos:7.1.1503 
# docker load --input centos_7.1.tar
# docker load < centos_7.1.tar

基于dockerfile

之后的内容详细介绍

运行第一个docker容器

代码语言:javascript
复制
# docker run centos echo "hello world"
Unable to find p_w_picpath 'centos:latest' locally
latest: Pulling from centos
47d44cb6f252: Pull complete
168a69b62202: Pull complete
812e9d9d677f: Pull complete
4234bfdd88f8: Pull complete
ce20c473cd8a: Pull complete
centos:latest: The p_w_picpath you are pulling has been verified. Important: p_w_picpath verification is a tech preview feature and should not be relied on to provide 
security.
Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5
Status: Downloaded newer p_w_picpath for centos:latest
hello world

命令说明:

docker run :标准容器启动命令

centos: 镜像名称,默认是latest

echo和后面的内容:容器启动后执行的命令

启动一个交互式容器

代码语言:javascript
复制
docker run -it centos /bin/bash

*注:-t标示在心容器内指定一个伪终端或终端,-i标示允许我们对容器内的STDIN进行交互

以服务方式启动一个docker容器

如果你实际测试,估计也发现了,第一个“hello world”容器启动后执行完echo命令就退出了,而第二个交互式的容器,只要用户退出当前容器的bash,容器也退出了。这明显不能满足一个服务长时间运行的要求,好找docker run提供了‘-d’参数,可以实现将容器以守护进程方式启动。

代码语言:javascript
复制
docker  run -d  centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; 
179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33

这个长的字符串叫做容器ID。它是容器的唯一标识,所以我们可以使用它来操作容器,比如查看日志、停止或删除容器等。

代码语言:javascript
复制
dock logs 179fc7f17c358834364d

而为什么使用一个死循环来输出呢?

因为如果不是死循环,一次输出后,容器中的进程就结束了。容器的唯一进程都结束了,容器就停止了。因此如果要在容器中运行具体的服务,这项服务本身在容器中也必须是已守护进程方式运行的。

代码语言:javascript
复制
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
主要选项:
-d : 以后台进行方式运行容器
-t : 提供一个伪终端
-i : 提供交互输入,一般与“-t”一起使用,如果只提供“-i”选项,则容器启动后是无法退出的
-v : 映射一个volume给容器,如: -p /data/www:/var/www/html
-p : 将容器的端口映射给宿主机,如: -p 8080:80

更多命令操作

代码语言:javascript
复制
# docker p_w_picpaths 列出本地所有镜像
 
# docker search centos  从默认镜像仓库搜索镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2767 [OK] 
ansible/centos7-ansible Ansible on Centos7 90 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 42 [OK]
jdeathe/centos-ssh-apache-php CentOS-6 6.8 x86_64 - Apache / PHP / PHP M... 21 [OK]
nimmis/java-centos This is docker p_w_picpaths of CentOS 7 with dif... 17 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 14 [OK]

#docker pull centos  下载镜像到本地

#docker create -it  ubuntu:latest  创建一个容器 
Unable to find p_w_picpath 'ubuntu:latest' locally
latest: Pulling from ubuntu
58488e45273c: Pull complete 
25810b66099e: Pull complete 
6571ba684f54: Pull complete 
6ed49a73d8f0: Pull complete 
c53777cbfc31: Pull complete 
56465e1e45d2: Pull complete 
Digest: sha256:312986132029d622ae65423ca25d3a3cf4510de25c47b05b6819d61e2e2b5420
Status: Downloaded newer p_w_picpath for ubuntu:latest
1330233e50aba7fca99e5914bd28dd89321bc86ec35fb36b4775d3424337c190
#docker start 1330233e

docker create 命令创建的容器处于停止状态,需要用docker start 把它启动
docker run 命令等价于先执行docker create命令, 然后再执行docker start 命令
# docker run ubuntu /bin/echo "hello world"hello world
代码语言:javascript
复制
# docker ps -a   列出所有的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1330233e50ab ubuntu:latest "/bin/bash" About a minute ago happy_engelbart
#docker ps -l  列出最新创建的容器

#docker stop   1330233e 停止一个容器,和start相反,133023是容器ID

#docker rm 1330233e  删除一个停止的容器
#docker rm -f 1330233e  强制删除一个容器,没有停止也可以删掉,不建议使用

#docker rmi 镜像名   删除一个镜像

 进入容器

方法一:

# docker attach a54615a88787    后面跟的是容器名或者id,退出后docker容器也退出,不常用

方法二:

# docker exec -it a54615a88787 /bin/bash    跟的是容器名或者id

方法三:

yum -y install util-linux

# docker inspect --format "``.`State`.`Pid`" stupefied_cray  最后面跟的是容器的名称 4899 # nsenter --target 4899 --mount --uts --ipc --net --pid

脚本

代码语言:javascript
复制
#!/bin/bash

CNAME=$1

CPID=$(docker inspect --format "``.`State`.`Pid`" $CNAME)

nsenter --target $CPID --mount --uts --ipc --net –pid
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建镜像
    • 基于已有的容器创建
    • 基于本地模板导入
    • 存出和导入镜像
    • 基于dockerfile
    • 运行第一个docker容器
    • 启动一个交互式容器
    • 以服务方式启动一个docker容器
    • 更多命令操作
      •  进入容器
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档