docker image correlation

Dockerfile and image making practice

Dockerfile is a text file used to build an image. The text content contains instructions and instructions required to build an image. We usually create a docker image based on this file,
For example:

preparation

  1. centos:7 image (all image files need an empty centos image when they are created, which is similar to creating a system boot disk through an empty CD or u disk)
  2. JDK compressed package jdk-8u51-linux-x64.tar.gz (you can download it from the official website: oracle.org). Based on this compressed package, make a JDK image.

Dockerfile file

When creating a new image, you need to have a Dockerfile (the file name must pay attention to case). This file should be placed with your resources (such as the JDK you downloaded). We can create this file (touch Dockerfile) through the touch instruction in the linux system

Now, build the image of oracle jdk8 based on centos7. First, open the Dockerfile through vim. If the file does not exist, it will be created automatically. Then enter the editing mode (press the i letter). In the editing mode, you can copy the following statements (you can write them manually, but ensure that the words written are correct, case and space).
Step 1: write a FROM statement (the keyword must be uppercase, for example, FROM cannot be lowercase)

FROM centos:7

Step 2: transfer the compressed package from the host to the specified directory in the image container through the ADD command, and decompress it at the same time

ADD jdk-8u51-linux-x64.tar.gz /usr/local/docker

Step 3: set the environment variable (realized by ENV keyword, and start the directory in the container)

ENV JAVA_HOME=/usr/local/docker/jdk1.8.0_51 \
    PATH=/usr/local/docker/jdk1.8.0_51/bin:$PATH

Step 4: specify the command line operation (there should be spaces between all instructions and the following contents)

CMD ['bash']

The complete example is as follows: (pay attention to the space between the keyword and the following content. You can copy the following example to the Dockerfile file, but be sure to pay attention to your jdk name.)

FROM centos:7
ADD jdk-8u51-linux-x64.tar.gz /usr/local/docker
ENV JAVA_HOME=/usr/local/docker/jdk1.8.0_51 \
    PATH=/usr/local/docker/jdk1.8.0_51/bin:$PATH
CMD ['bash']

Use Dockerfile to build an image (execute the docker instruction in the directory where Dockerfile is located)

docker build -t jdk:8 .  #Don't lose the dot here, - t stands for the image ID (image name), which is the abbreviation of tag


Note the point at the end, which indicates that the file is found from the current directory during the construction process. jdk:8 is the image name created for us.

Run mirror file

docker run -it jdk:8 bash

How to run a web service through this image, sentinel

Step 1: copy sentinel to the specified directory of the host, such as / root/servers directory (the servers directory does not exist and can be created by yourself).

Step 2: start the image container and run the web service through java
Start and run sentinel service based on jdk:8 image (the service can be accessed on the host through localhost:8180 after startup)

docker run -d  -p 8180:8080 --name sentinel  \
-v /root/servers:/usr/sca  \ 
jdk:8 java -jar /usr/sca/sentinel-dashboard-1.8.0.jar

Step 3: open the browser and access sentinel service

Make sentinel image

Step 1: define the Dockerfile file, and put the JDK and sentinel in the same directory as this file

FROM centos:7
ADD jdk-8u51-linux-x64.tar.gz  /usr/local/
ADD sentinel-dashboard-1.8.0.jar  /usr/local/
ENV JAVA_HOME=/usr/local/jdk1.8.0_51 \
    PATH=/usr/local/jdk1.8.0_51/bin:$PATH
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/sentinel-dashboard-1.8.0.jar"]

Step 2: use Dockerfile to build an image (execute the docker instruction in the directory where Dockerfile is located)

docker build -t  sentinel:8 .  #Don't lose the point here

Step 3: run sentinel container

docker run -d -p 8180:8080 sentinel:8

If we want to mount the directory and save the log in the container, we can refer to the following methods:

docker run -d -p 8180:8080 --name sentinel  \
-v /usr/local/docker/sentinel/logs:/root/logs/csp sentinel:8

Step 4: View sentinel container

docker ps

Step 5: access sentinel service
When accessing, your ip address should be the ip address of the host, and the port number should be the port number of the host

Installation practice of Docker image

Install mysql image

Online installation

  1. Pull the specified version of mysql. You can also specify the pull version, for example:
 docker pull mysql:8.0.23

  1. View image information
 docker images

  1. Start and run mysql image (docker run is used to start a container)
sudo docker run -p 3306:3306 --name mysql \
-v /usr/local/docker/mysql/mysql-files:/var/lib/mysql-files \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:8.0.23

If the installation fails, you can view the previous container through docker ps -a. if it already exists, you can delete it through docker rm image id and reinstall it.
The following is the mysql login operation:

Install Redis database

Step 1: download the image file

docker pull redis

Step 2: prepare the configuration file

Create redis profile directory

mkdir -p /usr/local/docker/redis01/conf

Create a redis.conf configuration file under the configuration file record (this file must be created. No, a directory is generated by default when we mount the directory)

touch /usr/local/docker/redis01/conf/redis.conf

Step 3: create a redis instance and start it

sudo docker run -p 6379:6379 --name redis01 \
-v /usr/local/docker/redis01/data:/data \
-v /usr/local/docker/redis01/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf 

Step 4: view the running process

docker ps


Installing nginx


Access nginx

Install nacos

Step 1: pull nacos (hub.docker.com)

docker pull nacos/nacos-server:1.4.1

Step 2: execute the sql script file of nacos in mysql

1) Copy the file nacos-mysql.sql (which can be downloaded from the code server) to the mount directory corresponding to the host of the MySQL container (you can view your MySQL mount directory through docker inspect mysql)

2) Start and log in to mysql in linux environment

Enter mysql container (if mysql has been started)

docker exec -it mysql bash

Step 3: create and start the nacos container

docker run  \
-e TZ="Asia/Shanghai" \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_DATABASE_NUM=1 \
-e MYSQL_SERVICE_HOST=192.168.227.131 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=jason \
-e MYSQL_SERVICE_PASSWORD=jason \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-p 8848:8848 \
--name nacos \
--restart=always \
-d nacos/nacos-server:1.4.1

Parameter description

Single node mode
MODE=standalone
Database address
MYSQL_SERVICE_HOST
Database user name
MYSQL_SERVICE_USER
Database password
MYSQL_SERVICE_PASSWORD
Name of database to be connected
MYSQL_SERVICE_DB_NAME
Port mapping
-p 8848:8848
Restart the container at any time, and the container can be started automatically (docker needs to be set to start automatically)

--restart=always

Analysis of communication between Nacos and Mysql

Tags: Linux CentOS Docker

Posted on Sun, 05 Sep 2021 23:15:28 -0400 by smilesmita