1. 程式人生 > >Docker實踐-安裝Docker並在容器裡執行tomcat

Docker實踐-安裝Docker並在容器裡執行tomcat

隨著微服務的流行,Docker越來越流行,正如它的理念”Build, Ship, and Run Any App, Anywhere”一樣,Docker提供的容器隔離技術使得開發人員不用再去理清server裡的各種環境配置,輕鬆把應用執行起來。我們只需把執行環境的配置和應用封裝在Docker的映象(image),然後使用Docker執行這個映象即可。Docker可以說是給所有開發人員的一個福利包,學習和使用Docker是所有開發人員的標配技能。
文章來源:http://blog.csdn.net/massivestars/article/details/54357293

安裝Docker

yum install
docker

本文使用的系統是centos7,ubuntu使用以下命令
sudo apt-get update
sudo apt-get install docker-engine

如果報了以下錯誤,是因為yum被其它程序使用了

Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: PackageKit
    Memory :  12 M RSS (924 MB VSZ)
    Started: Mon Jan  2 17:22:13 2017
- 1 day(s) 1:06:13 ago State : Sleeping, pid: 16208

檢視正在yum使用的程序

ps -ef|grep yum

kill掉它即可

kill -9 16208

安裝完成,檢視安裝是否成功

docker info        #檢視docker的情況 
docker --version   #檢視docker的版本

啟動Docker服務

service docker start

啟動Docker的hello-world

從Docker Hub下載一個hello-world映象

docker pull hello-world

執行hello-world映象

docker run
hello-world

輸出以下資訊

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

至此,我們已成功執行起第一個Docker容器

tomcat執行環境

1、搜尋Docker Hub裡的tomcat映象

docker search tomcat
  • 部分搜尋結果如下
NAME                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                      Apache Tomcat is an open source implementa...   1132      [OK]
dordoka/tomcat              Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   29                   [OK]
cloudesire/tomcat           Tomcat server, 6/7/8                            12                   [OK]
davidcaste/alpine-tomcat    Apache Tomcat 7/8 using Oracle Java 7/8 wi...   11                   [OK]
andreptb/tomcat             Debian Jessie based image with Apache Tomc...   6                    [OK]

這裡寫圖片描述

上面 “7.0.73-jre7, 7.0-jre7, 7-jre7, 7.0.73, 7.0, 7”等等 是這個tomcat庫支援的tag(標籤),這裡我們選用的是 “7” 這個標籤

2、拉取Docker Hub裡的映象

docker pull tomcat:7

3、拉取完成後檢視本地的映象

docker images #所有映象
docker image tomcat:7  #檢視REPOSITORY為tomcat:7的映象

4、執行tomcat映象

docker run tomcat:7
  • 使用以下命令來檢視正在執行的容器
docker ps
  • 若埠被佔用,可以指定容器和主機的對映埠
docker run -p 8081:8080 tomcat:7 

5、執行我們的web應用

假設我們應用是www,目錄位置在/app/deploy/www

docker run --privileged=true -v /app/deploy/www:/usr/local/tomcat/webapps/www  -p 8081:8080 tomcat:7 

-v /app/deploy/www:/usr/local/tomcat/webapps/www 是把/app/deploy/www的目錄掛載至容器的/usr/local/tomcat/webapps/www。
–privileged=true是授予docker掛載的許可權

至此,已成功把web應用部署在Docker容器執行

常用命令

# 檢視所有映象
docker images

# 正在執行容器
docker ps

# 檢視docker容器
docker ps -a

# 啟動tomcat:7映象
docker run -p 8080:8080 tomcat:7

# 以後臺守護程序的方式啟動
docker run -d tomcat:7

# 停止一個容器
docker stop b840db1d182b

# 進入一個容器
docker attach d48b21a7e439

# 進入正在執行容器並以命令列互動
docker exec -it e9410ee182bd /bin/sh

# 以互動的方式執行
docker run -i -t -p 8081:8080 tomcat:7 /bin/bash