docker3 基础命令

创建镜像

创建镜像的方法有三种:

  • 基于已有的容器创建
  • 基于本地模板导入
  • 基于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_copy
5d318afa9e6f7fdb755db97e29e3860b752f24b0b50e6bfa0b7e457450802c0e
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_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 images

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容器

# docker run centos echo "hello world"
Unable to find image '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 image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide 
security.
Digest: sha256:3aaab9f1297db9b013063c781cfe901e2aa6e7e334c1d1f4df12f25ce356f2e5
Status: Downloaded newer image for centos:latest
hello 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; 
179fc7f17c358834364d23112aa26d6a9e1875b2281563720425f62a8f1b5c33

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

dock logs 179fc7f17c358834364d

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

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

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

  

更多命令操作

Docker help

查看docker的子命令,直接敲docker或完整的docker help就可以了:

[root@VM_27_98_centos ~]# docker help

Commands:

    attach    Attach to a running container

    build     Build a container from a Dockerfile

    commit    Create a new image from a container's changes

    cp        Copy files/folders from the containers filesystem to the host path

    diff      Inspect changes on a container's filesystem

    events    Get real time events from the server

    export    Stream the contents of a container as a tar archive

    history   Show the history of an image

    images    List images

    import    Create a new filesystem image from the contents of a tarball

    info      Display system-wide information

    inspect   Return low-level information on a container

    kill      Kill a running container

    load      Load an image from a tar archive

    login     Register or Login to the docker registry server

    logs      Fetch the logs of a container

    port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT

    ps        List containers

    pull      Pull an image or a repository from the docker registry server

    push      Push an image or a repository to the docker registry server

    restart   Restart a running container

    rm        Remove one or more containers

    rmi       Remove one or more images

    run       Run a command in a new container

    save      Save an image to a tar archive

    search    Search for an image in the docker index

    start     Start a stopped container

    stop      Stop a running container

    tag       Tag an image into a repository

    top       Lookup the running processes of a container

    version   Show the docker version information

    wait      Block until a container stops, then print its exit code

常用命令

总结一下常用命令:

  • docker version 查看docker的版本号,包括客户端、服务端、依赖的Go等
  • docker info 查看系统(docker)层面信息,包括管理的images, containers数等
  • docker search <image> 在docker index中搜索image
  • docker pull <image> 从docker registry server 中下拉image
  • docker push <image|repository> 推送一个image或repository到registry
  • docker push <image|repository>:TAG 同上,指定tag
  • docker inspect <image|container> 查看image或container的底层信息
  • docker images TODO filter out the intermediate image layers (intermediate image layers 是什么)
  • docker images -a 列出所有的images
  • docker ps 默认显示正在运行中的container
  • docker ps -l 显示最后一次创建的container,包括未运行的
  • docker ps -a 显示所有的container,包括未运行的
  • docker logs <container> 查看container的日志,也就是执行命令的一些输出
  • docker rm <container...> 删除一个或多个container
  • docker rm `docker ps -a -q` 删除所有的container
  • docker ps -a -q | xargs docker rm 同上, 删除所有的container
  • docker rmi <image...> 删除一个或多个image
  • docker start/stop/restart <container> 开启/停止/重启container
  • docker start -i <container> 启动一个container并进入交互模式
  • docker attach <container> attach一个运行中的container
  • docker run <image> <command> 使用image创建container并执行相应命令,然后停止
  • docker run -i -t <image> /bin/bash 使用image创建container并进入交互模式, login shell是/bin/bash
  • docker run -i -t -p <host_port:contain_port> 将container的端口映射到宿主机的端口
  • docker commit <container> [repo:tag] 将一个container固化为一个新的image,后面的repo:tag可选
  • docker build <path> 寻找path路径下名为的Dockerfile的配置文件,使用此配置生成新的image
  • docker build -t repo[:tag] 同上,可以指定repo和可选的tag
  • docker build - < <dockerfile> 使用指定的dockerfile配置文件,docker以stdin方式获取内容,使用此配置生成新的image
  • docker port <container> <container port> 查看本地哪个端口映射到container的指定端口,其实用docker ps 也可以看到

具体实例

  

docker images      

列出本地所有镜像,这些都是使用Dockerfile构建的镜像

 [root@VM_159_91_centos ~]# docker images

REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

pt/test                             v1                  2bfc44a4ad8f        4 days ago          196.5 MB

pt/tomcat_smq                       v1                  987073773bfd        5 days ago          754.9 MB

pt/tomcat_msg                       v1                  e82f90cf31c9        5 days ago          754.9 MB

centos                              latest              97cad5e16cb6        2 weeks ago         196.5 MB

registry                            latest              5c929a8b587a        4 weeks ago         33.27 MB

google/cadvisor                     latest              089623f9fe9e        6 weeks ago         47.79 MB

 

docker ps –a     

列出创建的所有容器 可以看到他们的状态都是exited,说明这些容器都是关闭状态,没有启动

[root@VM_159_91_centos ~]# docker ps -a

CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                      PORTS                              NAMES

