Docker的一些基本命令

Docker安装相关命令

创建docker用户并将其纳入docker这个用户组

#useradd docker -gdocker

更新源

#sudo apt-get update

安装依赖包

#sudo apt-getinstalllinux-image-generic-lts-trusty

重启

#sudo reboot

安装docker

#sudo apt-getinstallwget     // 可以通过命令which wget是否已安装

#wget -qO-https://get.docker.com/ |sh   //获取最新版本

验证安装是否成功

#sudo docker runhello-world

Docker的基本命令

1、查看docker信息(version、info)

 $docker version;  $docker info


2、对image的操作(search、pull、images、rmi、history)
#检索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、启动容器(run)
# 在容器中运行"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 appname
注意:
在执行apt-get 命令的时候,要带上-y参数。如果不指定-y参数的话,apt-get命令会进入交互模式,需要用户输入命令来进行确认,但在docker环境中是无法响应这种交互的。apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。

4、查看容器(ps)
# 列出当前所有正在运行的container
$docker ps
# 列出所有的container
$docker  ps  -a
# 列出最近一次启动的container
$docker  ps  -1

5、保存对容器的修改(commit)
# 保存对容器的修改; -a, --author="" Author; -m, --message="" Commit message
$docker  commit  ID  new_image_name
注意:
Note:  image相当于类,container相当于实例,不过可以动态给实例安装新软件,然后把这个container用commit命令固化成一个image。

6、对容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach)
# 删除所有容器 –f强制
$docker  rm  $(docker ps -a –q)
# 删除单个容器; -f, --force=false; -l, --link=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
# 列出一个容器里面被改变的文件或者目录,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
注意:
Note: attach命令允许你查看或者影响一个运行的容器。你可以在同一时间attach同一个容器。你也可以从一个容器中脱离出来,是从CTRL-C。

7、保存和加载镜像(save、load)
当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。
# 保存镜像到一个tar包; -o, --output="" Write to an file
$docker  save  image_name  -o  file_path
# 加载一个tar包格式的镜像; -i, --input="" Read from a tar archive file
$docker  load  -i  file_path
# 机器a
$docker  save  image_name > /home/save.tar
# 使用scp将save.tar拷到机器b上,然后:
$docker  load  < /home/save.tar
镜像加标签
$docker  tag  repository:tag  新的repository:tag
查看仓库镜像
docker search 仓库地址(192.168.253.212:5000)/a

8、登录registry server(login)
# 登陆registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Username
$docker  login

9、发布image(push)
# 发布docker镜像
$docker  push  new _image_name

10、根据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  Dockerfile_path

11.启动仓库
删除仓库里面的所有东西;
进入仓库所在的物理位置 cd /opt/data/registry/
ls目录列表 images  repositories
cd images/
rm -rf  *   删除仓库里面的所有镜像块
ls –镜像是以镜像块存储的
cd .. 退一格
cd repositories/
cd library/
rm -rf  *   删除仓库里面的所有东西

关闭或重启仓库
Docker  rm  -f  id/name

启动仓库:
docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry --restart=always docker.io/registry
注释: -d后台运行 –p 端口映射(容器内部端口8080——>30061)  5000:5000仓库内部端口和外部端口进行映射,-v 外部地址和docker容器的地址进行映射, --restart=always(当掉后立马自动启动)  镜像名称(docker.io/registry)
查看仓库所有东西
docker search 192.168.253.212:5000/a
拉取中间件到仓库里面
docker push 192.168.253.212:5000/tomcat_8

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值