使用Dockerfile搭建各种环境

本文详细介绍如何使用Dockerfile搭建Tomcat、Nginx服务及SpringBoot应用,涵盖Docker安装、镜像构建、容器运行、Docker Compose使用等关键步骤。

使用Dockerfile搭建各种环境

2018年05月08日 16:39:35 阅读数:328 标签: Docker 更多

个人分类: Docker学习

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/jlnu_wanglei/article/details/78265365

安装Docker

我的操作系统:Ubuntu14.04,我们平时安装docker因为使用的是命令:

 
  1. $ sudo apt-get update

  2. $ sudo apt-get install -y docker.io

  3. $ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

  4. $ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

如果使用操作系统自带的包安装Docker,使用的是比较旧的安装版本,要使用最新的docker推荐如下方式:
(1)使用阿里云容器提供的下载:
https://cr.console.aliyun.com/?spm=5176.100239.blogcont29941.12.DMQU50#/accelerator

(2)执行:curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
(3)这里阿里云还提供了Docker加速器,可以看到是修改配置文件,修改镜像地址,这里的镜像地址经过试验很慢,也是国外的,所以这里我们不使用他提供的加速器。我们使用:https://www.daocloud.io/mirror#
(4)执行:curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://aa09d22e.m.daocloud.io
经过测试这个镜像加速器还是很快的。

卸载docker,但是会保留镜像:

sudo apt-get purge docker.io

Dockerfile搭建tomcat服务

1.创建一个目录构建环境

mkdir test/web

2.上传tomcat和jdk到该目录下

 
  1. apache-tomcat-7.0.63.tar.gz

  2. jdk-7u76-linux-x64.tar.gz

3.编辑Dockerfile:vim Dockerfile

 
  1. #pull down centos image

  2. FROM centos

  3. MAINTAINER wanglei_dev@cyou-inc.com

  4.  
  5. #添加tomcat和jdk到镜像中

  6. ADD ./apache-tomcat-7.0.63.tar.gz /root

  7. ADD ./jdk-7u76-linux-x64.tar.gz /root

  8.  
  9. #设置环境变量

  10. ENV JAVA_HOME /root/jdk1.7.0_76

  11. ENV PATH $JAVA_HOME/bin:$PATH

  12.  
  13. #定义在容器启动之后的运行程序

  14. ENTRYPOINT /root/apache-tomcat-7.0.63/bin/startup.sh && tail -F /root/apache-tomcat-7.0.63/logs/catalina.out

4.构建镜像

 
  1. sudo docker build -t myCentos:tomcat-centos --rm=true .

  2. 此时屏幕输出如下:

  3. Sending build context to Docker daemon 470.4 MB

  4. Sending build context to Docker daemon

  5. Step 0 : FROM centos

  6. ---> d83a55af4e75

  7. Step 1 : MAINTAINER test@test.com

  8. ---> Running in 955747d64da5

  9. ---> 1619dc8f6d58

  10. ................

  11. 70/logs/catalina.out

  12. ---> Running in fe48acf12d70

  13. ---> 52076383f11b

  14. Removing intermediate container fe48acf12d70

  15. Successfully built 52076383f11b

注意:-t 选择指定生成镜像的用户名和标签 --rm=true 指定在生成镜像过程中删除中间产生的临时容器

5.查看新产生的镜像

 
  1. root@ubuntu:/# docker images

  2. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

  3. nginx_dockerfile centos 110a02c542ee 2 hours ago 357.9 MB

  4. mycentos tomcat-centos 50a61817c5a4 3 hours ago 502.4 MB

  5. ubuntu latest bd92ca350bbd 14 hours ago 122 MB

  6. centos latest 1c1b67b33c28 3 weeks ago 196.6 MB

6.运行镜像

 
  1. [root@wls12c web]$ docker run -d -p 8090:8080 50a6

  2. 8260fa22aa32126f613a6b64f121e78545ceae01da58c61396968bfafeda3706

注意:-p指定主机端口8090和容器端口8080绑定,即 -p 主机端口:容器端口 -d指定容器运行后与当前tty分离,后台运行

最终验证:通过访问:http://宿主机IP:8090,即可看见我们熟悉的tomcat首页了

Dockerfile搭建nginx服务(互联tomcat容器)

1.创建目录构建nginx环境

 
  1. mkdir -p /root/nginx_centos

  2. cd /root/nginx_centos

2.准备run.sh文件,用于启动ssh和apache服务脚本

 
  1. #!/bin/bash

  2. /usr/sbin/sshd &

  3. /usr/local/nginx/sbin/nginx

注意:这个.sh脚本,最好以#!/bin/bash,我试过写了一下备注,一直报错

