Docker notes-Docker basic concepts and deployment of Tomcat

Docker architecture

Docker Engine consists of 3 parts

Insert picture description here

  1. The core is Docker Server, which is Docker Daemon
  2. On the outside, there is a communication layer based on REST API
  3. The outermost layer is Docker Client. It can accept commands and send requests and instructions to the Docker Server through the REST API. After the Docker Server finishes processing, it returns the results to the Docker Client through the REST API. Communication is based on REST API, so the Http protocol is used

The REST API can be seen as a small Tomcat, and the internal Docker Server can be seen as a service.

The advantages of such a CS-based architecture are:

  1. Docker server and client do not need to be installed on the same computer. Operation and maintenance can manage multiple servers with Docker Server at the same time through a computer with Docker Client installed

Images and containers

  1. Mirror

    A mirror is a file, read-only, static, it provides the complete software and hardware resources needed to run the program, and is the container of the application

  2. container

    A container is an instance of a mirror, which is dynamic, and is created by Docker, and the containers are isolated from each other

  3. example

    For example, the installation CD of the windows operating system is a mirror image (it is some static files), and the computer with the windows system installed, it can be called an instance of the windows system, that is, the container of the windows image

Docker execution process

Insert picture description here

Inside a docker container is a mini linux system

Docker deploy Tomcat

Docker commonly used commands

# tags可以指定镜像的特定版本
docker pull 镜像名<:tags>
# 查看本地已经下载好的镜像
docker images
# 以某个镜像文件,创建容器,并启动
# 若docker run的镜像还没被pull到本地,则会自动先执行docker pull
docker run 镜像名<:tags>

# 以交互的方式进入到容器内部
docker exec -it 容器id /bin/bash
# 查看正在运行中的容器
docker ps
# 查看所有的容器
docker ps -a
# 删除容器
docker rm <-f> 容器id
# 删除镜像
docker rmi <-f> 镜像名<:tags>
# 拉取latest的tomcat镜像
docker pull tomcat

If you want to specify the tags of the mirror, you can go to hub.docker.com and search for the mirror name, such as tomcat

Insert picture description here

docker run tomcat

After the startup is complete, the tomcat service cannot be accessed through port 8080. This is because the port mapping between the host and the Docker container has not been configured

Insert picture description here

Insert picture description here

Docker provides port mapping to bind the port of the host and the port of the container.

The external caller only pays attention to the port exposed by the host, and it has no way of knowing which container to forward to. In this way, as long as the mapping port of the host is kept unchanged, I can run tomcat or jetty in the container, which is very convenient for migration.

So how to do port mapping

# 先Ctrl + C 停掉 tomcat这个容器
# 重新运行
docker run -p 8000:8080 tomcat
# 这样就把宿主机的8000端口,绑定到了容器中的8080端口

# 结果发现访问报404,说明tomcat起来了,也对外暴露了,但是找不到资源
docker exec -it tomcat容器id /bin/bash
# 进入容器后,发现webapps下没有任何文件,但是有一个webapps.dist
# webapps.dist下有ROOT文件夹等资源
# 于是,先删除webapps文件夹,再把webapps.dist重命名为webapps
rm webapps
mv webapps.dist webapps
# 再次访问,成功

Insert picture description here

If the following prompt appears, after tomcat is started, it can't be accessed. You need to modify the ipv4 network configuration

Insert picture description here

# 需要修改一下配置
vi /etc/sysctl.conf
# 进入该文件,添加这段配置
net.ipv4.ip_forward=1
# 重启network服务和docker服务
systemctl restart network
systemctl restart docker
# 再次启动docker容器即可
# 若启动成功但是访问不到,可能是centos 端口没有放行
# 用如下方式进行端口放行
firewall-cmd --zone=public --add-port=8000/tcp --permanent

Check it with the netstat command, you can see that port 8000 of the host is in LISTENING state

Insert picture description here

As you can see, port mapping is done through docker proxy

Through -dparameters, the container can be run as a background process

# 让docker在后台运行
# 注意把镜像名称放在最后
docker run -p 8000:8080 -d tomcat

# 如何把docker容器停止,并移除容器
docker stop tomcat
docker rm tomcat容器的id

# 删除某个镜像
docker rmi tomcat

Guess you like

Origin blog.csdn.net/vcj1009784814/article/details/106270150