In order to facilitate the local test project, in order to facilitate the opening of the new environment, in order to facilitate the deployment, the plan is to install Mysql and Redis locally using Docker.
Build Springboot project, write Dockerfile, package build image.
Simply start the service with Docker-compose.
Briefly describe Docker-compose and K8S.
The environment
System: the MAC
Docker Engine: 19.03.8
Mysql: 5.7, disk mounted directory: / Users/yclxiao/Program/volume/Mysql
Redis: 5.0.8, disk mounted directory: / Users/yclxiao/Program/volume/Redis
Install Mysql 5.7
-
Pull the official image
Docker pull mysql: 5.7Copy the code
-
View the Mirror Library
docker images Copy the code
-
Create a mount directory for mysql
mkdir -p /Users/yclxiao/Program/volume/mysql/data /Users/yclxiao/Program/volume/mysql/conf /Users/yclxiao/Program/volume/mysql/logs Copy the code
-
Create the CNF file
cd /Users/yclxiao/Program/volume/mysql/conf touch my.cnf Copy the code
-
Create containers to map data, logs, and configuration files to the native machine
docker run -p 3306:3306 --name mysql -v /Users/yclxiao/Program/volume/mysql/conf:/etc/mysql/conf.d -v /Users/yclxiao/Program/volume/mysql/logs:/logs -v /Users/yclxiao/Program/volume/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD = rootpwd - d mysql: 5.7Copy the code
-d: indicates the background running container
-p: maps the container port to the local port
-v: mounts the host directory to the container directory
-e: Sets parameters
-
Start the container
docker start mysql Copy the code
Docker run XXX docker start XXX
-
Docker run:
Only for the first run, put the image into the container, and then start the container again using docker start.
Docker run executes two commands:
- Docker Create: Puts the image into the container
- Docker start: Starts the container and becomes a running container
-
Docker start:
Start an existing container
-
-
Use Navicat and other tools to test whether it can be connected
User name: root
Password: rootpwd
Install Redis – 5.0.8
-
Create a redis-related directory under the host mount directory
/Users/yclxiao/Program/volume/redis/conf
/Users/yclxiao/Program/volume/redis/data
-
In the/Users/yclxiao/Program/volume/redis/conf
touch redis.conf Copy the code
-
Pull the official image and view the image
Docker pull Redis :5.0.8 Docker imagesCopy the code
-
Start the container, redis:5.0.8 Image name
docker run -d --name redis -p 6379:6379 -v /Users/yclxiao/Program/volume/redis/conf/redis.conf:/redis.conf -v / Users/yclxiao/Program/volume/redis/data: / data redis: 5.0.8 redis - server -- appendonly yesCopy the code
-d: Makes the container run in the background
-p: Indicates the exposed port in front and the service port inside the container in the back
-v: to mount the host directory to the /data directory of the container, run the following command:
–name: Alias for this container
–appendonly: Enable persistent data saving
Redis-server –appendonly YES: Run the redis-server startup command in the container and start the redis persistent configuration
-
View the running container
docker ps Copy the code
-
RDM tool connection test OK
Common Docker commands
Docker Search # searches images from the Registry repository
Docker pull # Download the image locally from the repository
Docker images # lists all images
Docker tag # alias the image
Docker rmI image name # delete an image
Docker images -q docker images -q
Docker push # Pushes an image into the Registry repository
Docker build -t. Docker build -t. Cannot little
Docker create # Creates a container without starting it
Docker run # creates and starts a container
Docker start # Starts a stopped container
Docker ps -aq # list all container ids
Docker ps # lists running containers
Docker ps -a #
Docker stop container id # Stop a container
Docker rm container id # delete a container
Docker stop $(docker ps -aq) #
Docker rm $(docker ps -aq) #
Docker kill # sends a signal to the container, SIGKILL by default
Docker exec # enters the container and executes the command
Docker inspect # inspect for all information about a container
Write Dockerfile
Dockerfile is the basis for docker to build images, and is also an important feature of Docker that distinguishes it from other containers. It is with Dockerfile that the automation and portability of Docker becomes possible.
-
FROM builds a new image FROM an underlying image
FROM openjdk:8-jdk-alpine Copy the code
-
MAINTAINER: Provides MAINTAINER information
MAINTAINER yclxiao <[email protected]> Copy the code
-
ENV, sets the environment variable
ENV TESTVAR 123 Copy the code
-
RUN, RUN the shell command
RUN xxxxxx Copy the code
-
ADD, copy the external file to the image, SRC can be the URL
ADD xxxxx /data/xxxxxx Copy the code
-
WORKDIR /path/to/ WORKDIR: sets the working directory
WORKDIR /var/www Copy the code
-
USER: Sets the USER ID
USER nginx Copy the code
-
VULUME<#dir>, set volume, set mount volume
VOLUME ['/data'] Copy the code
-
EXPOSE which ports
EXPOSE 8080 8081 Copy the code
-
ENTRYPOINT [“executable”,”param1″,”param2″] executes the command
ENTRYPOINT ["/usr/sbin/nginx"] Copy the code
-
CMD [“param1″,”param2”]
CMD ["start"] Copy the code
Command used when docker creates and starts containers. If ENTRYPOINT is set, CMD is used as the argument
Example: Build an image of a Java application using Dockerfile
FROM openjdk:8-jdk-alpine
ENV TZ=Asia/Shanghai
This variable is required if the docker build command packages images directly (in the form of non-Maven plug-ins)
# ENV JAR_FILE = - dbpool - the main target/blog - 0.0.1 - the SNAPSHOT. The jar
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
VOLUME /tmp
Parameter defined by the dockerFile plug-in in #pom.xml
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
Copy the code
Building a Java project
Build Springboot project with Dockerfile file, POM configuration docker plug-in, build image push image, start container.
-
Preparation project:
Address of the completed project
-
The dockerfile-maven-plugin plugin is recommended. The dockerfile-maven-plugin plugin is not recommended.
See this article: Spotify officially recommends the Maven plugin to build Docker images
The maven setting is in the.m2 folder
servers
Otherwise, the configuration does not take effect<servers> <server> <id>docker.io</id> <username>yclxiao</username> <password>xxxxxx</password> </server> </servers> Copy the code
-
Build mirror, push warehouse:
-
Run the docker command directly from the dockerfile directory:
Docker build -t yclxiao/blog-dbpool-main:0.0.1-SNAPSHOT. Docker push yclxiao/blog-dbpool-main:0.0.1-SNAPSHOTCopy the code
-
Using the Maven plugin:
mvn clean package dockerfile:build mvn clean package dockerfile:push Copy the code
-
-
By executing Docker Images, you can see the image in the local repository.
Check out the dockerHub repository https://hub.docker.com/ to see this image
-
Create a startup container:
Docker run -p 8080:8080 yclxiao/blog-dbpool-main:0.0.1 -snapshot [-d] docker run -p 8080:8080 yclxiao/blog-dbpool-main:0.0.1 -snapshot [-d]
-
After local docker start mysql, visit OK: http://localhost:8080/swagger-ui.html
Deploying with Docker-compose
Docker-compose is a tool used to define and run multiple Docker containers on the machine, which is responsible for realizing the rapid arrangement of docker container clusters. You can use yML files to configure all the services your application needs. Then use the command to create and start all the services from the YML file configuration.
Two important concepts:
Service: A container for an application that can actually contain several instances of the container running the same image.
Project: A complete business unit consisting of a set of associated application containers, defined in the docker-comemess.yml file.
- Docker-comemage. yml: docker-comemage. yml: docker-comemage. yml: docker-comemage. yml
version: '3.7'
# Define service
services:
# specify a service name, such as the spring-boot service
app-server:
build:
context: . # configure the path required to build Dockerfile relative to docker-comemage.yml
dockerfile: Dockerfile
# specify the port on which the service runs
ports:
- "8080:8080" # map native port 8080 to container port 8080
restart: always
The dependent services are built first
depends_on:
- db
- redis
#environment: # Set environment variables
#SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/airTicket? useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
#SPRING_DATASOURCE_USERNAME: root
#SPRING_DATASOURCE_PASSWORD: 123456
#SPRING_REDIS.HOST: redis
db:
# specify the mirror used by the service
image: Mysql: 5.7
# specify the container name
container_name: mysql5.7
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpwd
volumes:
- /Users/yclxiao/Program/volume/mysql/conf:/etc/mysql/conf.d
- /Users/yclxiao/Program/volume/mysql/logs:/logs
- /Users/yclxiao/Program/volume/mysql/data:/var/lib/mysql
redis:
image: Redis: 5.0.8
# specify the container name
container_name: redis5.0.8
volumes:
- /Users/yclxiao/Program/volume/redis/conf/redis.conf:/redis.conf
- /Users/yclxiao/Program/volume/redis/data:/data
command: ["redis-server", "--protected-mode". "no". "--appendonly". "yes"]
hostname:
redis
ports:
- "6379:6379"
Copy the code
- Then run the following command to create a container that has been downloaded locally:
Docker-compose up: docker-compose up: docker-compose up: docker-compose up Docker-compose ps # list all docker-compose informationCopy the code
- Docker – compose builds, docker – compose up after the output log normal, visit OK: http://localhost:8080/swagger-ui.html
Docker-compose is different from K8S
Docker Compose is the core and foundation of container technology. Docker Compose is a docker-based single-host container orchestration tool (container management tool), which is not as rich as Kubernetes, which is a dCOker-based cross-host container management platform.
The original link