docker命令使用技巧

本文详细介绍了Docker的基础操作,包括查看系统信息、登录/登出Registry、查看版本信息以及镜像和容器的管理。在镜像操作中,讲解了如何查找、拉取、查看和删除镜像。在容器操作部分,阐述了如何启动容器、执行命令、查看容器列表以及删除容器。此外,还提供了管理命令的概述。通过实例演示,帮助读者更好地理解和运用Docker。
摘要由CSDN通过智能技术生成

0 help使用

  • 使用docker help查看docker能够使用的命令
  • 使用docker help run查看具体命令的使用

功能详解

  • info Display system-wide information
  • login Log in to a Docker registry
  • logout Log out from a Docker registry
  • version Show the Docker version information

1 镜像操作

  • 查找镜像:docker search 镜像名
  • 拉取镜像:docker pull 镜像名[:版本号]
    • 示例:docker pull tomcat docker pull tomcat:9.0
    • 说明:不带tag,默认拉取tag为latest的镜像,即docker pull tomcatdocker pull tomcat:latest拉取的是同一个镜像
  • 查看本地镜像:docker images
  • 查看镜像信息:docker inspect 镜像id docker inspect 镜像名称[:版本号]
  • 删除镜像:docker rmi 镜像id docker rmi 镜像名称[:版本号]

功能详解

  • build Build an image from a Dockerfile
  • history Show the history of an image
  • images List images
  • import Import the contents from a tarball to create a filesystem image
  • inspect Return low-level information on Docker objects
  • load Load an image from a tar archive or STDIN
  • pull Pull an image or a repository from a registry
  • push Push an image or a repository to a registry
  • rmi Remove one or more images
  • save Save one or more images to a tar archive (streamed to STDOUT by default)
  • search Search the Docker Hub for images
  • tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

2 容器操作

  • 启动容器:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    • 启动centos:docker run -it centos:7,按Ctrl+P+Q可以不停止退出
    • 启动nginx:docker run --name nginx80 -p 80:80 -d nginx
    • 启动mysql:docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --rm -d mysql:8.0.19
    Options:
          --add-host list                  Add a custom host-to-IP mapping (host:ip)
          --cap-add list                   Add Linux capabilities
          --cap-drop list                  Drop Linux capabilities
          --cpus decimal                   Number of CPUs
          --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
          --dns list                       Set custom DNS servers
          --domainname string              Container NIS domain name
          --entrypoint string              Overwrite the default ENTRYPOINT of the image
      -e, --env list                       Set environment variables
      -h, --hostname string                Container host name
      -i, --interactive                    Keep STDIN open even if not attached
          --ip string                      IPv4 address (e.g., 172.30.100.104)
          --kernel-memory bytes            Kernel memory limit
      -l, --label list                     Set meta data on a container
          --link list                      Add link to another container
          --link-local-ip list             Container IPv4/IPv6 link-local addresses
          --name string                    Assign a name to the container
          --network network                Connect a container to a network
          --privileged                     Give extended privileges to this container
      -p, --publish list                   Publish a container's port(s) to the host
      -P, --publish-all                    Publish all exposed ports to random ports
          --pull string                    Pull image before running ("always"|"missing"|"never") (default "missing")
          --read-only                      Mount the container's root filesystem as read only
          --restart string                 Restart policy to apply when a container exits (default "no")
          --rm                             Automatically remove the container when it exits
      -t, --tty                            Allocate a pseudo-TTY
      -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      -v, --volume list                    Bind mount a volume
          --volumes-from list              Mount volumes from the specified container(s)
      -w, --workdir string                 Working directory inside the container
    
  • 容器列表:docker ps [OPTIONS]
    • 显示正在运行的容器:docker ps
    • 显示所有状态的容器:docker ps -a
    • 显示所有容器的id:docker ps -aq
    Options:
      -a, --all             Show all containers (default shows just running)
      -f, --filter filter   Filter output based on conditions provided
          --format string   Pretty-print containers using a Go template
      -n, --last int        Show n last created containers (includes all states) (default -1)
      -l, --latest          Show the latest created container (includes all states)
          --no-trunc        Don't truncate output
      -q, --quiet           Only display container IDs
      -s, --size            Display total file sizes
    
  • 容器中执行命令:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
    • 进入容器:docker exec -it 容器名 /bin/bash
    • 执行命令:docker exec 容器名 cat /etc/hosts
  • 删除容器:docker rm [OPTIONS] CONTAINER [CONTAINER...]
    • 删除:docker rm 容器名
    • 强制删除:docker rm -f 容器名

功能详解

  • 详细查看https://www.kancloud.cn/woshigrey/docker/935883
  • 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
  • exec Run a command in a running container
  • export Export a container’s filesystem as a tar archive
  • inspect Return low-level information on Docker objects
  • kill Kill one or more running containers
  • 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
  • ps List containers
  • rename Rename a container
  • restart Restart one or more containers
  • rm Remove one or more containers
  • run Run a command in a new container
  • 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
  • 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

3、管理命令

  • image Manage images
  • network Manage networks
  • node Manage Swarm nodes
  • service Manage services
  • stack Manage Docker stacks
  • swarm Manage Swarm
  • volume Manage volumes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Docker Desktop时,以下是一些使用技巧和建议: 1. 确保您的系统满足Docker Desktop的最低要求,包括64位操作系统、虚拟化支持等。 2. 在安装Docker Desktop之前,检查您的计算机上是否已安装其他虚拟化软件,如VirtualBox。这些软件可能会与Docker Desktop冲突,导致启动问题。 3. 使用Docker Hub账户登录Docker Desktop,以便您可以轻松地访问和管理您的镜像、容器和仓库。 4. 在Docker Desktop设置中,您可以配置资源限制、网络设置、文件共享等。根据您的需求和计算机配置进行适当的设置。 5. 使用Docker Compose来定义和运行多容器应用程序。通过编写一个简单的YAML文件,您可以轻松地定义多个容器之间的依赖关系和网络配置。 6. 使用Docker命令行工具(如docker命令)来管理和操作容器。您可以创建、启动、停止、删除容器,以及查看容器日志等。 7. 在使用Docker时,保持容器的状态可复现性是很重要的。使用Dockerfile来定义您的镜像构建过程,并使用版本控制工具(如Git)管理您的Dockerfile和相关文件。 8. 如果您需要在本地开发环境中运行一个Web应用程序,考虑使用Docker容器来模拟生产环境。您可以使用容器化的数据库、Web服务器等组件,以及自动化部署和测试。 9. 在使用Docker时,注意安全性。确保您只使用受信任的镜像,定期更新您的镜像和容器,并配置适当的网络访问控制。 10. 加入Docker社区并参与讨论、学习和分享经验。Docker有一个活跃的社区,您可以在论坛、邮件列表、博客等地方找到有用的资源和技巧。 希望这些技巧对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值