服务计算——docker 应用容器化
文章目录
服务计算——docker 应用容器化
1. Docker 简介
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上, 也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
一个完整的Docker有以下几个部分组成:
- dockerClient客户端
- Docker Daemon守护进程
- Docker Image镜像
- DockerContainer容器
Docker的典型使用场景:
- Automating the packaging and deployment of applications(使应用的打包与部署自动化)
- Creation of lightweight, private PAAS environments(创建轻量、私密的PAAS环境)
- Automated testing and continuous integration/deployment(实现自动化测试和持续的集成/部署)
- Deploying and scaling web apps, databases and backend services(部署与扩展webapp、数据库和后台服务)
2. Docker 基本操作
2.1 安装配置 Docker
# 移除旧版本 Docekr
sudo yum remove docker*
# 设置 repository
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 使用阿里云镜像
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 不指定版本号 默认安装最新版
sudo yum install docker-ce
2.2 使用命令行搜索镜像
docker search + 镜像名称
2.3 下载镜像
docker search + 镜像名称
这里已经下载安装过(当时忘了截图)…
2.4 查看本地镜像列表
docker images
2.5 删除本地镜像
docker rmi [image id]
注:
其中[image id]
替换为具体的镜像image id
镜像删除时不能有该镜像的容器存在,无论是运行中的还是停止的
2.6 启动镜像成容器
docker run -it --rm -p 8888:8080 镜像名:latest
- 其中
-it
代表开启交互功能,即容器内tomcat启动日志你将能看到。 - 其中
—rm
代表当启动的容器停止后自动删除该容器而并非是镜像。 - 其中
-p 8888:8080
代表将容器中的8080端口映射到本地机器的8888端口上,即我们可以通过localhost:8888端口访问到镜像 ,甚至我可以改变本地端口来启动多个镜像 。 - 最后的
tomcat:latest
代表启动的容器名称及其版本标签。
2.7 查看运行中的容器
docker ps
2.8 停止运行中的容器
docker stop [container id]
其中[container id]
在执行时替换为具体容器的container id
2.9 查看停止状态的容器
docker ps -a
3. 针对本次实验做的工作
3.1 构建完整Docker file
FROM node:9.11.1-alpine
# install simple http server for serving static content
RUN npm install -g cnpm --registry=https://registry.npm.taobao.org && cnpm install -g http-server
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
# RUN npm install
RUN cnpm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
# build app for production with minification
RUN npm run build
EXPOSE 8080
CMD [ "http-server", "dist" ]
3.2 构建自定义镜像的基础镜像
FROM node:9.11.1-alpine
3.3 安装软件
# install simple http server for serving static content
RUN npm install -g cnpm --registry=https://registry.npm.taobao.org && cnpm install -g http-server
# install project dependencies
# RUN npm install
RUN cnpm install
# build app for production with minification
RUN npm run build
3.4 复制路径
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
3.5 切换工作路径
# make the 'app' folder the current working directory
WORKDIR /app
3.6 指定容器映射到主机端口
EXPOSE 8080
3.7 设置container启动时执行的操作
CMD [ "http-server", "dist" ]
3.8 在根目录下指令指令运行项目
# 位于项目的根目录下,但是由于代理问题这一步可能会出现部分配置文件安装超时问题,所以我们在docker file中更改了 npm 的源
docker build -t my-swapi-vue:v1
docker run -it -p 8080:8080 my-swapi-vue:v1c