Docker container orchestration implementation process analysis

Docker container orchestration implementation process analysis

In actual development or production environments, containers are often not run independently, and multiple containers are often required to run together. At this time, if you continue to use the run command to start the container, it will be very inconvenient. In this case, docker-compose is a good choice. Docker compose can be used to achieve container orchestration. This article will take a look at the use of docker-compose. This article takes the deployment of an open source website like jpress as an example to introduce the use of docker-compose to readers. jpress is the Java version of WordPress, but we don’t need to pay attention to the implementation of jpress. Here we just need to treat it as a normal application to complete the deployment of the project.

Preparation

Here we need two containers in total:

  • Tomcat
  • MySQL

Then you need the war package of jpress, war package address: jpress

Of course, jpress is not required here. Readers can also choose other Java projects or write a simple Java project deployment according to their own situation.

Writing a Dockerfile

In the Tomcat container, you need to download related war files, so I write a Dockerfile to do this. Create a Dockerfile in an empty folder with the following content:

FROM tomcat
ADD https://github.com/JpressProjects/jpress/raw/alpha/wars/jpress-web-newest.war
/usr/local/tomcat/webapps/
RUN cd /usr/local/tomcat/webapps/ \
&& mv jpress-web-newest.war jpress.war

explain:

  • The container is created based on Tomcat.
  • Download the war package of the jpress project to the webapps directory of tomcat.
  • Rename the jpress project.

Write docker-compose.yml

Write docker-compose.yml in the same directory with the following content (the basic knowledge of yml is not introduced here, and readers can find out by themselves):

version: "3.1"
services:
web:
build: .
container_name: jpress
ports:
- "8080:8080"
volumes:
- /usr/local/tomcat/
depends_on:
-db
db:
image: mysql
container_name: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DATABASE: jpress

explain:

  • First, the web container is declared, and then the db container.
  • build . means the build context of the web container project is ., that is, the Dockerfile will be searched in the current directory to build the web container.
  • container_name indicates the name of the container.
  • Ports refers to the port mapping of the container.
  • Volumes represent the data volumes that configure the container.
  • Depends_on means that the container depends on the db container. When starting, the db container will start first, and the web container will start later. This is just the timing of starting.
  • The order of the two containers does not mean that the web container will start only after the db container is fully started.
  • For the db container, image is used to build it instead of Dockerfile.
  • restart describes the restart strategy for the container.
  • environment is the environment variable when starting the container. Here, the password of the database root user is configured and a file named
  • In the jpress library, environment configuration can be in the form of dictionary and array.

OK, after the above steps, docker-compose.yml is configured successfully

run

There are several ways to run it, but the ultimate command is up. The up command is very powerful. It will try to automatically complete a series of operations including building images, (re)creating services, starting services, and associating service-related containers. Most applications can be started directly through this command. By default, all containers started by docker-compose up are in the foreground, and the console will print the output information of all containers at the same time, which is very convenient for debugging. When you stop the command through Ctrl-C, all containers will stop. If you use the docker-compose up -d command, all containers will be started and run in the background. This option is generally recommended for production environments. Therefore, enter the directory where docker-compose.yml is located and execute the following command:

docker-compose up -d

The execution results are as follows:


After execution, you can see that the container has started through the docker-compose ps command.

Initial configuration

Next, enter http://localhost:8080/jpress in the browser, and you can see the jpress configuration page as follows:

Configure the database connection information and basic website information according to the guide page:



Note: Since both MySQL and web are running in containers, the loopback address cannot be written when configuring the database address, otherwise the database will be found in the container where web is located.

After the configuration is complete, run the following command to restart the web container:

docker restart jpress

test

View the blog homepage and backend management page in the browser, as shown below:


other

If you want to stop the container, you can execute the following command:

docker-compose down

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem of docker container exiting immediately after starting
  • Summary of data interaction between Docker container and host
  • Detailed explanation of Docker container network port configuration process
  • Docker data volume container creation and usage analysis
  • Detailed explanation of Docker container data volumes
  • How to communicate between WIN10 system and Docker internal container IP

<<:  JavaScript to achieve a simple magnifying glass effect

>>:  What to do if the auto-increment primary key in MySQL is used up

Recommend

Implementation of CSS linear gradient concave rectangle transition effect

This article discusses the difficulties and ideas...

Detailed explanation of mysql integrity constraints example

This article describes the MySQL integrity constr...

Solution to the routing highlighting problem of Vue components

Preface Before, I used cache to highlight the rou...

How to implement Echats chart large screen adaptation

Table of contents describe accomplish The project...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...

Detailed examples of the difference between methods watch and computed in Vue.js

Table of contents Preface introduce 1. Mechanism ...

Solution to Docker image downloading too slowly

Docker image download is stuck or too slow I sear...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

Several ways to implement "text overflow truncation and omission" with pure CSS

In our daily development work, text overflow, tru...

IE9beta version browser supports HTML5/CSS3

Some people say that IE9 is Microsoft's secon...

Vue uses the Element el-upload component to step on the pit

Table of contents 1. Basic Use 2. Image quantity ...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

CentOS8 network card configuration file

1. Introduction CentOS8 system update, the new ve...

Analysis of the process of implementing Nginx+Tomcat cluster under Windwos

Introduction: Nginx (pronounced the same as engin...