docker 安装及简单 web 应用部署实战

2023-07-0405:58:54服务器及运维Comments696 views字数 1880阅读模式

部署项目, 因为前后端都有, 所以最终是决定通过 docker 部署, 并且通过 docker+jenkins 还能实现自动化部署.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

本文记录的是简单的一个 web 应用的部署, 学会这个之后其他应用部署其实都大同小异了, 使用的工具有: docker, nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

关于 docker 的学习, 如果不熟悉 docker 可以在自己电脑上下载一个有图形界面的 docker 客户端, 通过客户端+命令行来学习, 能更直观了解镜像 容器 应用之间的关系, 以及整个部署的流程.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

安装 docker

可以参考官方文档文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

  1. 卸载老版本 docker
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
  1. 设置仓库
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. 安装 docker
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  1. 启动 docker 服务
# 启动设置开机自启
systemctl start docker
systemctl enable docker
# 查看docker服务启动状态
systemctl status docker

构建镜像和容器

  1. 创建工作目录和项目文件
cd /home/workspace/
mkdir hello-world && cd hello-world
touch index.html && echo '<h1>hello world</h1>' >> index.html
  1. 通过 Dockerfile 来构建一个镜像
# 创建Dockerfile来构建镜像
vi Dockerfile
# 使用Nginx作为镜像
FROM nginx
# 将宿主主机的项目入口文件复制进容器里的nginx默认入口文件
COPY ./index.html /usr/share/nginx/html/index.html
# 容器对外暴露80端口
EXPOSE 80

构建镜像文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

docker image build ./ -t hello-world-image:1.0.0

docker 安装及简单 web 应用部署实战文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

  1. 通过镜像构建并启动容器
docker run -d --name hello-world-container -p 81:80 hello-world-image:1.0.0

启动完成后就可以打开[host]:81查看页面或者通过curl [host]:81查看文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

docker 安装及简单 web 应用部署实战文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

docker 的常用命令

镜像相关

# 查询镜像
docker search [image-name]
# 拉取镜像
docker pull [image-name]
# 导入镜像
docker load < /home/node.tar.gz
# 导出镜像
docker save > /home/node.tar.gz
# 查询所有镜像
docker images
# 修改镜像名
docker tag docker.io/node node
# 删除奖项
docker rmi [image-name]

容器相关

查看容器状态文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

docker ps -a

创建容器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

# 不挂载宿主目录
docker run -d --name demo-nginx -p 81:80 nginx
# 挂载宿主目录
docker run \
-p 80:80 \
--name homepage-nginx \
-v /home/workspace/demo/nginx.conf:/etc/nginx/nginx.conf \
-d nginx:latest

重启 暂停 删除容器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

# 重启容器
docker restart [container-name]
# 暂停容器, 容器名称, 容器id需要前3位数字
docker stop [container-name]
# 删除容器
docker rm [container-name]
# 删除正在运行的容器
docker rm -f [container-name]

进入容器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

docker exec -it [container-name] bash

-it 以交互式进入容器, bash 保留为容器终端的输入形式文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

查看日志文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

docker logs [container-name]

总结

使用docker部署流程文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

  1. 编写Dockerfile创建镜像
  2. 根据Dockerfile使用docker image build来创建镜像
  3. 使用docker run来启动容器

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html

作者:Conrad
来源:稀土掘金
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49639.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/49639.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定