3.编写Dockerfile文件

 
  1. #基于centos生成镜像

  2. FROM centos

  3.  
  4. #镜像创建者信息

  5. MAINTAINER wanglei_dev@cyou-inc.com

  6.  
  7. #切换目录

  8. WORKDIR /usr/local/src

  9. #安装wget

  10. RUN yum install -y wget

  11.  
  12. #下载并解压源码包

  13. RUN wget http://nginx.org/download/nginx-1.8.0.tar.gz

  14. RUN tar -zxvf nginx-1.8.0.tar.gz

  15. WORKDIR nginx-1.8.0

  16.  
  17. #编译安装nginx

  18. RUN yum install -y gcc make pcre-devel zlib-devel

  19. RUN ./configure --prefix=/usr/local/nginx --with-pcre

  20. RUN make

  21. RUN make install

  22.  
  23. #启动nginx服务

  24. RUN /usr/local/nginx/sbin/nginx

  25.  
  26. #修改nginx配置文件,以非daemon方式启动

  27. RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf

  28.  
  29. #复制服务器启动脚本并设置权限

  30. ADD run.sh /usr/local/sbin/run.sh

  31. RUN chmod 755 /usr/local/sbin/run.sh

  32.  
  33. #设置生成容器时需要执行的脚本

  34. CMD ["/usr/local/sbin/run.sh"]

  35. #开放22,80,443端口

  36. EXPOSE 22

  37. EXPOSE 80

  38. EXPOSE 443

注意:在Dockerfile文件中更换当前目录不使用cd,要用WORKDIR

4.根据Dockerfile生成镜像

docker build -t nginx_dockerfile:centos .

5.查看镜像

 
  1. root@ubuntu:/# docker images

  2. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

  3. nginx_dockerfile centos 110a02c542ee 2 hours ago 357.9 MB

6.根据镜像生成容器并测试

docker run -d --name myNginxDocker -p 2224:22 -p 8001:80 -p 4443:443 --link myDocker:tomcatDoker nginx_dockerfile:centos  

将容器的22端口、80端口和443端口分别映射到到宿主机上的2224端口、8001端口和4443端口 查看新生成的容器:docker ps -a 
测试:

curl localhost:8001

同时我们可以主机上访问会看到我们熟悉的nginx的欢迎界面

7.验证两个容器互通

登录docker容器:

docker exec -it myNginxDocker /bin/bash  

ping 我们刚才--link后面跟的myDocker:tomcatDoker,这里是容器名:别名,我们可以直接在nginx的容器中:

ping tomcatDoker

Docker Compose

1.简介

Compose

2.安装

 
  1. wanglei@ubuntu:~$ sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

  2. % Total % Received % Xferd Average Speed Time Time Time Current

  3. Dload Upload Total Spent Left Speed

  4. 100 617 0 617 0 0 522 0 --:--:-- 0:00:01 --:--:-- 523

  5. 0 0 0 0 0 0 0 0 --:--:-- 0:00:21 --:--:-- 0curl: (7) Failed to connect to github-production-release-asset-2e65be.s3.amazonaws.com port 443: Connection refused

我们可以看到由于网络原因,连接失败,那么我们可以自行下载相关版本移动到相关目录
使用echo命令输出下载地址:

 
  1. wanglei@ubuntu:~$ echo "https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m`"

  2. https://github.com/docker/compose/releases/download/1.16.1/docker-compose-Linux-x86_64

将下载的文件导入,移动到相关目录下,并修改权限:

 
  1. wanglei@ubuntu:~$ sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

  2. wanglei@ubuntu:/usr/local/bin$ sudo chmod +x /usr/local/bin/docker-compose

Docker构建,运行,发布一个springBoot应用

1.开放API(可选)

因为我们在开发阶段,要不停的构建发布项目,那么我们的测试机器就也要不停地发布构建,这样很麻烦,所以这里我们开放一个docker的API接口,通过这个接口连接到我们的Maven,这样就省去了很多构建的时间。
ubuntu下我们执行如下:

 
  1. vim /etc/default/docker

  2. DOCKER_OPTS="-H unix:///var/run/docker.sock -H 0.0.0.0:4243"

centos下我们执行如下:

 
  1. vim /etc/sysconfig/docker

  2. other_args="-H tcp://0.0.0.0:5555 -H unix:///var/run/docker.sock"

注意:0.0.0.0表示对所有IP都开放相应的端口

2.windows下设置DOCKER_HOST:

环境变量设置如下:

DOCKER_HOST=tcp://192.168.254.132:4243  

验证docker的端口是否已经开放:telnet ip 端口

