Combat: containerize springboot project with docker-compose

foreword

Previously, we learned and practiced using dockerfile to build mirrors, and through mirroring, we can containerize and deploy projects in the docker environment arbitrarily. But as developers in the WEB field, most of them are micro-service architectures. A project will have many sub-projects and middleware, and containerized deployment one by one will be very troublesome. Then, at this time, we can use docker-compose to build images and containers in batches to simplify our operation and maintenance.

Technology accumulation

docker-compose definition

docker-compose, as the name suggests, is a docker component that can build mirror images and manage containers. Developers can use it to build images and start containers in batches.

docker-compose file parameters

Commonly used parameters:
version specifies the file version of compose, must write
services defines the service, must write

The following is the services parameter:

parameter meaning
build When configuring the build, Compose will use it to automatically build the image. The value can be a path or an object
image Image name or image ID. If no image is specified locally, it will be pulled from the docker hub
restart: always Container always restarts
container_name container name
volumes Mount the shared directory
command Commands that need to be executed at build time
ports Externally exposed ports
environment Add environment variables
depends_on Rely on other services, other services are containerized before proceeding
networks Set the network, if not configured, a default network will be created, and all services will join this network

docker-compose command

parameter meaning
docker-compose -h view help
docker-compose build build image
docker-compose build -d Build a mirror in the background
docker-compose up Create and run all containers
docker-compose up -d Create and run all containers in the background
docker-compose -f docker-compose.yml up -d specify template
docker-compose down Stop and delete containers, networks, volumes, images
docker-compose pull pull dependency image
dokcer-compose config check configuration
dokcer-compose config -q Check the configuration, if there is a problem, there will be output
docker-compose restart restart service
docker-compose start start service
docker-compose stop Out of service
docker-compose ps List all containers in the project
docker-compose logs View the log information in the container
docker-compose up --force-recreate -d Force re-creation of the container after modifying compose

Combat demonstration

In actual production, containerized deployment is often containerized with multiple images, such as Kafka clusters, rabbitmq clusters, and ZK clusters. It is generally not recommended to put multiple irrelevant images in one docker-compose. Containerization is generally not recommended for databases, and containerization is recommended for microservice clusters.

Since the service image in the actual production environment is generally directly generated by the dockerfile, generally do not use docker-compose to build. If you need to use docker-compose to build an image, you can directly use the build parameter under services and provide the dockerfile path.

This article directly uses docker-compose to start and manage multiple containers.
Target images: demo_demo, demo_test-demo

1. Create a mount path

mkdir -p /home/test/demo/logs

2. Write docker-compose.yml

Write docker-compose, configure the containerization of the two images, and specify the log mount directory and time zone.

vim docker-compose.yml
version: '3.3'  #docker-compose版本
services: #服务列表
  demo: #服务名
    container_name: demo #容器名称
    image: demo_demo  #镜像
    ports: #端口映射
      - 8888:9999
    volumes: #目录挂载
      - /home/test/demo/logs:/opt/logs
      - /etc/localtime:/etc/localtime
    restart: always #自动重启
    environment: #环境变量
      TZ: Asia/Shanghai    #时区
  test-demo:
    container_name: test-demo
    image: demo_test-demo
    ports:
      - 7777:9999
    volumes:
      - /home/test/demo/logs:/opt/logs
      - /etc/localtime:/etc/localtime
    restart: always
    environment:
      TZ: Asia/Shanghai

3. Start and manage containers

》》》Start the container in the background

docker-compose up -d

》》》View container

docker ps | grep demo

insert image description here

》》》View port mapping

netstat -nplt | grep 8888

netstat -nplt | grep 7777

insert image description here

"""View container log

docker-compose logs  --tail 300 -f demo

insert image description here

》》》Modify docker-compose to force restart the container

docker-compose up --force-recreate -d

insert image description here

write at the end

It is relatively simple to manage containerized springboot projects with docker-compose. It can directly build images and containerized management in batches, which greatly simplifies our operation and maintenance. In an actual production environment, containerization is generally not recommended for databases, but containerization is recommended for microservice clusters.

Guess you like

Origin blog.csdn.net/weixin_39970883/article/details/131247228