1. Install docker-compose
- Download the docker-compose offline installation package through a networked machine (see the Downloads section) https://github.com/docker/compose/releases
- Upload the downloaded file to the server to be installed (recommended tool: FileZilla)
- 重命名 sudo mv <download_filename> docker-compose
- Change file directory sudo mv docker-compose /usr/local/bin/docker-compose
- Modify file username user group (or modify access permissions) cd /usr/local/bin/ chmod 755 docker-compose
- Verify that the installation was successful docker-compose --version
- Installed successfully docker-compose version 1.12.0, build b31ff33
For online installation of docker compose, see https://docs.docker.com/compose/install/
2. Uninstall docker-compose
If it is installed as a binary package, delete the binary file. rm /usr/local/bin/docker-compose
3. There is already an image, use docker-compose to start the container
Write the docker-compose.yml configuration file:
version: '3' services: athena-eureka: image: athena/athena-eureka restart: always ports: - 8761:8761 athena-gateway: image: athena/athena-gateway restart: always ports: - 8765:8765In the directory where docker-compose.yml is located, execute the command: docker-compose up -d to create and execute the service in the background.
In this way, the eureka service and the gateway service are started, the registry can be accessed on the page, and you can see that the gateway service has been registered.
4. No image, use docker-compose to create an image and start
(1) Add the packaging plugin to the pom (same as the previous article):
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- tag::plugin[] --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <configuration> <imageName>mambo/${project.artifactId}</imageName> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <!-- end::plugin[] --> </plugins> </build>
(3) First use the mvn clean package -DskipTests command to package the jar package (ignoring the test code), you can write a sh script to achieve one-click packaging
(4) Organize the packaged .jar file and the Dockerfile of each corresponding module. The organized directory structure is as follows: athena_package
├── docker-compose.yml ├── athena-eureka │ athena-eureka-1.0.jar | Dockerfile ├── athena-gateway │ athena-gateway-1.0.jar | Dockerfile ├── athena-keystone │ athena-keystone-1.0.jar | Dockerfile ├── athena-webapp │ athena-webapp-1.0.jar | Dockerfile |----- athena-mysql-server | | Dockerfile | |----mysql init_table.sql init_data.sql install_db.sh
(5) Write the docker-compose.yml configuration file for building the docker image:
version: '3' services: athena-eureka: build: athena-eureka ports: - 8761:8761 athena-gateway: build: athena-gateway ports: - 8765:8765 athena-mysql-server: build: athena-mysql-server ports: - 3308:3306 athena-keystone: build: athena-keystone ports: - 8881:8881 athena-webapp: build: athena-webapp ports: - 8883:8883
(6) Execute the command
docker-compose -f docker-compose.yml up
(7) The image will automatically create and start the container, and the page can access each application
5. Construction of the athena-mysql-server image:
(1) Download the mysql official database image.
We need to use the official image fusion database initialization script to make a new database image. This article uses the mysql:5.7 version of the image.
(2) Write Dockerfile
The official mysql image can support automatic traversal and execution of all files with .sh and .sql suffixes in the docker-entrypoint-initdb.d directory when the container starts . But the execution is out of order and doesn't quite meet our requirements. The solution is to write a shell script that defines the order in which the sql file is executed.
#base image FROM mysql:5.7 #define working directory ENV WORK_PATH /usr/local/work #define the directory that will be automatically executed by the container ENV AUTO_RUN_DIR /docker-entrypoint-initdb.d #define sql file name ENV FILE_1 init_table.sql ENV FILE_2 init_data.sql #define shell file name ENV INSTALL_DB_SHELL install_db.sh #create folder RUN mkdir -p $WORK_PATH #Copy the database initialization data file to the working directory COPY .mysql/$FILE_1 $WORK_PATH/ COPY .mysql/$FILE_2 $WORK_PATH/ # Put the shell file to be executed in the /docker-entrypoint-initdb.d/ directory, the container will automatically execute the shell COPY ./$INSTALL_DB_SHELL $AUTO_RUN_DIR/ #Add executable permission to the executable file RUN chmod a+x $AUTO_RUN_DIR/$INSTALL_DB_SHELL
The shell script is as follows:
mysql -uroot -p$MYSQL_ROOT_PASSWORD << EOF source $WORK_PATH/$FILE_1; source $WORK_PATH/$FILE_2;
(3) Then you can use the above docker-compose command to build the image and start the container, of course, you can also use the docker command:
Build the image:
docker build -t athena-mysql-server .
Start the container:
docker run --name mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD=1q2w3e -d athena-mysql-server