简述: 在狂神说学习Docker,本文章主要记录一些项目的实战。

本文初发于 “曾晨de小站” zengchen233.cn,同步转载于此。

注:本文假设阅读者会使用Docker。

Nginx部署

搜索并下载镜像

[root@zengchen ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15903     [OK]
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2098                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   820                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   285
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   160
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   147                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        144                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   110                  [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   79                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   61                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   57
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  54
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   28
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   25                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   22
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13
flashspys/nginx-static            Super Lightweight Nginx Image                   11                   [OK]
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   11
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
arnau/nginx-gate                  Docker image with Nginx with Lua enabled on …   1                    [OK]
[root@zengchen ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e5ae68f74026: Pull complete
21e0df283cd6: Pull complete
ed835de16acd: Pull complete
881ff011f1c9: Pull complete
77700c52c969: Pull complete
44be98c0fab6: Pull complete
Digest: sha256:92a41cec180dd3cc645e5a8968d41b4aba786c4956e20020212e1d5636a7eda5
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

详细信息可以去官网查看

运行测试

[root@zengchen html]# docker run -d -p 8090:80 --name nginx -v /home/nginx/html:/usr/share/nginx/html nginx
[root@zengchen html]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
04e68d8e70e7   nginx     "/docker-entrypoint.…"   57 seconds ago   Up 56 seconds   0.0.0.0:8090->80/tcp   nginx

可以通过curl localhost:8090来查看网页源代码。

Tomcat部署

搜索并下载镜像

[root@zengchen ~]# docker search tomcat
NAME                          DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   3186      [OK]
tomee                         Apache TomEE is an all-Apache Java EE certif…   94        [OK]
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   58                   [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      31
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              18                   [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                   [OK]
arm32v7/tomcat                Apache Tomcat is an open source implementati…   11
andreptb/tomcat               Debian Jessie based image with Apache Tomcat…   10                   [OK]
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl…   7                    [OK]
arm64v8/tomcat                Apache Tomcat is an open source implementati…   6
unidata/tomcat-docker         Security-hardened Tomcat Docker container.      5                    [OK]
amd64/tomcat                  Apache Tomcat is an open source implementati…   3
jelastic/tomcat               An image of the Tomcat Java application serv…   2
cfje/tomcat-resource          Tomcat Concourse Resource                       2
oobsri/tomcat8                Testing CI Jobs with different names.           2
fabric8/tomcat-8              Fabric8 Tomcat 8 Image                          2                    [OK]
camptocamp/tomcat-logback     Docker image for tomcat with logback integra…   1                    [OK]
picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAGER…   1                    [OK]
ppc64le/tomcat                Apache Tomcat is an open source implementati…   1
chenyufeng/tomcat-centos      tomcat基于centos6的镜像                              1                    [OK]
99taxis/tomcat7               Tomcat7                                         1                    [OK]
secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                    [OK]
softwareplant/tomcat          Tomcat images for jira-cloud testing            0                    [OK]
s390x/tomcat                  Apache Tomcat is an open source implementati…   0

运行容器

[root@zengchen ~]# docker run -d -p 8090:8080 --name tomcat -v                               /home/tomcat/test:/usr/local/tomcat/webapps/test tomcat:9.0
920314ff05613f32ccd8167b4aedcc867a0ead219371ec04f708784c51b3f036

可以首先进入容器 docker exec -it 62d770e36ad9a5 /bin/bash

找到tomcat的根目录,你会发现webapps下面什么东西都没有,这时候就要执行一条命令:cp -r webapps.dist/* webapps/

root@62d770e36ad9:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager

然后东西就全部进来了。

这个时候就可以curl localhost:8090

[root@zengchen ~]# curl localhost:8090
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.55</h3></body></html>

创建自己的镜像

编写Dockerfile

这里的路径为/home/tomcat

FROM centos

MAINTAINER zengchen<1239571995@qq.com>

COPY README.txt /usr/local/README.txt

ADD jdk-8u311-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.55.tar.gz /usr/local

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_311
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.55
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.55

ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.55/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.55/bin/logs/catalina.out

这里的tomcat和jdk就需要自己去下载了。

创建镜像

docker build -t zengchen233/dittomcat:1.0 .

运行镜像

docker run -d -p 8090:8080 --name diytomcat -v /home/tomcat/webapps/自己项目的路径:/usr/local/apache-tomcat-9.0.55/webapps/自己项目的路径 -v /home/tomcat/webapps/tomcatlogs/:/usr/local/apache-tomcat-9.0.55/logs diytomcat

这个时候就可以curl localhost:8090