您的位置:首页 > 运维架构 > Docker

Docker之常用命令

2017-05-05 23:19 302 查看

Docker之常用命令

1.查看docker信息

#查看docker版本

$ docker version

#查看docker系统信息

$ docker info


2.对镜像的操作

# 检索image

$ docker search image_name

# 下载image

$ docker pull image_name

# 列出镜像列表; -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs

$ docker images

# 删除一个或者多个镜像; -f, --force=false Force; --no-prune=false Do not delete untagged parents

$ docker rmi image_name

# 显示一个镜像的历史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs

$ docker history image_name


3.启动容器

# 在容器中运行"echo"命令,输出"hello word"

$ docker run image_name echo "hello word"

# 交互式进入容器中

$ docker run -i -t image_name /bin/bash

# 在容器中安装新的程序

$ docker run image_name apt-get install -y app_name

# 在运维中普遍使用的docker命令

$ docker run --name titan-showcase -p 8480:8080 -v /u01/titan-showcase/download:/home/titan-showcase/download  -e TZ=Asia/Shanghai  --restart=always -d titan-showcase:0.1.47


注意: 在执行apt-get 命令的时候,要带上-y参数。如果不指定-y参数的话,apt-get命令会进入交互模式,需要用户输入命令来进行确认,但在docker环境中是无法响应这种交互的。apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。

4.查看容器

# 列出当前所有正在运行的container

$ docker ps

# 列出所有的container

$ docker ps -a

# 列出最近一次启动的container

$ docker ps -l


5.对容器的操作

# 删除所有容器

$ docker rm `docker ps -a -q`

# 删除单个容器; -f, --force=false; -l, --l
4000
ink=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container

$docker rm Name/ID

# 停止、启动、杀死一个容器

$ docker stop Name/ID
$ docker start Name/ID
$ docker kill Name/ID

# 从一个容器中取日志; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps

$docker logs Name/ID

# 使用比较广泛的读取docker容器日志的后一千行信息的命令

$ docker logs -f --tail=1000 Name/ID

# 列出一个容器里面被改变的文件或者目录,list列表会显示出三种事件,A 增加的,D 删除的,C 被改变的

$ docker diff Name/ID

# 显示一个运行的容器里面的进程信息

$ docker top Name/ID

# 从容器里面拷贝文件/目录到本地一个路径

$ docker cp Name:/container_path to_path

$ docker cp ID:/container_path to_path

# 重启一个正在运行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10

$ docker restart Name/ID

# 附加到一个运行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process

$ docker attach ID


6.保存和加载镜像

当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。

# 保存镜像到一个tar包; -o, --output="" Write to an file

$ docker save image_name:image_version -o file_path

# 加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file

$ docker load -i file_path

# 机器a

$ docker save image_name:image_version > /home/save.tar
或
$ docker save -o /home/save.tar image_name:image_version

# 使用scp将save.tar拷到机器b上,然后:

$ docker load -i < /home/save.tar


7.登录registry server

# 登陆registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Username

$ docker login


8.发布镜像

# 发布docker镜像

$ docker push new_image_name


9.根据dockerfile构建镜像

#build
--no-cache=false Do not use cache when building the image
-q, --quiet=false Suppress the verbose output generated by the containers
--rm=true Remove intermediate containers after a successful build
-t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success

$ docker build -t image_name:image_version Dockerfile_path


10.运维中一个比较典型的dockerfile文件示例

FROM changan/java:1.8

MAINTAINER xxx

# set timezone to PRC
ENV TZ="Asia/Shanghai" LANG=en_US.UTF-8  PRO_ENV="prod"

ADD build/libs/xxx-0.0.1.jar /home/App.jar

ENTRYPOINT java -jar /home/App.jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: