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

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

如果你需要经常性需要多处部署同样的项目,如果你曾经也遇到过"明明在我电脑运行得好好的"问题,如果听说过 Docker 但还没用过,如果你不确定你到底需不需要 Docker ,那么,希望你花时间阅读一下这篇文章!

因为 Docker 将帮助你轻松运行自己不熟悉语言编写的开源项目,帮助你更加优雅地部署自己的项目,省去重复下载并配置环境的繁琐过程...

现在让我们先睹为快,预览一下基于 Docker 部署项目的实际效果,希望能让你对 Docker 有个初步的印象!

  • Docker 部署的 nginx 作为反向代理服务器,支持 https 访问以及泛域名解析.

体验地址: https://snowdreams1006.cn/

  • Docker 部署的 letsencrypt 免费制作泛域名证书并整合反向代理服务 nginx 实现 https 访问.

体验地址: https://www.snowdreams1006.cn/

  • Docker 部署的 nginx 作为静态服务器,部署静态网站用于演示静态博客功能.

体验地址: https://resume.snowdreams1006.cn/

  • Docker 部署的 bark 作为后端服务器,部署开源项目用于充当消息推送服务器.

体验地址: https://bark.snowdreams1006.cn/ping

  • Docker 部署的 webhook 作为后端服务器,部署开源项目用于接收 Webhook 事件回调.

体验地址: https://webhook.snowdreams1006.cn/hooks/github

  • Docker 部署的 blog 作为静态服务器,基于 Github ActionWebhook 实现博客内容自动更新并推送消息.

Github 仓库内容更新后触发 Github Action 自动构建并部署远程服务器静态博客,同时发送的 Webhook 事件给 webhook 钩子容器,紧接着调用 bark 消息推送容器,实现消息推送到微信消息以及 app 通知.

Github 仓库更新后自动运行 Github Action 源码构建静态博客并上传到远程服务器,blog 容器会立即重启完成内容更新.

Github 仓库更新后发送 Webhooks 到远程服务器,webhook 容器接收到请求后转发给 bark 容器,进而推送给手机.

无论是熟悉的开源项目还是陌生的开源项目,Docker 让这些不一样变得一样,统一的管理方式使得使用成本大大降低,更加优雅地部署项目,真的不止是说说而已!

前提条件

目前在 Linux 系统上安装 Docker,对系统版本有以下要求:

  • CentOS : 7
  • Debian : 7.7(Wheezy LTS)、8.0(Jessie LTS)、9(Stretch)
  • Fedora : 24、25
  • Ubuntu : 16.04(Xenial LTS)、14.04(Trusty LTS)、17.04(Zesty)

一方面上述前提条件基本上新服务器都会满足,另一方面笔者对此并未深入实验,请读者自行验证,下面主要以 Centos7.6 为例讲解如何安装 Docker .

验证环境

对于新手来说,尽管安装 Docker 非常简单,但是总是不可避免地会遇到一些意外情况,或许是安装出错需要重新安装或者是不确定远程服务器是否已经安装,所以开始安装前还是先看一下到底有没有安装过 Docker 吧!

  • 调用 docker 命令

首先连接到远程服务器后运行 docker 命令,如果像下面那样输出一大堆用法介绍,那么证明 Docker 已经成功安装过,并且可能已经配置好相关环境了.

你现在唯一要做的就是学习一下 Docker 的基本用法,因为不用自己安装 Docker 环境,基本上也可以不必往下看了.

[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 提示 command not found ,说明服务器很可能并没有安装 Docker 环境,下面就教你如何一步一步安装 Docker 环境!

安装 Docker

  1. Step 1 : 移除旧版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

这一步是可选的,是因为最新版 Docker 的名称已经发生了变化,为了保证安装的是最新版的 Docker-CE ,所以首先卸载可能已经安装过的旧版本.

  1. Step 2 : 安装必要系统依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

安装一些必要依赖,跟着官方教程说明走就好了,即使系统已存在该环境也可以再次运行,放心复制粘贴吧!

  1. Step 3 : 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  1. Step 4 : 更新 yum 缓存
sudo yum makecache fast
  1. Step 5 : 安装 docker-ce
sudo yum -y install docker-ce

如果上述安装过程中没有出现任何报错,那么现在已经安装好基本的 Docker 环境!

启动 Docker

  • 查看状态
sudo systemctl status docker

初次安装成功后默认是不会自动启动 Docker 服务的,此时查看运行状态的输出结果不会包括 Active: active (running) 而是 Active: inactive (dead) .

  • 首次启动
sudo systemctl start docker

安装后默认是没有启动 Docker 服务的,因此安装后需要先启动 Docker 服务,再次查看运行状态 sudo systemctl status docker 应该会出现正在运行 Active: active (running) .

  • 重新启动
sudo systemctl restart docker

如果 Docker 服务已停止可以重新启动,如果已经启动也可以重新启动.

  • 停止服务
sudo systemctl stop docker

如果正在运行的 Docker 存在问题需要停止维修,那么可以先停止 Docker 服务,待维修结束后可以运行 sudo systemctl start docker 再次启动服务.

  • 检查自启
systemctl list-unit-files | grep enabled | grep docker

检查 Docker 服务是否会开机自启,如果存在结果则表示会开机自启,如果没有结果则表示不会开机自启.

  • 开机自启
sudo systemctl enable docker

Docker 服务是非常重要的进程服务,一般需要开机自启,保证意外关机后能自行恢复服务,推荐开机自启.

  • 禁止自启
sudo systemctl disable docker

如果不小心设置了开机自启而你真的不打算开机自启的话,那么可以禁用开机自启功能,下次电脑重启后不会自动启动Docker 服务.

  • 查看版本
docker version

查看当前安装的 Docker 版本信息,可以看出来主要分为两部分: Client: Docker Engine - CommunityServer: Docker Engine - Community .

[root@snowdreams1006 ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:25:41 2019
 OS/Arch:           linux/amd64
 Experimental:      false
 
Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:24:18 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

现在并不必关心具体的版本信息,只要运行 docker version 命令后能够输出类似信息即可,接下来开始真正的表演!

镜像加速

Docker 服务已经安装并启动,接下来我们可以基于 Docker 部署应用了,当然现在离真正部署自己的应用还有不小距离,但是我们可以运行公开的应用啊!

学习任何新语言的第一件事就是运行 hello world ,学习 Docker 容器化部署也不例外,我们也运行 Docker 版本的 hello world !

当我们敲入 docker run hello-world 命令后,终端会输出下列内容,只要输出 Hello from Docker! 字样就证明环境已经搭建完毕!

[root@snowdreams1006 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.
 
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
 
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
 
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
 
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
 
[root@snowdreams1006 ~]# 

如果你的网速比较慢,上述过程可能有一些耗时,但是如果你的网速一般而上述过程异常慢,很可能是因为你没有配置镜像!

因为 Docker 默认是先从国外下载项目到本地,然后再运行服务的,正如我们平时访问 Github 一样,那网速不是一般的慢!

Github 没有镜像加速地址并不能为我们加速访问,但是 Docker 项目仓库是有镜像仓库的,国内提供这种镜像服务的有不少,基本上都需要注册账号获取镜像地址之类的.

这里提供一下网易的镜像仓库地址 http://hub-mirror.c.163.com 以及阿里云的个人镜像仓库地址 https://8upnmlh3.mirror.aliyuncs.com .

只要将镜像地址配置给 Docker ,下一次再下载项目时速度应该就会得到明显提升!

首选打开并编辑 /etc/docker/daemon.json 文件,如果没有的话就新建该文件,内容如下:

{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

保存后重启 Docker 服务,试一试运行 docker pull nginx 会不会很快呢?

卸载 docker

假如发现意外想要重新安装 Docker 服务或者就是想要卸载 Docker ,那么只需要简单运行下列命令就能清除掉 Docker 环境!

sudo yum remove docker-ce
sudo rm -rf /var/lib/docker

此时再次运行 docker 命令就会提示 command not found ,期待下一次相见会让人焕然一新!

基本命令

正如初次见面的那样,当我们成功安装 Docker 后控制台输出了一大堆关于用法的介绍,只不过当时年少轻狂并不在乎,蓦然回首,竟发现如此有用!

[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.

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

相关文章
|
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控制台部署个人应用(图文示例)

热门文章

最新文章