【Docker】Docker部署实战
狂神老师的视频地址:https://space.bilibili.com/95256449
1、部署Nginx
#如果要部署指定版本的镜像建议到仓库中查看https://hub.docker.com/
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
42c077c10790: Pull complete
62c70f376f6a: Pull complete
915cc9bd79c2: Pull complete
75a963e94de0: Pull complete
7b1fab684d70: Pull complete
db24d06d5af4: Pull complete
Digest: sha256:2bcabc23b45489fb0885d69a06ba1d648aeda973fae7bb981bafbb884165e514
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
#查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 0e901e68141f 4 days ago 142MB
centos latest 5d0da3dc9764 8 months ago 231MB
#后台模式运行容器,并且配置暴露端口
[root@localhost ~]# docker run -d --name nginx01 -p 3344:80 nginx
b7cd3aeef060fa1657730fc23dcc04ff1a68ad43bad37e81a732af79c544cd84
#查看容器运行
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7cd3aeef060 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
#本机连接测试
[root@localhost ~]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7cd3aeef060 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
#进入容器
[root@localhost ~]# docker exec -it nginx01 /bin/bash
#查看容器配置文件
root@b7cd3aeef060:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
外网访问:服务器IP:3344
2、tomcat部署实战
官方的例子:
$ docker run -it --rm tomcat:9.0 #--rm,用完即删,一般用来测试
#下载再启动
[root@localhost ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
e756f3fdd6a3: Already exists
bf168a674899: Already exists
e604223835cc: Already exists
6d5c91c4cd86: Already exists
5e20d165240e: Already exists
1334d60df9a8: Already exists
16c2728dcd90: Already exists
05288798d23d: Already exists
c022dc2b2581: Pull complete
d86ac2f896ee: Pull complete
Digest: sha256:b4e84cff017ff5202cb760ccb1373dd950158f926d6afb04bd5e9f7337291501
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
#查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0 bce48a218e94 5 days ago 680MB
tomcat latest c795915cb678 5 days ago 680MB
nginx latest 0e901e68141f 5 days ago 142MB
#以后台方式运行
[root@localhost ~]# docker run -d -p 3355:8080 --name tomcat01 tomcat
6559dbe7dfb0bc2f02a460df83c3cc84e0a1ffe6d22c29c641c31f73f139a97a
#测试访问发现404
#进入容器
[root@localhost ~]# docker exec -it tomcat01 /bin/bash
root@6559dbe7dfb0:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@6559dbe7dfb0:/usr/local/tomcat# ls -al
total 132
drwxr-xr-x. 1 root root 30 May 28 21:48 .
drwxr-xr-x. 1 root root 20 May 28 21:45 ..
-rw-r--r--. 1 root root 19010 May 10 21:53 BUILDING.txt
-rw-r--r--. 1 root root 6210 May 10 21:53 CONTRIBUTING.md
-rw-r--r--. 1 root root 60269 May 10 21:53 LICENSE
-rw-r--r--. 1 root root 2333 May 10 21:53 NOTICE
-rw-r--r--. 1 root root 3398 May 10 21:53 README.md
-rw-r--r--. 1 root root 6908 May 10 21:53 RELEASE-NOTES
-rw-r--r--. 1 root root 16515 May 10 21:53 RUNNING.txt
drwxr-xr-x. 2 root root 4096 May 28 21:48 bin
drwxr-xr-x. 1 root root 22 Jun 3 02:49 conf
drwxr-xr-x. 2 root root 4096 May 28 21:48 lib
drwxrwxrwx. 1 root root 80 Jun 3 02:49 logs
drwxr-xr-x. 2 root root 159 May 28 21:48 native-jni-lib
drwxrwxrwx. 2 root root 30 May 28 21:48 temp
drwxr-xr-x. 2 root root 6 May 28 21:48 webapps
drwxr-xr-x. 7 root root 81 May 10 21:53 webapps.dist
drwxrwxrwx. 2 root root 6 May 10 21:53 work
#查看webapps,发现这个目录是空的,因为阿里云上面的tomcat是精简版的
root@6559dbe7dfb0:/usr/local/tomcat# cd webapps
root@6559dbe7dfb0:/usr/local/tomcat/webapps# ls
root@6559dbe7dfb0:/usr/local/tomcat/webapps#
#webapps的东西阿里云放到了webapps.dist里面,只需要把webapps.dist复制到webapps里面就好了
root@6559dbe7dfb0:/usr/local/tomcat# ls webapps.dist
ROOT docs examples host-manager manager
#复制webapps.dist到webapps里面
root@6559dbe7dfb0:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@6559dbe7dfb0:/usr/local/tomcat# ls webapps
ROOT docs examples host-manager manager
刷新,发现tomcat可以访问了