3.创建springboot项目-pom文件设置插件

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5.  
  6. <groupId>com.example</groupId>

  7. <artifactId>helloworld</artifactId>

  8. <version>0.0.1-SNAPSHOT</version>

  9. <packaging>jar</packaging>

  10.  
  11. <name>helloworld</name>

  12. <description>Demo project for Spring Boot</description>

  13.  
  14. <parent>

  15. <groupId>org.springframework.boot</groupId>

  16. <artifactId>spring-boot-starter-parent</artifactId>

  17. <version>1.5.7.RELEASE</version>

  18. <relativePath/> <!-- lookup parent from repository -->

  19. </parent>

  20.  
  21. <properties>

  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  24. <java.version>1.8</java.version>

  25. <!--docker镜像前缀-->

  26. <docker.image.prefix>helloworld</docker.image.prefix>

  27. </properties>

  28.  
  29. <dependencies>

  30. <dependency>

  31. <groupId>org.springframework.boot</groupId>

  32. <artifactId>spring-boot-starter-web</artifactId>

  33. </dependency>

  34.  
  35. <dependency>

  36. <groupId>org.springframework.boot</groupId>

  37. <artifactId>spring-boot-starter-test</artifactId>

  38. <scope>test</scope>

  39. </dependency>

  40. </dependencies>

  41.  
  42. <build>

  43. <finalName>${project.name}</finalName>

  44. <plugins>

  45. <plugin>

  46. <groupId>org.springframework.boot</groupId>

  47. <artifactId>spring-boot-maven-plugin</artifactId>

  48. <configuration>

  49. <finalName>${project.name}</finalName>

  50. <fork>true</fork>

  51. </configuration>

  52. </plugin>

  53. <!--docker插件,docker-maven-plugi用于构建maven的Docker image-->

  54. <plugin>

  55. <groupId>com.spotify</groupId>

  56. <artifactId>docker-maven-plugin</artifactId>

  57. <version>0.4.3</version>

  58. <configuration>

  59. <!--指定镜像名称-->

  60. <imageName>${docker.image.prefix}/${project.artifactId}</imageName>

  61. <!--指定Dockerfile的位置-->

  62. <dockerDirectory>src/main/docker</dockerDirectory>

  63. <!--在构建镜像的时候使用到的文件需要和Dockerfile放在一起,一般应用jar包需要纳入-->

  64. <resources>

  65. <resource>

  66. <targetPath>/</targetPath>

  67. <directory>${project.build.directory}</directory>

  68. <include>${project.build.finalName}.jar</include>

  69. </resource>

  70. </resources>

  71. </configuration>

  72. </plugin>

  73. </plugins>

  74. </build>

  75. </project>

4.创建springboot项目-程序主入口

 
  1. @SpringBootApplication

  2. @RestController

  3. public class HelloworldApplication {

  4.  
  5. public static void main(String[] args) {

  6. SpringApplication.run(HelloworldApplication.class, args);

  7. }

  8.  
  9. @RequestMapping("/")

  10. public String home(){

  11. return "Hello Docker World";

  12. }

  13. }

5.运行程序确保程序没有问题

编译:mvn package
运行:java -jar target/docker-spring-boot-1.0.0.jar
访问项目: http://localhost:8080/

6.将项目容器化

Docker 使用 Dockerfile 文件格式来指定 image 层,编写Dockerfile,创建文件 src/main/docker/Dockerfile:

 
  1. #基于那个镜像

  2. FROM anapsix/alpine-java

  3. #FROM frolvlad/alpine-oraclejdk8:slim

  4.  
  5. #指定临时文件目录为/tmp

  6. VOLUME /tmp

  7.  
  8. #拷贝文件到目录中

  9. ADD helloworld-0.0.1-SNAPSHOT.jar app.jar

  10. RUN bash -c 'touch /app.jar'

  11.  
  12. #开放端口

  13. EXPOSE 8080

  14. #环境变量(BOOTAPP_OPTS用于传入Java运行时的变量,例如test,dev)

  15. ENV BOOTAPP_OPTS ""

  16.  
  17. #配置容器启动命令

  18. # docker run -d -p 8080:8080 -e "spring.profiles.active=dev" --name sample helloworld/helloworld

  19. # docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name sample helloworld/helloworld

  20. ENTRYPOINT java ${BOOTAPP_OPTS} -Djava.security.egd=file:/dev/./urandom -jar app.jar

7.构建Docker Image


运行docker Image:

docker run -p 8082:8080 -t springbootDocker

验证成功:访问宿主(主机IP)我们可以看到springboot的输出

转载于:https://my.oschina.net/u/3367404/blog/1932873

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值