docker安装
Ubuntu 16.04安装docker
参考文档地址
https://blog.csdn.net/jinking01/article/details/82490688
-
由于apt官方库里的docker版本可能比较旧,所以先卸载可能存在的旧版本
sudo apt-get remove docker docker-engine docker-ce docker.io
-
更新apt包索引
sudo apt-get update
-
安装以下包以使apt可以通过HTTPS使用存储库(repository)
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
-
添加Docker官方的GPG密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
-
使用下面的命令来设置stable存储库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
-
再更新一下apt包索引
sudo apt-get update
-
安装最新版本的Docker CE
sudo apt-get install -y docker-ce
验证docker
-
查看docker服务是否启动
systemctl status docker
显示以下内容表示已经启动(Active: active (running))
root@VM-63-138-ubuntu:/# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2019-04-26 22:27:25 CST; 1 day 16h ago Docs: https://docs.docker.com Main PID: 16555 (dockerd) Tasks: 39 Memory: 456.3M CPU: 1min 23.631s CGroup: /system.slice/docker.service ├─16555 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock └─23550 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 18090 -container-ip 172.17.0.2 Apr 27 16:14:25 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:14:25.100259085+08:00" level=error msg="No Apr 27 16:14:25 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:14:25.100720920+08:00" level=info msg="Ign Apr 27 16:14:42 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:14:42.339657251+08:00" level=error msg="No Apr 27 16:14:42 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:14:42.339721462+08:00" level=info msg="Ign Apr 27 16:14:51 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:14:51.420645513+08:00" level=info msg="ign Apr 27 16:18:20 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:18:20.661476977+08:00" level=error msg="No Apr 27 16:18:20 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:18:20.661535776+08:00" level=info msg="Ign Apr 27 16:18:40 VM-63-138-ubuntu dockerd[16555]: time="2019-04-27T16:18:40.369750759+08:00" level=info msg="ign
-
若未启动,则启动docker服务
sudo systemctl start docker
-
经典的hello world
sudo docker run hello-world
显示以下内容表示docker部署成功,并跑了一个hello-world的应用
root@VM-63-138-ubuntu:/# sudo 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/
docker安装Tomcat容器
这里tomcat容器值就是一个存放于docker的镜像
参考文档地址https://blog.csdn.net/qq_32351227/article/details/78673591
-
查找服务器的tomcat信息
docker search tomcat
root@VM-63-138-ubuntu:/# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 2363 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 65 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK] davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with… 34 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 28 [OK] cloudesire/tomcat Tomcat server, 6/7/8 14 [OK] meirwa/spring-boot-tomcat-mysql-app a sample spring-boot app using tomcat and My… 12 [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 fabric8/tomcat-8 Fabric8 Tomcat 8 Image 2 [OK] 99taxis/tomcat7 Tomcat7 1 [OK] camptocamp/tomcat-logback Docker image for tomcat with logback integra… 1 [OK] 1and1internet/debian-9-java-8-tomcat-8.5 Our tomcat 8.5 image 0 [OK] s390x/tomcat Apache Tomcat is an open source implementati… 0 picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAGER… 0 [OK] oobsri/tomcat8 Testing CI Jobs with different names. 0 cfje/tomcat-resource Tomcat Concourse Resource 0 jelastic/tomcat An image of the Tomcat Java application serv… 0 swisstopo/service-print-tomcat backend tomcat for service-print "the true, … 0
-
下载下来官方的镜像Starts最高的那个
docker pull docker.io/tomcat
-
查看docker所有的镜像
docker images
root@VM-63-138-ubuntu:/# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/tomcat latest 5a069ba3df4d 2 weeks ago 465MB hello-world latest fce289e99eb9 3 months ago 1.84kB
-
启动镜像
docker run -p 18090:18080 docker.io/tomcat
18090 表示当前镜像对外开放的端口,这个是自己随意命名的,只要不要其他端口冲突即可。
18080表示镜像里面tomcat的端口。默认是8080,这里是因为我自己改成了18080。
docker.io/tomcat表示的是镜像名称。使用docker image命令即可查看全部docker的镜像信息。
配置nginx
location /docker-tomcat/ {
proxy_pass http://localhost:18090/;
#解决宿主机能直接访问docker部署的应用,但是q不能访问nginx转
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
在宿主机的nginx中加上上面的配置,以方便访问。这里不能只配置一个proxy_pass。需要写成和上面一样的配置。不然外部系统访问不到docker部署的应用系统,只能docker的宿主机才能访问到。这里的18090和刚刚启动镜像时,自己命名的端口应该一致。如果忘记了可以使用docker ps命令来查看
root@VM-63-138-ubuntu:/usr/local# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc63cd12c064 docker.io/tomcat "catalina.sh run" 24 hours ago Up 6 hours 8080/tcp, 0.0.0.0:18090->18080/tcp app
- 访问tomcat
http://ip/dokcer-tomcat 即可验证tomcat是否启动。
部署应用
-
本地打包并上传到服务器。
这里需要注意的是,如果数据库和docker放在同一个服务器上的。不要使用localhost或者127.0.0.1。应该使用服务器的ip地址。不然docker的镜像(这里指的是docker.io/tomcat)会连接不到数据库。
-
进入镜像(CONTAINER ID:fc63cd12c064通过docker ps命令查询)
docker exec -it fc63cd12c064 /bin/bash
root@VM-63-138-ubuntu:/usr/local# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fc63cd12c064 docker.io/tomcat "catalina.sh run" 24 hours ago Up 6 hours 8080/tcp, 0.0.0.0:18090->18080/tcp app root@VM-63-138-ubuntu:/usr/local# docker exec -it fc63cd12c064 /bin/bash root@fc63cd12c064:/usr/local/tomcat#
-
退出镜像exit。把war包丢到宿主机,在丢到docker镜像docker.io/tomcat的tomcat/webapps(/usr/local/tomcat/webapps是默认固定的目录)
docker cp NginxDemo.war fc63cd12c064:/usr/local/tomcat/webapps
-
启动/重启tomcat镜像【容器id】
docker run -p 18090:18080 docker.io/tomcat
但其实我是进入镜像之后在/usr/local/tomcat/bin路径下去启动tomcat的,跟平时使用tomcat一样。
-
查看启动日志
进入docker.io/tomcat镜像之后,查看/usr/local/tomcat/logs下面的日志文件即可。
-
应用访问
http://服务器ip/nginx配置目录/项目名称(webapps文件夹下项目文件夹的名称)/项目api