docker 常用命令

点击个人博客,查看更多文章:https://elonjelinek.github.io

列出已经下载的镜像:

docker image ls

ElonJelinek:~ ElonJelinek$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c82521676580        7 weeks ago         109MB
hello-world         latest              2cb0d9787c4d        2 months ago        1.85kB
ElonJelinek:~ ElonJelinek$ 
查看镜像、容器、数据卷所占用的空间:

docker system df

ElonJelinek:~ ElonJelinek$ docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              2                   1                   109MB               109MB (99%)
Containers          2                   0                   0B                  0B
Local Volumes       0                   0                   0B                  0B
Build Cache         0                   0                   0B                  0B
ElonJelinek:~ ElonJelinek$ 
显示虚悬镜像:

docker image ls -f dangling=true

ElonJelinek:~ ElonJelinek$ docker image ls -f dangling=true
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ElonJelinek:~ ElonJelinek$ 
显示包括中间层的所有镜像:

docker image ls -a

ElonJelinek:~ ElonJelinek$ docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c82521676580        7 weeks ago         109MB
hello-world         latest              2cb0d9787c4d        2 months ago        1.85kB
ElonJelinek:~ ElonJelinek$ 

定制一个web服务器,用nginx镜像启动一个容器,命名为:webserver,并映射80端口。然后在浏览器输入:localhost,查看效果。再修改欢迎页面的文字,用docker exec命令进入容器,修改内容为:hello docker!。然后用浏览器访问localhost查看效果。

ElonJelinek:~ ElonJelinek$ docker run --name webserver -d -p 80:80 nginx
74ddf8bb271e826dc814459bd8de9c5c9dd6f53de14a86bb6f52420712176355
ElonJelinek:~ ElonJelinek$ docker exec -it webserver bash
root@74ddf8bb271e:/# echo '<h1>Hello,Docker!</h1>' > /usr/share/nginx/html/index.html
root@74ddf8bb271e:/# exit
exit
ElonJelinek:~ ElonJelinek$ 

查看具体的改动

ElonJelinek:~ ElonJelinek$ docker diff webserver
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
C /root
A /root/.bash_history
C /usr
C /usr/share
C /usr/share/nginx
C /usr/share/nginx/html
C /usr/share/nginx/html/index.html
C /run
A /run/nginx.pid
ElonJelinek:~ ElonJelinek$ 

将定制好的变化,保存下来形成镜像:

ElonJelinek:~ ElonJelinek$ docker commit \
> --author "ElonJelinek <Elon@126.com>" \
> --message "修改了默认网页" \
> webserver \
> nginx:v2
sha256:d584cfbd53522c9d73ead4f82a57a1c62f54699085574780c19f13a3362d4bb6
ElonJelinek:~ ElonJelinek$ 

查看新定制的镜像:

ElonJelinek:~ ElonJelinek$ docker image ls nginx
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
nginx               v2                  d584cfbd5352        About a minute ago   109MB
nginx               latest              c82521676580        7 weeks ago          109MB
ElonJelinek:~ ElonJelinek$ 

查看镜像内的历史记录:

ElonJelinek:~ ElonJelinek$ docker history nginx:v2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d584cfbd5352        3 minutes ago       nginx -g daemon off;                            95B                 修改了默认网页
c82521676580        7 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           7 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B                  
<missing>           7 weeks ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0B                  
<missing>           7 weeks ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           7 weeks ago         /bin/sh -c set -x  && apt-get update  && apt…   53.7MB              
<missing>           7 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=1.15.2.0.…   0B                  
<missing>           7 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.15.2-…   0B                  
<missing>           2 months ago        /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           2 months ago        /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           2 months ago        /bin/sh -c #(nop) ADD file:919939fa022472751…   55.3MB              
ElonJelinek:~ ElonJelinek$ 
运行镜像:

命名新的服务为web2,并且映射到81端口,用浏览器访问:localhost:81,查看效果

