# 第一个docker化的java应用 **Repository Path**: Yenn-2017_admin/docker_demo1 ## Basic Information - **Project Name**: 第一个docker化的java应用 - **Description**: 练习 第一个docker化的java应用 https://www.imooc.com/learn/824 - **Primary Language**: Shell - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-09 - **Last Updated**: 2021-10-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: Docker ## README # 第一个docker化的java应用 #### 介绍 练习 第一个docker化的java应用 https://www.imooc.com/learn/824 简介:Docker是一个使用Go语言开发的开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的机器上。Docker的发展速度和火爆程度着实令人惊叹,一发不可收拾,形成了席卷整个IT界的新浪潮。学完本课程你将了解到什么是docker,docker的思想以及诸如镜像,仓库,容器等核心概念。你将学会怎样运行一个容器,如何搭建私有仓库,怎么写dockerfile以及怎样把自己的应用放到容器中运行。docker将会是你的IT路上一笔不小的财富。 #### 第1章 课程介绍 对docker有个简单的印象,了解课程的安排。 #### 1-1 课程介绍 (03:10) 1. 课程内容 + 什么是docker + 理解Docker + 走进Docker + Hello World + Nginx服务 + docker化的Java web #### 1-2 什么是docker (03:58) 1. Docker历史 + 2010 dotCloud PAAS + 2013 docker开源 + 2014.6 Docker 1.0 + 2014.7 C轮$4000万 + 2015.4 D轮$9500万 + 至今 Docker 1.13 2. 什么是Docker ? https://www.docker.com + Docker is the world's leading software containerization platform + Docker公司开发,开源,托管在github + 跨平台 ,支持Windows、Macos、 Linux #### 第2章 了解docker 用形象的类比说明docker的集装箱、标准化、隔离的思想。在用几个工作学习中碰到的问题说明docker解决了哪些问题。 #### 2-1 docker思想 (04:46) 1. 集装箱 2. 标准化 + 运输方式 + 存储方式. + API接口 3. 隔离 #### 2-2 docker解决了什么问题 (05:26) + 我本地运行没问题啊! + 系统好卡,哪个哥们又写死循环了? ! + 双11来了,服务器撑不住啦! #### 第3章 走进docker 结合上面的类比引出docker的核心技术:镜像、仓库和容器的概念,并分别深入讲解技术、原理。 #### 3-1 docker核心技术 (01:30) + docker ship run #### 3-2 docker镜像 (03:14) 1. #### 3-3 docker容器 (02:11) #### 3-4 docker仓库 (03:49) + hub.docker.com + c.163.com #### 第4章 docker安装 分别在三中平台上讲解docker的安装。同学可以选择自己的平台观看。 最近学习 #### 4-1 window安装docker (03:51) + Win10之外: 下载地址: https://www.docker.com/products/docker-toolbox + Win10: 下载地址: https://www.docker.com/products/docker#/windows #### 4-2 macos安装docker (02:46) + Yosemite 10.10.3+(2014/6): 下载地址: https://www.docker.com/products/docker#/mac + 旧版本 下载地址https://www.docker.com/products/docker-toolbox #### 4-3 linux安装docker (03:50) + Redhat & CentOS: 系统要求: 64-bit OS and version 3.10 + Ubuntu 系统要求: 64-bit OS and version 3.10 1. Redhat & CentOS: + http://www.imooc.com/article/16448 2. ubuntu + apt-get update + apt-get install -y docker.io + service docker start + docker version #### 第5章 docker初体验 第一个实例:用helloworld镜像带入,熟悉docker最基本的两个命令,拉取镜像和运行容器,并讲解背后运行逻辑。 #### 5-1 第一个docker镜像 (04:19) + docker pull [OPTIONS] NAME[:TAG] + docker images [OPTIONS] [REPOSITORY[:TAG]] > root@ubuntu:/opt/docker/ghost# docker images root@ubuntu:/opt/docker/ghost# docker pull hello-world Using default tag: latest #### 5-2 第一个docker容器 (04:38) + docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG..] > root@ubuntu:/opt/docker/ghost# 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. (amd64) 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. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ #### 第6章 docker运行nginx静态网站 第二个实例:从运行nginx镜像引出docker网络概念和docker的端口映射,最后运行nginx容器。 #### 6-1 运行nginx镜像 (08:20) 1. 实践前奏 + 持久运行的容器 + 前台挂起&后台运行 + 进入容器内部 > root@ubuntu:/opt/docker/nginx1# docker pull library/nginx:latest root@ubuntu:/opt/docker/nginx1# docker run -d nginx 0f661de2ca356d50d4282bb277ab3805fd87341f91bc10e1edab72370d9b2a57 root@ubuntu:/opt/docker/nginx1# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0f661de2ca35 nginx "/docker-entrypoint.…" 19 seconds ago Up 19 seconds 80/tcp competent_northcutt root@ubuntu:/opt/docker/nginx1# docker exec -it 0f661de2ca35 bash root@0f661de2ca35:/# root@0f661de2ca35:/# which nginx /usr/sbin/nginx root@0f661de2ca35:/# ps -ef bash: ps: command not found root@0f661de2ca35:/# exit exit root@ubuntu:/opt/docker/nginx1# #### 6-2 docker网络 (08:19) + 网络类型 - Bridge、Host、None . + 端口映射 > root@ubuntu:/opt/docker/nginx1# docker run -d -p 8080:80 nginx d70462bea0855ba2e515532f404efead44140e3b418384db049f6abade44b63f root@ubuntu:/opt/docker/nginx1# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d70462bea085 nginx "/docker-entrypoint.…" 20 seconds ago Up 19 seconds 0.0.0.0:8080->80/tcp eloquent_nightingale root@ubuntu:/opt/docker/nginx1# netstat -na|grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN root@ubuntu:/opt/docker/nginx1# curl http://localhost:8080 root@ubuntu:/opt/docker/nginx1# docker run -d -P nginx 2eaf798b6ccfd747cee36784b7993d537d30029d92b784f89b7f33a05c58bd0c root@ubuntu:/opt/docker/nginx1# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2eaf798b6ccf nginx "/docker-entrypoint.…" 7 seconds ago Up 2 seconds 0.0.0.0:49153->80/tcp compassionate_hoover #### 第7章 第一个java web应用 最后一个实例:创建自己的镜像,引出dockerfile,讲解基本的dockerfile语法。然后讲解私有仓库的搭建。最后分别在两台机器上演示docker的跨平台运行我们的java web项目。 #### 7-1 制作自己的镜像 (08:45) + Dockerfile + docker build + Jpress: http://jpress.io/ 1. 网易镜像【需要登录】:https://c.163yun.com/hub#/home > root@ubuntu:/opt# cd jpress root@ubuntu:/opt/jpress# ll total 8 drwxr-xr-x 2 root root 4096 Jun 9 19:16 ./ drwxr-xr-x 5 root root 4096 Jun 9 19:16 ../ root@ubuntu:/opt/jpress# ll total 65892 drwxr-xr-x 2 root root 4096 Jun 9 19:17 ./ drwxr-xr-x 5 root root 4096 Jun 9 19:16 ../ -rw-r--r-- 1 root root 67461537 Jun 9 19:18 jpress-v4.0.2.war root@ubuntu:/opt/jpress# chmod +x jpress-v4.0.2.war root@ubuntu:/opt/jpress# ll total 65892 drwxr-xr-x 2 root root 4096 Jun 9 19:17 ./ drwxr-xr-x 5 root root 4096 Jun 9 19:16 ../ -rwxr-xr-x 1 root root 67461537 Jun 9 19:18 jpress-v4.0.2.war* root@ubuntu:/opt/jpress# chmod 777 jpress-v4.0.2.war root@ubuntu:/opt/jpress# ll total 65892 drwxr-xr-x 2 root root 4096 Jun 9 19:17 ./ drwxr-xr-x 5 root root 4096 Jun 9 19:16 ../ -rwxrwxrwx 1 root root 67461537 Jun 9 19:18 jpress-v4.0.2.war* root@ubuntu:/opt/jpress# hub.c.163.com/library/tomcat:latest bash: hub.c.163.com/library/tomcat:latest: No such file or directory root@ubuntu:/opt/jpress# docker pull hub.c.163.com/library/tomcat:latest latest: Pulling from library/tomcat 9af7279b9dbd: Pull complete 31816c948f2f: Pull complete c59a1cdf83d3: Pull complete 232c7a75d568: Pull complete de412d312979: Pull complete 80315ba34693: Pull complete 5d3f97bd90e8: Pull complete dc8dc63f6baa: Pull complete f6c6e2d67f03: Pull complete 9123b340aa92: Pull complete 76abaea2279d: Pull complete 4476602e3346: Pull complete 12e1fda011bd: Pull complete Digest: sha256:db1a8ca2fe44449d265e5505f300be6f34fc63211a5506400a0a8c24653af91f Status: Downloaded newer image for hub.c.163.com/library/tomcat:latest hub.c.163.com/library/tomcat:latest root@ubuntu:/opt/jpress# root@ubuntu:/opt/jpress# docker build . Sending build context to Docker daemon 67.46MB Step 1/3 : from hub.c.163.com/library/tomcat ---> 72d2be374029 Step 2/3 : MAINTAINER yenn2019 xxx@126.com ---> Running in 9b7aafcc3d0a Removing intermediate container 9b7aafcc3d0a ---> ef4a4a539b72 Step 3/3 : COPY jpress.war /usr/local/tomcat/webapps ---> c73e17446711 Successfully built c73e17446711 root@ubuntu:/opt/jpress# docker images REPOSITORY TAG IMAGE ID CREATED SIZE c73e17446711 33 seconds ago 360MB ghost_ghost-app latest 2729d45b5292 18 hours ago 456MB ghost_nginx latest b20fa18dcc96 18 hours ago 133MB fa9c82c90ecf 19 hours ago 456MB c4ba1dbe8955 19 hours ago 133MB 2158a5657620 19 hours ago 456MB ghost latest 2f5360e7440c 28 hours ago 456MB nginx latest d1a364dc548d 2 weeks ago 133MB mysql latest c8562eaf9d81 4 months ago 546MB mysql 5.6 6e68afc1976f 4 months ago 302MB myregistry/mymysql 1.0 6e68afc1976f 4 months ago 302MB hub.c.163.com/library/tomcat latest 72d2be374029 3 years ago 292MB mysql 5.7.15 18f13d72f7f0 4 years ago 383MB root@ubuntu:/opt/jpress# docker build -t jpress:latest . Sending build context to Docker daemon 67.46MB Step 1/3 : from hub.c.163.com/library/tomcat ---> 72d2be374029 Step 2/3 : MAINTAINER yenn2019 xxx@126.com ---> Using cache ---> ef4a4a539b72 Step 3/3 : COPY jpress.war /usr/local/tomcat/webapps ---> Using cache ---> c73e17446711 Successfully built c73e17446711 Successfully tagged jpress:latest root@ubuntu:/opt/jpress# docker images REPOSITORY TAG IMAGE ID CREATED SIZE jpress latest c73e17446711 About a minute ago 360MB ghost_ghost-app latest 2729d45b5292 18 hours ago 456MB ghost_nginx latest b20fa18dcc96 18 hours ago 133MB fa9c82c90ecf 19 hours ago 456MB c4ba1dbe8955 19 hours ago 133MB 2158a5657620 19 hours ago 456MB ghost latest 2f5360e7440c 28 hours ago 456MB nginx latest d1a364dc548d 2 weeks ago 133MB mysql latest c8562eaf9d81 4 months ago 546MB mysql 5.6 6e68afc1976f 4 months ago 302MB myregistry/mymysql 1.0 6e68afc1976f 4 months ago 302MB hub.c.163.com/library/tomcat latest 72d2be374029 3 years ago 292MB mysql 5.7.15 18f13d72f7f0 4 years ago 383MB #### 7-2 运行自己的容器 (07:03) > root@ubuntu:/opt/jpress# docker run -d -p 8888:8080 jpress fccd8c5ef8f2480a292143ee12a1336fcd61401db1320eaa821b705e581b5811 root@ubuntu:/opt/jpress# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fccd8c5ef8f2 jpress "catalina.sh run" 9 seconds ago Up 7 seconds 0.0.0.0:8888->8080/tcp magical_kepler root@ubuntu:/opt/jpress# netstat -na | grep 8888 tcp 0 0 127.0.0.1:50712 127.0.0.1:8888 TIME_WAIT tcp6 0 0 :::8888 :::* LISTEN tcp6 0 0 127.0.0.1:8888 127.0.0.1:50704 TIME_WAIT tcp6 0 0 127.0.0.1:8888 127.0.0.1:50700 TIME_WAIT root@ubuntu:/opt/jpress# docker pull hub.c.163.com/library/mysql:latest root@ubuntu:/opt/jpress# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=000000 hub.c.163.com/library/mysql:latest c728bfddda242c39c5f6ce5806c2f01e593f05c142514ac32a533c8635fbaf36 root@ubuntu:/opt/jpress# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c728bfddda24 hub.c.163.com/library/mysql:latest "docker-entrypoint.s…" 12 seconds ago Up 11 seconds 0.0.0.0:3306->3306/tcp distracted_mclaren fccd8c5ef8f2 jpress "catalina.sh run" 13 minutes ago Up 13 minutes 0.0.0.0:8888->8080/tcp magical_kepler root@ubuntu:/opt/jpress# docker stop c72 c72 root@ubuntu:/opt/jpress# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=000000 -e MYSQL_DATABASE=jpress hub.c.163.com/library/mysql:latest 22c2d5d630700351818bab45a96689fbf04995676aaceaf64e76ebcb1149d10a #### 第8章 课程总结 课程总结 #### 8-1 课程总结 (03:03) 1. 内容回顾 + 集装箱,标准化,隔离 + 镜像容器仓库(BUILD SHIP RUN)