从零开始搭建服务器之更加优雅地部署项目(2)

简介: 从零开始搭建服务器之更加优雅地部署项目

从零开始搭建服务器之更加优雅地部署项目(1)https://developer.aliyun.com/article/1542857

自我介绍

用法介绍的第一段就是自我介绍,用法是: docker [OPTIONS] COMMAND ,即 docker + 可选选项 + 必选命令.

表示的含义就是 A self-sufficient runtime for containers 为容器提供一个自包含的运行环境!

Usage:  docker [OPTIONS] COMMAND
 
A self-sufficient runtime for containers

Docker 类似于生活中超级货轮,运输着统一规格的集装箱,而集装箱装着各种各样的货物,开往不同的目的地.

容器则是集装箱,货轮为集装箱提供了自包含的环境,集装箱之间是相互独立的,这也是对第一段话的简单解释.

选项配置

下面我们继续看第二段内容,主要解释了有哪些配置项以及这些配置项背后表示的具体含义.

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

只要有一定英语基础的人应该都能看懂其中的意思,如果对个人细节不是很清楚的话,可以复制粘贴到浏览器在线翻译,这里就不全文解释了.

不消息看到了最后一个 -v, --version 选项,表示的意思是打印版本信息并且退出.

看到这里我们就明白了,原来之前运行的 docker version 和这里的 --version 并不是一回事啊!

[root@snowdreams1006 ~]# docker -v
Docker version 19.03.5, build 633a0ea
[root@snowdreams1006 ~]# docker --version
Docker version 19.03.5, build 633a0ea

单纯从输出结果来说,docker --version 更加简洁,如果只是验证环境安装是否成功,还是运行docker --version 比较简单明了.

管理命令

第三部分是 Docker 支持的管理命令,现在不去深究细节,只要有印象就行,注意这里有个关于镜像的命令 docker image

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

因为自我介绍中关于用法是 docker [OPTIONS] COMMAND ,而中括号 [] 表示该内容是可选的,所以不加任何选项的基本用法就是 docker COMMAND ,因此其中关于 image 命令的完整用法就是: docker image .

[root@snowdreams1006 ~]# docker image
 
Usage:  docker image COMMAND
 
Manage images
 
Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
 
Run 'docker image COMMAND --help' for more information on a command.

别有洞天,管理命令中还有子命令,大概用法和之前介绍的内容大致相同,基本用法是: docker image COMMAND .

其中支持的命令中有 ls ,因此调用 ls 命令的最终完整命令就是: docker image ls .

# docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              fce289e99eb9        11 months ago       1.84kB

服务器已安装的 image 中就包括我们熟悉的 hello-world ,至于什么是 REPOSITORY ,什么是 IMAGE 暂时也不用深究,只需要知道如何无文档使用这些命令即可!

如果用心留意的话,可以看到 Run 'docker image COMMAND --help' for more information on a command. 这么一句话,看来我们有现成的帮助文档供我们学习啊!

还是以 ls 命令为例,演示一下如何使用 docker image COMMAND --help 查看帮助文档.

[root@snowdreams1006 ~]# docker image ls --help
 
Usage:  docker image ls [OPTIONS] [REPOSITORY[:TAG]]
 
List images
 
Aliases:
  ls, images, list
 
Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

麻雀虽小五脏俱全,没想到 ls 命令还有更加细粒度的用法说明,支持可选参数和 [REPOSITORY[:TAG]] ,除此之外还有 ls, images, list 别名!

如果 lsimageslist 别名,那么岂不是意味着 docker image ls 等价于 docker image imagesdocker image list ?

[root@snowdreams1006 ~]# docker image list
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              fce289e99eb9        11 months ago       1.84kB
[root@snowdreams1006 ~]# docker image images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
hello-world            latest              fce289e99eb9        11 months ago       1.84kB

从上述输出结果来看,三者的运行效果确实是一样的,看来又发现了新大陆!