ElonJelinek:~ ElonJelinek$ docker run --name web2 -d -p 81:80 nginx:v2
8172e4d985ba9adc9e0fbe4c56202d5cef2a5a7ab966a7f79ae2a149acdad1a3
ElonJelinek:~ ElonJelinek$ 
使用dockerfile定制镜像

在Application下新建一个文件夹,mynginx,并在其中新建一个文本文件,命名为Dockerfile,并添加内容:

FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

FROM用来指定基础镜像,RUN用来执行命令行命令,每一个RUN都是新建立一层。
构建镜像:
在dockerfile文件所在目录执行:docker build -t nginx:v3 .

ElonJelinek:mynginx ElonJelinek$ docker build -t nginx:v3 .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM nginx
 ---> c82521676580
Step 2/2 : RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
 ---> Running in 49981b6d80b5
Removing intermediate container 49981b6d80b5
 ---> 027675b65995
Successfully built 027675b65995
Successfully tagged nginx:v3
ElonJelinek:mynginx ElonJelinek$ 

运行这个镜像:docker run --name web5 -d -p 82:80 nginx:v3

ElonJelinek:~ ElonJelinek$ docker run --name web5 -d -p 82:80 nginx:v3
550b5f38b10933d53c94f7ce12526f5eed0a6274f17ab64bdb1b4dc6cf559614
ElonJelinek:~ ElonJelinek$ 

在浏览器输入:http://localhost:82/,查看效果。

查看所有容器:

docker container ls -a

ElonJelinek:~ ElonJelinek$ docker container ls 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ElonJelinek:~ ElonJelinek$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
550b5f38b109        nginx:v3            "nginx -g 'daemon of…"   13 hours ago        Exited (255) 13 hours ago   0.0.0.0:82->80/tcp   web5
4596d42872b4        nginx:v3            "nginx -g 'daemon of…"   13 hours ago        Created                                          web4
977ffcc57532        nginx:v3            "nginx -g 'daemon of…"   13 hours ago        Exited (255) 13 hours ago   0.0.0.0:81->80/tcp   web3
8172e4d985ba        nginx:v2            "nginx -g 'daemon of…"   15 hours ago        Exited (255) 13 hours ago   0.0.0.0:81->80/tcp   web2
74ddf8bb271e        nginx               "nginx -g 'daemon of…"   15 hours ago        Exited (255) 13 hours ago   0.0.0.0:80->80/tcp   webserver
9e42a8cfaa7a        hello-world         "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                           compassionate_yonath
c22c2835c747        hello-world         "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                           competent_shannon
删除容器:

docker container rm 容器名

ElonJelinek:~ ElonJelinek$ docker container rm web5
web5
ElonJelinek:~ ElonJelinek$ docker container rm web4
web4
ElonJelinek:~ ElonJelinek$ docker container rm web3
web3
ElonJelinek:~ ElonJelinek$ docker container rm web6
Error: No such container: web6
ElonJelinek:~ ElonJelinek$ docker container rm web2
web2
ElonJelinek:~ ElonJelinek$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
74ddf8bb271e        nginx               "nginx -g 'daemon of…"   15 hours ago        Exited (255) 13 hours ago   0.0.0.0:80->80/tcp   webserver
9e42a8cfaa7a        hello-world         "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                           compassionate_yonath
c22c2835c747        hello-world         "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                           competent_shannon
ElonJelinek:~ ElonJelinek$ 
删除镜像:

docker image rm 027,必须先删除容器,然后才能删除镜像。

