Day03_12_Docker教程之Docker-Compose构建tomcat与mysql教程

Docker-Compose构建tomcat与mysql教程

一. docker方式运行tomcat镜像

1. 查找 Docker Hub 上的 Tomcat 镜像

root@sun:/usr/local# docker search tomcat
NAME                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   2403                [OK]                
tomee                         Apache TomEE is an all-Apache Java EE certif…   66                  [OK]                
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   53                                      [OK]
bitnami/tomcat                Bitnami Tomcat Docker Image                     28                                      [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      23                                      
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              16                                      [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            14                                      [OK]
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              11                                      [OK]
tutum/tomcat                  Base docker image to run a Tomcat applicatio…   11                                      
jeanblanchard/tomcat          Minimal Docker image with Apache Tomcat         8                                       
arm32v7/tomcat                Apache Tomcat is an open source implementati…   6                                       
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl…   4                                       [OK]
maluuba/tomcat7-java8         Tomcat7 with java8.                             3                                       
amd64/tomcat                  Apache Tomcat is an open source implementati…   2                                       
arm64v8/tomcat                Apache Tomcat is an open source implementati…   2                                       
ppc64le/tomcat                Apache Tomcat is an open source implementati…   1                                       
99taxis/tomcat7               Tomcat7                                         1                                       [OK]
camptocamp/tomcat-logback     Docker image for tomcat with logback integra…   1                                       [OK]
i386/tomcat                   Apache Tomcat is an open source implementati…   1                                       
s390x/tomcat                  Apache Tomcat is an open source implementati…   0                                       
oobsri/tomcat8                Testing CI Jobs with different names.           0                                       
jelastic/tomcat               An image of the Tomcat Java application serv…   0                                       
cfje/tomcat-resource          Tomcat Concourse Resource                       0                                       
picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAGER…   0                                       [OK]
secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                                       [OK]

拉取官方的tomcat镜像

docker pull tomcat

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 tomcat 的镜像.

2. 运行tomcat容器

docker run --name tomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat

命令说明:

  • -p 8080:8080:将容器的8080端口映射到主机的8080端口
  • -v $PWD/test:/usr/local/tomcat/webapps/test:将主机中当前目录下的test挂载到容器的/test

查看容器启动情况

yyg@sun:/usr/local/docker$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
a1b17e8403f1        tomcat              "catalina.sh run"        About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp   tomcat
edf6f80ea88f        registry            "/entrypoint.sh /etc…"   8 hours ago          Up 8 minutes        0.0.0.0:5000->5000/tcp   registry

通过浏览器访问

二. Docker-Compose方式运行Tomcat镜像

1. 编写docker-compose.yml配置文件

version: '3.1'
services:
  web:
    restart: always
    image: tomcat
    container_name: web
    ports:
      - 8080:8080
    volumes:
      - /usr/local/docker/tomcat/webapps/test:/usr/local/tomcat/webapps/test
    environment:
      TZ: Asia/Shanghai

2. 以docker-compose方式启动tomcat服务器

2.1 前台运行

docker-compose up

2.2 后台运行

docker-compose up -d

2.3 启动

docker-compose start

2.4 停止

docker-compose stop

2.5 停止并移除容器

docker-compose down

2.6 跟踪查看启动日志

docker-compose logs -f

三. docker-compose部署mysql

1. 获取mysql镜像

docker pull mysql

2. 创建目录

mkdir -p /usr/local/docker/mysql/data
cd /opt/docker/mysql/

3. 配置yaml文件

cd /usr/local/docker/mysql/
vim docker-compose.yml

version: '3'
services:
  mysql:
    restart: always
    image: mysql
    container_name: mysql
    ports:
      - 3307:3307
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: 123456
    command:
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DI
VISION_BY_ZERO"
    volumes:
     - /usr/local/docker/mysql/data:/var/lib/mysql

4. 后台方式启动

docker-compose -f docker-compose.yml up -d

 

你可能感兴趣的:(Linux,Docker,容器化)