一. 首先在我們的服務器上安裝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
- 選版本的時候根據(jù)第二列
:
后面,-
前面,中間一段,表示版本。舉例:
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/
出現(xiàn)上面那個就表示安裝成功了。
通過 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
搜索tomcat鏡像
我就用第一個官方鏡像好了。
下載鏡像:docker pull tomcat
等待下載完成。。。下載會比較緩慢。。
查看本地鏡像:docker images
image.png
三. 運行我們的tomcat鏡像:
docker run -d -p 8888:8080 -v /root/tomcat/:/usr/local/tomcat/webapps/ tomcat
參數(shù)說明:
- -d 后臺運行
- -p 指定訪問主機的
8888
端口映射到8080
端口。 - -v 指定我們?nèi)萜鞯?code>/usr/local/tomcat/webapps/目錄為
/root/tomcat/
主機目錄,后續(xù)我們要對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
訪問docker中的頁面
最后
我們不能直接在tomcat
目錄下直接創(chuàng)建hello.html
文件
curl -i http://127.0.0.1:8888/hello.html
訪問的結果404.