ElonJelinek:~ ElonJelinek$ docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v3                  027675b65995        15 hours ago        109MB
nginx               v2                  d584cfbd5352        15 hours ago        109MB
nginx               latest              c82521676580        7 weeks ago         109MB
hello-world         latest              2cb0d9787c4d        2 months ago        1.85kB
ElonJelinek:~ ElonJelinek$ docker image rm 027
Untagged: nginx:v3
Deleted: sha256:027675b659958311bdd692dbdb9fd81985142434c5d83830fb6752c61da44d3f
Deleted: sha256:357d6cd26f8c389f2f2f2374b95a064957e734dd81c27c190c60a043dddc2247
ElonJelinek:~ ElonJelinek$ docker image rm d58
Untagged: nginx:v2
Deleted: sha256:d584cfbd53522c9d73ead4f82a57a1c62f54699085574780c19f13a3362d4bb6
Deleted: sha256:9da22e7f99fb49a616adf8b9d3ca659a7e8a2f3940a8b0dd28b16bd9d4871bf9
ElonJelinek:~ ElonJelinek$ docker image ls -a
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c82521676580        7 weeks ago         109MB
hello-world         latest              2cb0d9787c4d        2 months ago        1.85kB
ElonJelinek:~ ElonJelinek$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
74ddf8bb271e        nginx               "nginx -g 'daemon of…"   15 hours ago        Exited (255) 13 hours ago   0.0.0.0:80->80/tcp   webserver
9e42a8cfaa7a        hello-world         "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                           compassionate_yonath
c22c2835c747        hello-world         "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                           competent_shannon
ElonJelinek:~ ElonJelinek$ 
终止容器:

docker container stop 容器名

ElonJelinek:~ ElonJelinek$ docker container stop webserver
webserver
ElonJelinek:~ ElonJelinek$ 
启动已终止容器:

docker container start 容器名

ElonJelinek:~ ElonJelinek$ docker container start webserver
webserver
ElonJelinek:~ ElonJelinek$ 
启动和终止容器写法2:

启动:docker start webserver
终止:docker stop webserver

ElonJelinek:~ ElonJelinek$ docker start webserver
webserver
ElonJelinek:~ ElonJelinek$ docker stop webserver
webserver
ElonJelinek:~ ElonJelinek$ 
查找官方仓库中的镜像:

docker search go

ElonJelinek:~ ElonJelinek$ docker search go
NAME                           DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
golang                         Go (golang) is a general purpose, higher-lev…   2154                [OK]                
gogs/gogs                      Gogs is a painless self-hosted Git service.     444                                     [OK]
google/cadvisor                Analyzes resource usage and performance char…   308                                     
google/cloud-sdk               Google Cloud SDKbundle with all components a…   206                                     [OK]
ethereum/client-go             Official golang implementation of the Ethere…   146                                     [OK]
ipfs/go-ipfs                   Go implementation of IPFS, the InterPlanetar…   47                                      [OK]
gogs/gogs-rpi                  Gogs Docker image for Raspberry Pi 2            46                                      
gocd/gocd-server               GoCD server running on docker                   41                                      
google/docker-registry         Docker Registry w/ Google Cloud Storage driv…   36                                      
allinurl/goaccess              Official build of GoAccess                      18                                      [OK]
gocd/gocd-agent-alpine-3.5     Docker GoCD agent based on Alpine 3.5           11                                      
iron/go                        Tiny Docker image for Go.                       10                                      
hairyhenderson/gomplate        A commandline Golang template processor         8                                       [OK]
elijahwright/goldfish          Docker image build of the goldfish vault-ui …   4                                       [OK]
trivago/gollum                 Gollum docker image                             3                                       [OK]
openshiftdemos/gogs            Gogs git server optimized for use on OpenShi…   3                                       
resin/artik710-golang          Go (golang) is a general purpose, imperative…   2                                       
circleci/golang                CircleCI images for Go                          2                                       [OK]
govuk/government-frontend      Public-facing app to display documents on ww…   1                                       [OK]
garukun/golgtm                 github.com/garukun/golgtm                       1                                       
jetbrainsinfra/golang          Golang + custom build tools                     0                                       [OK]
resin/artik710-fedora-golang   Go (golang) is a general purpose, imperative…   0                                       
paasmule/paas-tools-go         PaaS Tools based on Golang 1.10                 0                                       [OK]
paasmule/govc                  minimal debian based image with govc            0                                       [OK]
starkandwayne/concourse-go     Task Image for running Concourse Pipelines -0                                       
ElonJelinek:~ ElonJelinek$ 

点击个人博客,查看更多文章:https://elonjelinek.github.io

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值