普通命令

回到 docker 命令的主线,除了管理命令外还是普通命令,这部分命令也是经常性使用到的命令也是重点学习掌握的命令!

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  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
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  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
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  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
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

命令虽好但不可贪多,还是找到最简单刚刚用过的 docker rundocker version 命令吧!

  • docker run : Run a command in a new container

表示在新的容器内运行命令,翻译成生活语言就是在集装箱内做着不可告人的神秘操作!

  • docker version : Show the Docker version information

显示 Docker 版本信息,还记得 docker --version 吗?

忘记了的话,往上翻翻看,--version 的描述是 Print version information and quit ,是一种更加简单的版本信息.

无论是管理命令还是普通命令,直接输入命令后都会有相应的用法说明以及帮助信息,同样地追加 --help 即可!

[root@snowdreams1006 ~]# docker run
"docker run" requires at least 1 argument.
See 'docker run --help'.
 
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
 
Run a command in a new container

帮助信息

最后的才是亮点,在命令结尾处追加 --help 可以获取更加详细的帮助信息,这一点不仅适合一级命令 docker image --help 还适合二级子命令 docker image ls --help .

Run 'docker COMMAND --help' for more information on a command.

所以,遇到不懂的或者陌生的命令请一定要记住 --help 帮助命令,这是 docker 全部命令中最重要的一点!

回忆总结

Docker 是一种规范化的部署运维新方式,相对于传统打包部署的来说,更加统一规范化,货物是各种各样的正如开发语言的多样性一样,但是集装箱的出现却颠覆了物流运输,带来了巨大的进步!

如果你是 Java 后台开发,或多或少肯定有着自己独立部署项目的经历,先登录服务器装个 Java 环境再装个 Tomcat 环境,最后在上传自己的 War 包到 Tomcat 部署目录,如此重复繁琐的劳动还不一定能保证一次性成功!

因为有时你的代码中很有可能有些绝对路径,部署到服务器肯定会报错,如果缺少了个人文件也会报错等等,这时候就出现了经典的对话:明明在我的电脑运行地好好的啊!

Docker 的出现在一定程度上解决了这种问题,将应用打包到集装箱,Docker 作为超级货轮承载着集装箱安全快速地运送到目的地,集装箱内的环境是自给自足的封闭环境,所有的相关依赖一次性全部都给你.

无论是本机运输这个封闭的集装箱还是远程服务器运输这个集装箱结果都是一样的,再也不会出现环境不一致而导致的相互埋怨情况的发生了!

那么问题来了,如果给你一个集装箱,你能安全快速运输到目的地吗?如果你手头上已经有一批货需要这种集装箱服务,如何快速封装成集装箱呢?

对于第一个问题,本文已经给出答案,那就是 docker + docker COMMAND --help 查询支持的命令以及查看命令的帮助文档.

[root@snowdreams1006 ~]# docker
 
Usage:  docker [OPTIONS] COMMAND
 
A self-sufficient runtime for containers
 
