• Docker 常用命令


     

    本文URL: http://www.cnblogs.com/go2bed/p/5703117.html

    官方文档: https://docs.docker.com/

    搜索镜像

    镜像可以去 https://hub.docker.com/ 搜索,或者使用命令

    如:

    user@ubuntu:~$ sudo docker search debian

    NAME

     DESCRIPTION

    STARS

    OFFICIAL

     AUTOMATED

    debian

     Debian is a Linux distribution that's comp...

    1519

    [OK]

    neurodebian

    NeuroDebian provides neuroscience research...

    25

    [OK]

    jesselang/debian-vagrant

     Stock Debian Images made Vagrant-friendly ...

     8

    [OK]

    armbuild/debian

     ARMHF port of debian

    8

    [OK]

    docker hub 几个镜像:

    nickistre/ubuntu-lamp

    https://hub.docker.com/r/nickistre/ubuntu-lamp/~/dockerfile/

    php

    https://hub.docker.com/_/php/

    mysql

    https://hub.docker.com/_/mysql/

    下载镜像

    sudo docker pull ubuntu

    显示本地镜像

    user@ubuntu:~$ sudo docker images

    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

    ubuntu              latest              4ef6a5ece191        3 days ago          120.1 MB

    reinblau/lamp       latest              e9df29833f32        9 days ago          703.8 MB

    运行镜像,映射端口

    开启lamp镜像,映射主机8080端口到容器80端口

    e9d是 镜像id或镜像名

    user@ubuntu:~$ sudo docker run -it -p 8080:80 e9d apache2

    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.1. Set the 'ServerName' directive globally to suppress this message

    user@ubuntu:~$ sudo docker run -it -p 8080:80 e9d /bin/bash

    root@ac4c74c9ac8a:/var/www/html# apache2

    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

    在后台运行,启动后容器内自动运行 /root/run.sh

    sudo docker run -itd -p 8080:80 e9d /root/run.sh

    参数:

    加上 -i -t 可以 ctrl+p ctrl+q 退出。

    -d, --detach=false         Run container in background and print container ID

    -i, --interactive=false    Keep STDIN open even if not attached

    -P, --publish-all=false    Publish all exposed ports to random ports

    -p, --publish=[]           Publish a container's port(s) to the host

    -t, --tty=false            Allocate a pseudo-TTY 分配一个伪TTY?

    依附到运行中的容器

    ac4c 是容器号

    sudo docker attach ac4c

    退出,一定不要用ctrl+c,那样就是让docker容器停止了。

    要用如下快捷键:先按,ctrl+p;再按,ctrl+q

    列出容器

    user@ubuntu:~$ sudo docker ps -a

    CONTAINER ID

    IMAGE

    COMMAND

    CREATED

    STATUS

    PORTS

    NAMES

    ac4c74c9ac8a

    e9d:latest

    "/bin/bash"

    7 minutes ago

    Up 7 minutes

    0.0.0.0:8080->80/tcp

    insane_mayer 

    3a4b37b41ea7

    e9d:latest

    "apache2"

    7 minutes ago

    Exited (0) 7 minutes ago 

    suspicious_darwin

    参数

    -a, --all=false       Show all containers (default shows just running)

    -q, --quiet=false     Only display numeric IDs

    查看端口映射

    ac4c是容器id

    user@ubuntu:~$ sudo docker port ac4c

    80/tcp -> 0.0.0.0:8080

    从dockerfile 创建镜像

    sudo docker build -t mylamp/test /home/user/Docker/mylamp_test/

    上例中,dockerfile存在于 /home/shen/Docker/mylamp_test/,镜像tag为mylamp/test

    参数

     -t, --tag=            Repository name (and optionally a tag) for the image

    删除镜像

    先删除所有依赖容器,再删除镜像。

    后面跟上标签或ID,跟标签会先删除标签(untag),如果没有标签指向镜像,就删除(delete)镜像。

    跟ID,删除所有相关标签(untag),再删除(delete)镜像。

    sudo docker rmi 2318

    sudo docker rmi ubuntu

    删除容器

    sudo docker rm e81

    批量操作容器

    停止正在运行的容器

    sudo docker stop $(sudo docker ps -q)

    删除(已经停止的,正在运行的不能删除)容器

    sudo docker rm $(sudo docker ps -a -q)



    文章出处:博客园http://www.cnblogs.com/go2bed/
    本文采用 知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议 进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
     
    新增:

    一执行以下命令,可以批量删除 <none> 的镜像,但是前提是必须保证这些镜像没有引用的容器。

    docker images | grep none | awk '{print $3}' | xargs docker rmi

    但是暂时好像没有什么好的办法可以通过命令删除 <none> 的镜像容器,同时删除镜像。

    二简化命令

    打开当前用户的 .bashrc

    vim ~/.bashrc

    添加如下一行, ddnone 可以你自己随便起。

    alias ddnone="docker images | grep none | awk '{print $3}' |xargs docker rmi"

    退出终端别忘记执行 source ~/.bashrc

    三 用 commit 命令创建镜像

    运行 docker commit 命令:

    $  exit
    $ sudo docker commit eafd9111ada6 docker/lua
    b1829eb2e483c9d353dd8cd21da9bf2f530bd38ccf5a95e08944a887ec856e99

    命令中,指定了要提交的修改过的容器的ID、目标镜像仓库、镜像名。commit提交的知识创建容器的镜像与容器的当前状态之间的差异部分,很轻量。

    查看新创建的镜像:

    $ sudo docker images docker/lua
    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    docker/lua          latest              b1829eb2e483        25 seconds ago      301.5 MB





  • 相关阅读:
    页面头部出现空白,页面头部出现 隐藏字符
    使用TortoiseGit对Git版本进行分支操作
    文件夹添加右键DOS快捷入口
    android离线安装adt
    V9最新手机门户域名绑定教程。
    phpcms v9 模板标签说明整理
    CAM350 10.5移动和叠层的用法
    MOS简单应用
    c语言知识点总结-------静态区、堆、栈、常量区等
    C语言关键字—-sizeof 、typedef、const、static、register、extern、#define
  • 原文地址:https://www.cnblogs.com/HendSame-JMZ/p/6069599.html
Copyright © 2020-2023  润新知