使用Puppet对Web应用程序进行Docker化

本文介绍了如何使用Docker和Puppet将Web应用程序,特别是Hello-Spring应用,部署到容器中。讨论了Docker与虚拟机的区别,并详细阐述了从创建Dockerfile和Puppetfile,到构建和运行Docker容器的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装Puppet模块– Librarian Puppet帖子中,我们提供了一个Vagrant VM,以使用librarian-puppetPuppet Forge安装puppet模块,现在在这篇文章中,我们将做同样的事情,除了我们提供Docker容器而不是Vagrant VM并部署一个hello-Spring在预配容器中的应用。

在开始在Docker容器中进行应用程序部署之前,让我们了解有关Docker及其与虚拟机的区别的简要介绍。

什么是Docker?

Docker是Linux容器的高级抽象,它管理容器的生命周期。 Docker允许将具有所有依赖关系的应用程序打包到用于软件开发的标准化单元中。 而且,如果该设备在您本地运行,我们可以保证它会以完全相同的方式运行,从质量检查到阶段到生产环境。

Docker与虚拟机

虚拟化技术(例如VirtualBox,VMWare,KVM等)使用完整的机器虚拟化,而Docker共享主机内核和操作系统实例的某些部分,从而以较低的隔离性为代价提供了较低的开销。 此外,与提供我们完整的新虚拟机的虚拟化技术相比,Docker使用LXC主机上Linux内核中的Cgroups功能提供了一个拥有自己的CPU,内存,块I / O,网络等空间的虚拟环境。 现在,让我们开始在容器中进行部署,请执行以下步骤:

步骤1:创建与我的任何名称的目录它的码头工人和文件名称为DockerfilePuppetfile目录内,如下所示:

$ mkdir docker
$ cd docker
$ touch Dockerfile Puppetfile

上面创建的Dockerfile包含用户可以在命令行上调用以组装映像的所有命令。

上面创建的Puppetfile是基于Ruby的DSL,用于指定应在何处安装Puppet模块以及从何处获取模块。

步骤2:将以下内容复制到Puppetfile中

# Puppetfile Configuration for librarian-puppet.
forge "http://forge.puppetlabs.com"
mod "puppet-java8", :git => "https://github.com/arpitaggarwal/puppet-java8.git"
mod "arpitaggarwal/tomcat"

步骤3:在Dockerfile中复制以下内容

FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get -y update
RUN apt-get -q -y install git
RUN sudo apt-get install -y ruby
RUN apt-get install -y ruby-dev
RUN apt-get -y update
RUN apt-get install -y make
RUN apt-get install -y build-essential
RUN apt-get install -y puppet
RUN gem install librarian-puppet
ADD Puppetfile /
RUN librarian-puppet install
RUN puppet apply --modulepath=/modules -e "include java8 class { 'tomcat':version => '7',java_home => '/usr/lib/jvm/java-8-oracle'}"
RUN apt-get remove -y make puppet build-essential ruby-dev

步骤4:在docker目录中构建应用程序,对我来说,它是从github克隆的hello-spring:

$ git clone https://github.com/arpitaggarwal/hello-spring.git
$ cd hello-spring
$ mvn clean install

第5步:使用Docker COPY命令更新Dockerfile,添加您的.war文件位置:

COPY hello-spring/target/hello-spring.war /var/lib/tomcat7/webapps/
EXPOSE 8080

上面指定的EXPOSE命令指的是适用于容器间通信的Docker命令。

最后, Dockerfile应该如下所示

FROM ubuntu:latest
MAINTAINER arpitaggarwal "aggarwalarpit.89@gmail.com"
RUN apt-get -y update
RUN apt-get -q -y install git
RUN sudo apt-get install -y ruby
RUN apt-get install -y ruby-dev
RUN apt-get -y update
RUN apt-get install -y make
RUN apt-get install -y build-essential
RUN apt-get install -y puppet
RUN gem install librarian-puppet
ADD Puppetfile /
RUN librarian-puppet install
RUN puppet apply --modulepath=/modules -e "include java8 class { 'tomcat':version => '7',java_home => '/usr/lib/jvm/java-8-oracle'}"
RUN apt-get remove -y make puppet build-essential ruby-dev
COPY hello-spring/target/hello-spring.war /var/lib/tomcat7/webapps/
EXPOSE 8080

第6步:使用Docker build命令构建Docker容器:

docker build --no-cache=true -t spring-application-container .

上面指定的spring-application-container是指docker映像的名称。

步骤7:构建完成后,使用Docker run命令运行容器:

docker run -p 8080:8080 --cap-add SYS_PTRACE -it spring-application-container /bin/bash

第8步:使用docker-machine获取您的Docker容器IP地址:

docker-machine ip your_vm_name

接下来,打开网址http:// container-ip-address:8080 / hello-spring

翻译自: https://www.javacodegeeks.com/2015/12/dockerizing-web-application-puppet.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值