7f76edae027d        pt/test:v1               "/bin/bash"            4 days ago          Exited (137) 4 days ago                                        jolly_torvalds     

05cf1c1dbf71        pt/tomcat_smq:v1         "/usr/local/smq-tomc   5 days ago          Exited (143) 20 hours ago                                      smq                

1c296658025a        pt/tomcat_msg:v1         "/usr/local/msg-tomc   5 days ago          Exited (143) 4 days ago                                        msg                

 

 
docker start 05cf1c  

启动容器,start后面跟的是容器ID,状态变成了up,且映射了一个端口,把容器里面的8080端口映射到宿主机的6180上面

[root@VM_159_91_centos ~]# docker start 05cf1c1dbf71

05cf1c1dbf71

[root@VM_159_91_centos ~]# docker ps -a

CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                      PORTS                              NAMES

7f76edae027d        pt/test:v1               "/bin/bash"            4 days ago          Exited (137) 4 days ago                                        jolly_torvalds     

05cf1c1dbf71        pt/tomcat_smq:v1         "/usr/local/smq-tomc   5 days ago          Up 28 seconds               0.0.0.0:6180->8080/tcp             smq      

 

docker stop 05cf1c   

关闭容器

[root@VM_159_91_centos ~]# docker stop 05cf1c1dbf71

05cf1c1dbf71

[root@VM_159_91_centos ~]# docker ps -a

CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                       PORTS                              NAMES

7f76edae027d        pt/test:v1               "/bin/bash"            4 days ago          Exited (137) 4 days ago                                         jolly_torvalds     

05cf1c1dbf71        pt/tomcat_smq:v1         "/usr/local/smq-tomc   5 days ago          Exited (143) 3 seconds ago                                      smq  

 

tag做标记

docker tag pt/tomcat_search:v1 IP:5000/pt/tomcat_search:v1

[root@VM_159_91_centos ~]# docker tag pt/tomcat_search:v1 IP:5000/pt/tomcat_search:v1     (ip填写自己的地址)

[root@VM_159_91_centos ~]# docker images

REPOSITORY                            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

pt/tomcat_search                      v1                  b5681406505f        6 days ago          755.2 MB

IP/pt/tomcat_search   v1                  b5681406505f        6 days ago          755.2 MB

   

push推送到镜像仓库

docker push IP:5000/pt/tomcat_search:v1   IP替换成自己的真实IP

[root@VM_159_91_centos ~]# docker push IP:5000/pt/tomcat_search:v1  

The push refers to a repository [IP:5000/pt/tomcat_search] (len: 1)

b5681406505f: Image already exists

4056159949ab: Image already exists

e9970493708d: Image already exists

a06c57a073b3: Image already exists

84551ae56dd4: Image already exists

49e5958f14aa: Image already exists

b484689c1f77: Image already exists

a336ec9f22dc: Image already exists

31f25b288c4a: Image already exists

65d689569b3f: Image already exists

411a7bdc8e8d: Image already exists

c863857a498b: Image already exists

97cad5e16cb6: Image already exists

05fe84bf6d3f: Image already exists

af0819ed1fac: Buffering to Disk

af0819ed1fac: Image successfully pushed

3690474eb5b4: Image already exists

Digest: sha256:e7d15d45fa95a9d0b8d2e7efbecfdce262f563e91a3af4a738970d55bbe69b55

 

Docker pull

在另外一台机器就可以pull下来刚才上传到镜像仓库里面的镜像

登录另外一台服务器

#docker pull IP:5000/pt/tomcat_search:v1

[root@VM_27_98_centos ~]# docker pull 10.104.159.91:5000/pt/tomcat_search:v1   

v1: Pulling from IP:5000/pt/tomcat_search

411a7bdc8e8d: Pull complete

65d689569b3f: Pull complete

31f25b288c4a: Pull complete

a336ec9f22dc: Pull complete

b484689c1f77: Pull complete

49e5958f14aa: Pull complete

84551ae56dd4: Pull complete

a06c57a073b3: Pull complete

e9970493708d: Pull complete

4056159949ab: Pull complete

b5681406505f: Already exists

3690474eb5b4: Already exists

af0819ed1fac: Already exists

05fe84bf6d3f: Already exists

97cad5e16cb6: Already exists

c863857a498b: Already exists

Digest: sha256:e7d15d45fa95a9d0b8d2e7efbecfdce262f563e91a3af4a738970d55bbe69b55

Status: Downloaded newer image for IP:5000/pt/tomcat_search:v1

#docker pull centos  下载镜像到本地
#docker create -it  ubuntu:latest  创建一个容器 
Unable to find image '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 image for ubuntu:latest
1330233e50aba7fca99e5914bd28dd89321bc86ec35fb36b4775d3424337c190
#docker start 1330233e


 进入容器

方法一:

# 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


脚本
#!/bin/bash CNAME=$1 CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME) nsenter --target $CPID --mount --uts --ipc --net –pid

 2016-10-28 01:00:26 

  

原文地址:https://www.cnblogs.com/kafeikele/p/6006292.html