一. 首先在我们的服务器上安装Docker。
安装教程-任意门
我就罗列一下主要的几步:
- yum install -y yum-utils device-mapper-persistent-data lvm2
- yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- yum install -y docker-ce docker-ce-cli containerd.io
- yum list docker-ce --showduplicates | sort -r
[root@js-main ~]# yum list docker-ce --showduplicates | sort -r
Last metadata expiration check: 0:21:38 ago on Wed 17 Mar 2021 09:10:11 PM CST.
docker-ce.x86_64 3:20.10.5-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.1-3.el8 docker-ce-stable
docker-ce.x86_64 3:20.10.0-3.el8 docker-ce-stable
docker-ce.x86_64 3:19.03.15-3.el8 docker-ce-stable
docker-ce.x86_64 3:19.03.14-3.el8 docker-ce-stable
docker-ce.x86_64 3:19.03.13-3.el8 docker-ce-stable
- 选版本的时候根据第二列
:
后面,-
前面,中间一段,表示版本。举例:
yum install -y docker-ce-20.10.5 docker-ce-cli-20.10.5 containerd.io - systemctl start docker
- systemctl enable docker
- docker run hello-world
[root@js-main ~]# 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 version
可以查看当前的docker版本
[root@js-main ~]# docker version
Client: Docker Engine - Community
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:17:04 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.5
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Tue Mar 2 20:15:27 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-(uname -m)" -o /usr/local/bin/docker-compose
#改用下面的镜像,下载速度快。
curl -L https://get.daocloud.io/docker/compose/releases/download/1.28.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
#测试是否安装成功
docker-compose --version
二.搜索下载tomcat镜像
搜索镜像docker search tomcat
我就用第一个官方镜像好了。
下载镜像:docker pull tomcat
等待下载完成。。。下载会比较缓慢。。
查看本地镜像:docker images
三. 运行我们的tomcat镜像:
docker run -d -p 8888:8080 -v /root/tomcat/:/usr/local/tomcat/webapps/ tomcat
参数说明:
- -d 后台运行
- -p 指定访问主机的
8888
端口映射到8080
端口。 - -v 指定我们容器的
/usr/local/tomcat/webapps/
目录为/root/tomcat/
主机目录,后续我们要对tomcat进行操作直接在主机这个目录操作即可。
在/root/tomcat/
新建test
目录,并在test
目录下写入hello.html
文件
<html>
<head>Tomcat Run In Docker</head>
<body>
hello docker.
</body>
</html>
访问这个页面:
curl -i http://127.0.0.1:8888/test/hello.html
最后
我们不能直接在tomcat
目录下直接创建hello.html
文件
curl -i http://127.0.0.1:8888/hello.html
访问的结果404.