首页 > 网站 > 建站经验 > 正文

Docker基础命令详解

2019-11-02 16:23:01
字体:
来源:转载
供稿:网友

docker基本概念

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上。

Docker是一个重新定义了程序开发测试、交付和部署过程的开放平台,Docker则可以称为构建一次,到处运行,这就是docker提出的“Build once,Run anywhere”

创建镜像

创建镜像的方法有三种:

基于已有的容器创建

基于本地模板导入

基于dockerfile

基于已有的容器创建

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

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

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

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

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

例如:

# 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_copy5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEcentos_copy latest 5d318afa9e6f 13 seconds ago 196.7 MB

基于本地模板导入

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

https://openvz.org/Download/templates/precreated#cat centos-7-x86_64-minimal.tar.gz.crdownload | docker import - centos:latest

存出和导入镜像

# docker imagescentos 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容器

# docker run centos echo "hello world"Unable to find image 'centos:latest' locallylatest: Pulling from centos47d44cb6f252: Pull complete168a69b62202: Pull complete812e9d9d677f: Pull complete4234bfdd88f8: Pull completece20c473cd8a: Pull completecentos:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to providesecurity.Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5Status: Downloaded newer image for centos:latesthello world

命令说明:

docker run :标准容器启动命令

centos: 镜像名称,默认是latest

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

启动一个交互式容器

docker run -it centos /bin/bash

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

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

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

docker run -d centos /bin/bash -c "while true; do echo Docker,hello world; sleep 2; <br>179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表