Options:
      --config string      Location of client config files (default
                           "/root/.docker")
  -c, --context string     Name of the context to use to connect to the
                           daemon (overrides DOCKER_HOST env var and
                           default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default
                           "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default
                           "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit
 
Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  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
  build       Build an image from a Dockerfile
  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
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  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
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  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
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.

对于第二个问题,请先预习 docker 相关命令,下一次将实例分享如何使用 docker 运输集装箱,感谢你的阅读!

如果想要更方便地查看系列文章,欢迎访问我的网站 https://snowdreams1006.tech/ 或者 https://blog.snowdreams1006.cn/ ,喜欢就点个赞再走呗!

参考资料

相关文章
|
3月前
|
弹性计算 监控 负载均衡
|
4天前
|
并行计算 前端开发 异构计算
告别服务器繁忙,云上部署DeepSeek
本文以 DeepSeek-R1-Distill-Qwen-32B-FP8 为例,向您介绍如何在GPU实例上使用容器来部署量化的 DeepSeek-R1 蒸馏模型。
|
7天前
|
弹性计算 JavaScript 前端开发
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
Node.js 是一种高效的 JavaScript 运行环境,基于 Chrome V8 引擎,支持在服务器端运行 JavaScript 代码。本文介绍如何在阿里云上一键部署 Node.js 环境,无需繁琐配置,轻松上手。前提条件包括 ECS 实例运行中且操作系统为 CentOS、Ubuntu 等。功能特点为一键安装和稳定性好,支持常用 LTS 版本。安装步骤简单:登录阿里云控制台,选择扩展程序管理页面,安装 Node.js 扩展,选择实例和版本,等待创建完成并验证安装成功。通过阿里云的公共扩展,初学者和经验丰富的开发者都能快速进入开发状态,开启高效开发之旅。
|
9天前
|
弹性计算 JavaScript 前端开发
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
一键安装!阿里云新功能部署Nodejs环境到ECS竟然如此简单!
|
10天前
|
机器学习/深度学习 弹性计算 人工智能
在阿里云ECS上一键部署DeepSeek-R1
Open WebUI 和 Ollama 的联合,通过集成 DeepSeek-R1 的强大功能,赋予每一位用户使用尖端 AI 技术的能力,使得复杂的 AI 技术不再是遥不可及的梦想。无论是研究人员、开发者,还是企业用户,您都能从这一创新中获得新的灵感和增长点。本文介绍通过计算巢一键部署和使用DeepSeek-R1。
在阿里云ECS上一键部署DeepSeek-R1
|
25天前
|
安全 前端开发 Go
轻松部署翼龙面板社区版:您的游戏服务器管理新体验
翼龙面板(Pterodactyl)社区版正是为此而生,它通过强大的开源技术栈(PHP、React 和 Go)和可靠的安全设计,让游戏服务器管理变得简单而高效。本文将带您通过阿里云计算巢快速部署翼龙面板社区版,尽享流畅的管理体验。
轻松部署翼龙面板社区版:您的游戏服务器管理新体验
|
28天前
|
XML Java 应用服务中间件
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
211 17
Spring Boot 两种部署到服务器的方式
|
4月前
|
监控 安全 Linux
RHEL 环境下 Subversion 服务器部署与配置
【10月更文挑战第18天】在RHEL环境下部署Subversion服务器需依次完成安装Subversion、创建版本库、配置服务器、启动服务、客户端连接及备份维护等步骤。确保遵循安全最佳实践,保障数据安全。
156 1
|
6天前
|
机器学习/深度学习 人工智能 开发者
DeepSeek服务器繁忙?拒绝稍后再试!基于阿里云PAI实现0代码一键部署DeepSeek-V3和DeepSeek-R1大模型
阿里云PAI平台支持零代码一键部署DeepSeek-V3和DeepSeek-R1大模型,用户可轻松实现从训练到部署再到推理的全流程。通过PAI Model Gallery,开发者只需简单几步即可完成模型部署,享受高效便捷的AI开发体验。具体步骤包括开通PAI服务、进入控制台选择模型、一键部署并获取调用信息。整个过程无需编写代码,极大简化了模型应用的门槛。
127 7
|
3月前
|
弹性计算 开发工具 git
2分钟在阿里云ECS控制台部署个人应用(图文示例)
作为一名程序员,我在部署托管于Github/Gitee的代码到阿里云ECS服务器时,经常遇到繁琐的手动配置问题。近期,阿里云ECS控制台推出了一键构建部署功能,简化了这一过程,支持Gitee和GitHub仓库,自动处理git、docker等安装配置,无需手动登录服务器执行命令,大大提升了部署效率。本文将详细介绍该功能的使用方法和适用场景。
2分钟在阿里云ECS控制台部署个人应用(图文示例)

热门文章

最新文章