日常学习--docker命令梳理--20240714

1、docker的帮助命令

docker --help        展示所有docker相关命令的用法

docker command --help        展示docker中某个命令的具体用法

主要和常用的基础docker 命令:

Usage:  docker [OPTIONS] COMMAND

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

2、查看已有的容器和镜像

docker ps        查看正在运行的容器

docker ps -a        查看所有容器

docker top container        查看正在运行容器的进程

docker logs [-t -f -n 20] container        查看容器的所有日志(实时查看日志末尾20条数据);可以通过Ctrl+C来退出该状态

docker images        查看所有镜像

3、启动容器

docker start container        可以用来启动一个已经stoped的容器

docker run [-t -i -d] [--name container_name] ubuntu:14.04 [/bin/bash]        运行镜像获得一个容器,生产环境运行时可能不需要在启动容器时,就不需要后面的[/bin/bash],命令的含义为先从本地找ubuntu:14.04的镜像,如果没有,则从注册仓库中下载,并运行镜像获得一个容器

4、连接运行的容器做一些事情

docker attach container        多个attach的容器窗口会相互影响,可以通过exit或者Ctrl+d命令退出但是可能会关闭对应的容器;还可以通过Ctrl+Q+P退出不影响容器运行(未证实)

docker exec -it container /bin/bash        可以通过exit和Ctrl+d退出不影响容器运行

5、容器的重启、暂停和停止

docker restart container        重启正在运行或者已经停止的容器

docker pause container        暂停容器运行

docker unpause container        让暂停的容器重新运行

docker stop container        容器停止运行

6、删除容器和镜像

docker rm container        删除容器

docker rmi image        删除镜像(使用名称删除时需要包含其tag)

7、导出、导入容器快照

docker export container > ubuntu.tar        容器从docker导出到本地

cat ubuntu.tar | docker import - test/ubuntu:v1.0        Windows命令可能不支持,需要依赖docker图形界面导入

8、镜像获取和导入导出

docker pull ubuntu:12.04        从注册仓库中获取镜像

docker commit -m "Added json gem" -a "Docker Newbee" container ouruser/sinatra:v2        修改的容器提交为新的镜像

通过dockerfile文件中的命令,将本地项目打包成镜像上传到docker仓库中

本地的容器快照导入到docker镜像仓库中

docker save -o ubuntu_14.04.tar ubuntu:14.04        导出镜像到本地

docker load < ubuntu_14.04.tar        导入本地镜像到镜像仓库

9、端口映射和数据卷挂载

docker port container        查看容器和主机间的端口映射

docker run -itd -p 8080:80 --name linux_volume_v2 -v "C:\Users\X1 carbon\docker-volume":/docker-volume ubuntu:18.04        命令解释:后台运行镜像ubuntu:18.04,创建名称为linux_volume_v2的容器,将主机的8080端口映射到容器的80端口上,同时将主机的C:\Users\X1 carbon\docker-volume目录挂载到容器的/docker-volume上(两者地址需要都是绝对地址,在本地修改挂载文件容器中会同步生效,可以在-v "C:\Users\X1 carbon\docker-volume":/docker-volume:ro方式限制挂载目录的权限为只读)

docker run -d -v /dbdata --name dbdata ubuntu:latest echo Data-only container for postgres        创建一个名称为dbdata的数据卷容器挂载数据卷为容器的/dbdata,

docker run -itd --name linux_use_dbdata --volumes-from dbdata  ubuntu:18.04        创建一个名为linux_use_dbdata的容器挂载一个名称为dbdata 的数据卷容器(也可以挂载多个)

10、数据卷容器的备份和恢复以及容器间的连接

docker run --volumes-from dbdata -v "C:\Users\X1 carbon":/backup ubuntu:18.04 tar cvf /backup/backup.tar dbdata        将数据卷容器中的数据备份到容器的/backup/backup.tar中,但是容器的/backup和主机的当前目录"C:\Users\X1 carbon"挂载,相当于将数据卷数据备份到主机

# 创建一个新的容器,挂载dbdata2的数据卷,并执行恢复操作  (有待验证)
docker run --rm -v dbdata2_volume_name:/restore_target busybox tar xvf /path/to/backup/backup.tar -C /restore_target

docker run -itd --link linux_use_dbdata:dblink training/webapp        创建一个容器连接正在运行的容器linux_use_dbdata(docker1.9以后不推荐了,直接创建网络,将容器都连接到同一个网络中)

11、docker注意事项

已经创建的容器不能直接修改其数据卷配置、也不能直接改变其端口的映射关系、(前两者只能通过迂回的方式达成要求)但是可以和其他的容器连接(通过加入同一个网络的方式)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值