Docker basic learning notes

Docker basic learning notes

Docker common basic commands

Mirror common commands

images command

Used to view the local docker image

# usage
docker images

search command

Used to search for images on Docker Hub

# usage
docker search + Image name:[tag]
# Example:
docker search centos:8

tag command

Mark the local image and group it into a warehouse

It can also be used to change the image name

# usage
docker tag + container ID + New container name:[tag]
# Example:
docker tag d23bdf5b1b1b java01:8

pull command

Pull or update the specified image from the image warehouse

# usage
docker pull + Image name:[tag]
# Example:
docker pull java:8

rmi command

Delete one or more local mirrors.

==docker rmi > docker image rm

# usage
docker rmi + Image name:[tag]/image id
# Common parameters
-f : Force delete
# Example:
docker rmi java:8  # docker image rm java:8

inspect command

Get metadata of container / image

# usage
docker inspect + image id

# Example: viewing debian images
docker inspect 

login command

Log in to the mirror warehouse

# usage
docker login

push command

To upload the local image to the image warehouse, log in to the image warehouse first

The image name here is the repository name / image name: tag

# usage
docker push + Repository name/Image name:[tag]

# Example: upload debian
docker push debian:latest

build command

Used to create an image using Dockerfile

# usage
docker build + parameter + Dockerfile address .

# Common parameters:
-f : Specify the to use Dockerfile route
-m : Set maximum memory
-q : --quiet Quiet mode. Only the image is output after success id
--rm : Delete the intermediate container after setting the image successfully
-t : --tag The name and label of the image, usually name:tag

# Example: dockerfile creates a how:0.1 image
docker build -f dockerfile -t how:0.1 .

history command

View the creation history of the specified mirror

usage:
docker history + Image name/image id

# Example: viewing debian image history
docker history debian

Container common commands

run command

Create a new container and run a command

# usage
docker run + parameter + Image name:[tag]/image id 
# Common parameters:
-d : Background run container
-t : Reassign a pseudo input terminal to the container, usually with -i Simultaneous use
-p : Randomly assigned port mapping
-P : Specify port mapping, format: Host port:Container port
-i : Run the container in interactive mode, usually with -t Simultaneous use
--name= : Specify a name for the container(Otherwise, name it randomly)
-e username= : Setting environment variables
--net= : Specify the network connection type of the container, support bridge/host/none/container: Four types(No default bridging mode is set)
--link= : Link to another container
--expose= : Open a port or group of ports
-v : amount to -volume Bind a volume/route
--rm : Automatically delete containers when they are stopped
# Example: start the Debian container in interactive mode, name it debian01, and execute the / bin/bash command
docker run -it --name debian01 debian /bin/bash

# Example: start the bwapp container in the background, name it bwapp01, and specify the mapping between port 80 of the container and port 8080 of the host
docker run -d -p 8080:80 --name bwapp01 35d0ed4da0ae
# Access host port: 127.0.0.1:8080

# Example: start the Debian container in interactive mode, name it debian02, link to the debian01 container, execute the bin/bash command, install iputils Ping and Ping debain01
docker run -it --name debian02 --link debian01 debian /bin/bash


create command

Create a new container without starting it

Similar to the run command

# usage
docker create + parameter + Image name:[tag]/id

# Example: create a container named debian
docker create --name debian debian

exit command

Exit container

The shortcut Ctrl + P + Q can also exit the container

# usage
exit

Example: exit from debian02 container

stop command

Stop a running container

# usage
docker stop + Container name/container id

# Example: stop debian02 container
docker stop debian02

start command

Start one or more containers that have been stopped

# usage
docker start + Container name/container id

# Example: start debian02 container
docker start debian02

restart command

Restart container

# usage
docker restart + Container name/container id

# Example: restart debian02 container
docker restart debian02

kill Command

Kill a running container

# usage
docker kill + Container name/container id
# Example: kill the debian02 container currently running
docker kill debian02

pause command

Pauses all processes in the container

# usage
docker pause + Container name/container id

# Example: pause all processes in Debian 02 container
docker pause debian02

unpause command

Recover all processes in the container

# usage
docker unpause + Container name/container id

# Example: restore all processes in Debian 02 container
docker unpause debian02

exec command

Enter to execute the command in the running container

# usage
docker exec + parameter + Container name/container id

# Common parameters:
-d : Run in the background
-i : Interaction mode(Often in -t Combined use)
-t : Assign a pseudo terminal(Often in -i Combined use)

# Example: enter debian02 in interactive mode and execute / bin/bash
docker exec -it debian02 /bin/bash

attach command

Connect to a running container

exit the container closes automatically after exiting

# usage
docker attach + Container name/container id

# Example: connect to debian02 container
docker attach debian02

ps command

List containers

# usage
docker ps + parameter

# Common parameters
-a : All containers(Running and stopped containers)
-q : Display container id
# Example: list running containers
docker ps

# Example: list all containers
docker ps -a

# Example: list the running container id
docker -q

# Example: display the IDs of all containers
docker ps -aq

rm command

Delete one or more containers

==docker rm > docker container rm

# usage
docker rm + parameter + Container name/container id

# Common parameters
-f : Force delete
-l : Remove container's network,Do not remove container
-v : Delete the volume associated with the container
# Example: forcibly delete debian container
docker rm -f debian

# Example: delete all containers
docker rm $(docker ps -a -q)

inspect command

Get metadata of container / image

# usage
docker inspect + container id

# Example: view debian02 container
docker inspect 4a58f3042379

cp command

Used for data copy between container and host

# usage
docker cp container id:Data path in container + Host path      # Copy container files to host
docker cp Host path data path + container id:In container path   # Copy host files to container

# Example: from host E:\Docker_Projects \ copy a copy of readme.md into / home of debian02
cd /d E:\Docker_Projects\              # cd to E:\Docker_Projects \ next
docker cp readme.md 4a58f3042379:/home # Copy the file to / home of the container
docker exec -it debian02 /bin/bash     # Enter container
cd /home/                              # cd to / home
ls                                     # View files under / home

# Example: copy / home/1.txt in the container to e: \ docker of the host_ Projects \ next
docker cp 4a58f3042379:/home/1.txt /Docker_Projects\ # Copy the container / home/1.txt to E:\Docker_Projects \ next
cd Docker_Projects\                                  # cd to E:\Docker_Projects \ next
ls                                                   # see file

commit command

Create a new image from the container

# usage
docker commit + parameter + container id + Image name:[tag]

# Common parameters:
-a : Submitted image author
-c : use Dockerfile Directive to create a mirror
-m : Description text at the time of submission
-p : stay commit Pause container when

# Example: create a Debian 02:0.1 image with the running Debian 02 container
docker commit -a "how" -m "just for fun" 4a58f3042379 debian02:0.1

Container data volume

Usage:

Method 1: use the - v command to mount

docker run -it -v Host Directory:In container directory

Anonymous mount: after the - v command, write only the path inside the container, not the path outside the container

Named mount: after the - v command, the host path and container path are available (easy to find)

Distinguish between specified path mounts , Named mount or anonymous mount
-v In container path                 #Anonymous mount
-v Volume name:In container path             #Named mount
-v Host path:In container path          #Specified path mount
# Change the read and write permissions through the - v path in the container: ro/rw
ro:readonly    #read-only
rw:readwrite   #Readable and writable

Once container permissions are set,It can only be operated through the host,Cannot operate inside container
# View synchronization directory
docker inspect + container id

Method 2: dockerfile

Data volume container

Multiple container data synchronization

--volumes-from + Parent container id

Dockerfile

  • Write a dockerfile
  • docker build builds an image file
  • docker run run image
  • docker push publishing image (dockerhub, Alibaba cloud)

Basics:

  1. Each reserved keyword (instruction) must be uppercase
  2. Execution order: from top to bottom
  3. #Indicates a comment
  4. Each assignment creates a new mirror layer and commits

Dockerfile common commands

FROM           # base image 
MAINTAINER     # Who wrote the image, name + email
RUN            # Commands to be executed during image construction
ADD            # Add content
WORKDIR        # Mirrored working directory
VOLUME         # Mounted directory
EXPOSE         # Exposed port
CMD            # Specify the command to run when the container starts (only the last one will take effect and can be replaced)
ENTRYPOINT     # Specify the commands to run when the container starts (you can append commands)
ONBULID        # When an inherited Dockerfile is built, the onbuilt instruction (trigger instruction) will be run
COPY           # Similar to the ADD command, the file is copied to the image
ENV            #Setting environment variables during construction

Create your own CentOS

1. Prepare dockerfile file
# Write dockerfile file
FROM centos
MAINTAINER how<penghao223366@gmail.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80

CMD echo SMYPATH
CMD echo "-----end-----"
CMD /bin/bash
2. Create centos image through dockerfile file
# Create centos image from dockerfile file
docker build -f + dockerfile File path -t + Image name:[tag] .
3. Test run

Use the history command to list the change history of the local image

docker history + image id

Difference between CMD and ENTRYPOINT

# CMD
FROM centos
CMD ["ls", "-a"]

# ENTRYPOINT
FROM centos
ENTRYPOINT ["ls", "-a"]

Actual combat: deploy Tomcat

1. Write Dockerfile

# Write Dockerfile
FROM centos
MAINTAINER how<penghao223366@gmail.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u301-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.52.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYTPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_301
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.52
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.52
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINER_HOME/lib:$CATALINER_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.52/bin/startup.sh && tail -F /url/local/apache-tomcat-9.0.52/bin/logs/catalina.out

2. Make image

# Make image
docker build -t diytomcat .

3. Start the container

# Start container
docker run -d -p 9090:8080 --name mytomcat -v /e/Docker_Projects/testfile/Tomcat/test:/usr/local/apache-tomcat-9.0.52/webapps/test -v /e/Docker_Projects/testfile/Tomcat/tomcatlogs:/usr/local/apache-tomcat-9.0.52/logs 21138e46bc7f

4. Start access test

# Start access test
127.0.0.1:9090

5. Publish project (write and publish locally)

# Publish project (write publish locally)
# (1)
touch web.xml

<?xml version="1.0" encoding="UTF-8" ?>   


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 id="WebApp_ID" version="2.5">

<display-name></display-name>

<!-- Welcome page -->
<welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>index1.jsp</welcome-file>
</welcome-file-list
<!-- Welcome page -->


<!-- url-pattern It means all.do The documents will pass through TestServlet handle. --> 
<servlet>
 <servlet-name>servlet1</servlet-name>
 <servlet-class>net.test.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>servlet1</servlet-name>
 <url-pattern>*.do</url-pattern>
</servlet-mapping>

# (2)
touch index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="beandemo.jsp" method="post">
     user name:<input type="text" name="username">
     password:<input type="password" name="password">
     <input type="submit" value="Submit">
 </form>
</body>
</html>

6. Successfully deployed and accessible

Publish image

DockerHub

  1. Address: https://hub.docker.com/ , register your account
  2. Confirm that the account can be logged in
  3. Submit your own image on the server
docker login # Sign in
	-u:username
	-p:password


docker push # Publish image
# [note] rename
docker tag + [image id] + [New name of image:Version number]

Alibaba cloud image warehouse

  1. Log in to alicloud
  2. Container mirroring service found
  3. Create namespace
  4. Create container image
# Log in to alicloud Docker Registry
docker login --username=[user name] registry.cn-shanghai.aliyuncs.com
# Pull the image from the Registry
docker pull registry.cn-shanghai.aliyuncs.com/howhacker/howhacker:[Mirror version number]
# Push image to Registry
docker login --username=howha**** registry.cn-shanghai.aliyuncs.com

docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/howhacker/howhacker:[Mirror version number]

docker push registry.cn-shanghai.aliyuncs.com/howhacker/howhacker:[Mirror version number]

Docker network

Understand Docker0

– link command

# Create tomcat01 container
docker run -d -P --name tomcat01 tomcat
# Create tomcat02 container connection tomcat01
docker run -d -P --link tomcat01 --name tomcat02 tomcat
# This is tomcat02. You can ping tomcat01
docker exec -it tomcat02 ping tomcat01  # Can ping through
# However, tomcat01 cannot ping tomcat02
docker exec -it tomcat01 ping tomcat02  # Can't ping

– link connect via host mapping

However, this is not recommended

Custom network! Not applicable docker0

Problem with docker0: it does not support container name connection access

Custom network

Network mode:

  • Bridge: Bridge (by default, you can create it yourself in bridge mode)
  • none: do not configure the network
  • Host: share network with host
  • Container: the container is connected to the network (less used and more limited)
# The command directly started before has a -- net bridge by default, which is docker0
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat

# docker0 features: by default, the domain name cannot be accessed, - link can get through the connection!

Create a custom network

docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet

# --driver bridge: Bridge
# --subnet 192.168.0.0/16: subnet address (- 192.168.255.255)
# --gateway 192.168.0.1: gateway address
[root@how ~]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "9eb5921910ce0d4fa4d1ee8d5f55b07203cc62a67e71154596e9d39f8d557323",
        "Created": "2021-09-04T16:31:49.676301454+08:00",
        "Scope": "local",
       "Driver": "bridge",
        "EnableIPv6": false,
       "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
           ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
# Start both containers
[root@how ~]# docker run -d -P --net mynet --name tomcat01 tomcat
276c60ea13a5c3bb9ca7a2d4762576e62ffc7d4083449c70595f0433e0c2c26d
[root@how ~]# docker run -d -P --net mynet --name tomcat02 tomcat
0a0e314d0197992ae4e21cc539e87ffb6ade91cbf2ab0229422759b9f1b60c01
#see
[root@how ~]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "9eb5921910ce0d4fa4d1ee8d5f55b07203cc62a67e71154596e9d39f8d557323",
        "Created": "2021-09-04T16:31:49.676301454+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
           "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0a0e314d0197992ae4e21cc539e87ffb6ade91cbf2ab0229422759b9f1b60c01": {
                "Name": "tomcat02",
                "EndpointID": "7ba95ab2b03a6bf2719c78bb46092e1184bef280e632978312d379400d022e60",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "276c60ea13a5c3bb9ca7a2d4762576e62ffc7d4083449c70595f0433e0c2c26d": {
                "Name": "tomcat01",
                "EndpointID": "b20754389dcdd0498171401452d86df6b9cf6e8435076793121dd24c6058de69",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
# Test (you can ping the container name or ping the container ip)
root@276c60ea13a5:/usr/local/tomcat# ping tomcat02
PING tomcat02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.088 ms
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.071 ms
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.064 ms
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.070 ms
^C
--- tomcat02 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3109ms
rtt min/avg/max/mdev = 0.064/0.073/0.088/0.008 ms

root@276c60ea13a5:/usr/local/tomcat# ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.046 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.062 ms
64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.059 ms
^C
--- 192.168.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3081ms
rtt min/avg/max/mdev = 0.046/0.057/0.064/0.007 ms

Different clusters use different networks to ensure cluster health

Network connectivity

#Create a new network mynet2
docker network create --driver bridge --subnet 192.167.0.0/16 --gateway 192.167.0.1 mynet2

# Create two containers tomcat03 and tomcat04 under the mynet2 network
docker run -d -P --net mynet2 --name tomcat03 tomcat
docker run -d -P --net mynet2 --name tomcat04 tomcat
# Connect tomcat03 to the mynet network
docker network connect mynet tomcat03

# View mynet network
[root@how ~]# docker network inspect mynet
[
 {
     "Name": "mynet",
        "Id": "9eb5921910ce0d4fa4d1ee8d5f55b07203cc62a67e71154596e9d39f8d557323",
        "Created": "2021-09-04T16:31:49.676301454+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0a0e314d0197992ae4e21cc539e87ffb6ade91cbf2ab0229422759b9f1b60c01": {
                "Name": "tomcat02",
                "EndpointID": "7ba95ab2b03a6bf2719c78bb46092e1184bef280e632978312d379400d022e60",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "276c60ea13a5c3bb9ca7a2d4762576e62ffc7d4083449c70595f0433e0c2c26d": {
                "Name": "tomcat01",
                "EndpointID": "b20754389dcdd0498171401452d86df6b9cf6e8435076793121dd24c6058de69",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "318bd30ff5030f6e4f824f682d37368258f140b52c685c04a1b6507049ad318b": {
                "Name": "tomcat03",
                "EndpointID": "164ad155cd13719571c940f0360eb9d33cbdbe5f5837e180258d312ae1b0fcb6",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
   ]
   
# After Unicom, tomcat03 is directly added to mynet
# One container 2 ip
# test
PING tomcat02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.061 ms
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.059 ms
^C
--- tomcat02 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2026ms
rtt min/avg/max/mdev = 0.059/0.068/0.085/0.011 ms
root@318bd30ff503:/usr/local/tomcat# ping tomcat01
PING tomcat01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.087 ms
64 bytes from tomcat01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from tomcat01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from tomcat01.mynet (192.168.0.2): icmp_seq=4 ttl=64 time=0.059 ms
64 bytes from tomcat01.mynet (192.168.0.2): icmp_seq=5 ttl=64 time=0.057 ms
^C
--- tomcat01 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4094ms
rtt min/avg/max/mdev = 0.056/0.063/0.087/0.011 ms
# tomcat03 can ping tomcat02 and tomcat01
# At this time, tomcat04 still cannot ping tomcat02 and tomcat01

Actual deployment of Redis cluster:

Sharding + high availability + load balancing

Six redis

Three masters and three slaves

1. Create Redis network

# Create Redis network
docker network create --driver bridge --subnet 192.168.0.1/16 --gateway 192.168.0.1 Redis

2. Start 6 Redis containers

(1) . create script startup
# Create script startup
for port in $(seq 1 6); \
do \
mkdir -p /home/admin/Docker-file/mydata/redis/node-${port}/conf
touch /home/admin/Docker-file/mydata/redis/node-${port}/conf/redis.conf
cat <<EOF>/home/admin/Docker-file/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
docker run -p 637${port}:6379 -p1637${port}:16379 --name redis-${port} -v /home/admin/Docker-file/mydata/redis/node-${port}/data:/data -v /home/admin/Docker-file/mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
done
(2) . command start
# Start redis-1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v /home/admin/Docker-file/mydata/redis/node-1/data:/data -v /home/admin/Docker-file/mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# Start redis-2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 -v /home/admin/Docker-file/mydata/redis/node-2/data:/data -v /home/admin/Docker-file/mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# Start redis-3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 -v /home/admin/Docker-file/mydata/redis/node-3/data:/data -v /home/admin/Docker-file/mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# Start redis-4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 -v /home/admin/Docker-file/mydata/redis/node-4/data:/data -v /home/admin/Docker-file/mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# Start redis-5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 -v /home/admin/Docker-file/mydata/redis/node-5/data:/data -v /home/admin/Docker-file/mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# Start redis-6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 -v /home/admin/Docker-file/mydata/redis/node-6/data:/data -v /home/admin/Docker-file/mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf -d --net Redis --ip 192.168.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

3. Create a cluster

# Enter container redis-1
docker exec -it redis-1 /bin/sh
# Create cluster
redis-cli --cluster create 192.168.0.11:6379 192.168.0.12:6379 192.168.0.13:6379 192.168.0.14:6379 192.168.0.15:6379 192.168.0.16:6379 --cluster-replicas 1
# return
/data # redis-cli --cluster create 192.168.0.11:6379 192.168.0.12:6379 192.168.0.13:6379 192.168.0.14:6379 192.168.0.15:6379 192.168.0.16:6379 --cluster-re
plicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.15:6379 to 192.168.0.11:6379
Adding replica 192.168.0.16:6379 to 192.168.0.12:6379
Adding replica 192.168.0.14:6379 to 192.168.0.13:6379
M: 2fc2a259cc36419457e1576aa8e5f92b8c966b30 192.168.0.11:6379
   slots:[0-5460] (5461 slots) master
M: cca30ab3faf91376ba7a7e6dffefd325dd0e3887 192.168.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: acc7304f9f218970a9fef330accba86486329837 192.168.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: 44d1fc8286dc8d0f95514c9e9f1dec715c25fab7 192.168.0.14:6379
   replicates acc7304f9f218970a9fef330accba86486329837
S: 315f0d008576280a3f37c1e22552ad017bff46aa 192.168.0.15:6379
   replicates 2fc2a259cc36419457e1576aa8e5f92b8c966b30
S: d838c16b2cb75415e466c0e236a3eaf15c2bddfb 192.168.0.16:6379
   replicates cca30ab3faf91376ba7a7e6dffefd325dd0e3887
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.0.11:6379)
M: 2fc2a259cc36419457e1576aa8e5f92b8c966b30 192.168.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: acc7304f9f218970a9fef330accba86486329837 192.168.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 44d1fc8286dc8d0f95514c9e9f1dec715c25fab7 192.168.0.14:6379
   slots: (0 slots) slave
   replicates acc7304f9f218970a9fef330accba86486329837
S: 315f0d008576280a3f37c1e22552ad017bff46aa 192.168.0.15:6379
   slots: (0 slots) slave
   replicates 2fc2a259cc36419457e1576aa8e5f92b8c966b30
S: d838c16b2cb75415e466c0e236a3eaf15c2bddfb 192.168.0.16:6379
   slots: (0 slots) slave
   replicates cca30ab3faf91376ba7a7e6dffefd325dd0e3887
M: cca30ab3faf91376ba7a7e6dffefd325dd0e3887 192.168.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

4. Test

# Enter cluster
redis-cli -c  # Redis cli: single machine redis cli -c: Cluster

# see information
cluster info

# return
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:426
cluster_stats_messages_pong_sent:427
cluster_stats_messages_sent:853
cluster_stats_messages_ping_received:422
cluster_stats_messages_pong_received:426
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:853

# View nodes
cluster nodes

# return
127.0.0.1:6379> cluster nodes
acc7304f9f218970a9fef330accba86486329837 192.168.0.13:6379@16379 master - 0 1630764542020 3 connected 10923-16383
44d1fc8286dc8d0f95514c9e9f1dec715c25fab7 192.168.0.14:6379@16379 slave acc7304f9f218970a9fef330accba86486329837 0 1630764541519 4 connected
2fc2a259cc36419457e1576aa8e5f92b8c966b30 192.168.0.11:6379@16379 myself,master - 0 1630764540000 1 connected 0-5460
315f0d008576280a3f37c1e22552ad017bff46aa 192.168.0.15:6379@16379 slave 2fc2a259cc36419457e1576aa8e5f92b8c966b30 0 1630764541018 5 connected
d838c16b2cb75415e466c0e236a3eaf15c2bddfb 192.168.0.16:6379@16379 slave cca30ab3faf91376ba7a7e6dffefd325dd0e3887 0 1630764541018 6 connected
cca30ab3faf91376ba7a7e6dffefd325dd0e3887 192.168.0.12:6379@16379 master - 0 1630764543021 2 connected 5461-10922

# Stored value
set a b

# return
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 192.168.0.13:6379
OK
# Stored in 192.168.0.13

# Stop the container 192.168.0.13
docker stop redis-3
redis-3

# Value of get a
get a

# return
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 192.168.0.14:6379
"b"

# View nodes
cluster nodes

# return
192.168.0.14:6379> cluster nodes
315f0d008576280a3f37c1e22552ad017bff46aa 192.168.0.15:6379@16379 slave 2fc2a259cc36419457e1576aa8e5f92b8c966b30 0 1630764997099 5 connected
d838c16b2cb75415e466c0e236a3eaf15c2bddfb 192.168.0.16:6379@16379 slave cca30ab3faf91376ba7a7e6dffefd325dd0e3887 0 1630764996097 6 connected
acc7304f9f218970a9fef330accba86486329837 192.168.0.13:6379@16379 master,fail - 1630764788489 1630764787687 3 connected
cca30ab3faf91376ba7a7e6dffefd325dd0e3887 192.168.0.12:6379@16379 master - 0 1630764995096 2 connected 5461-10922
44d1fc8286dc8d0f95514c9e9f1dec715c25fab7 192.168.0.14:6379@16379 myself,master - 0 1630764996000 7 connected 10923-16383
2fc2a259cc36419457e1576aa8e5f92b8c966b30 192.168.0.11:6379@16379 master - 0 1630764996000 1 connected 0-5460

5.docker Redis setup is complete!

SpringBoot microservice packaging Docker image

  1. Building a spring boot project
  2. Package project
  3. Write dockerfile
  4. Build mirror
  5. Publish run

1. Create a Spring Boot project

# Create a Spring Boot project
# Create a new package controller and create a HelloController class
package com.example.hello.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,howhacker";
    }
}

Local self test: 127.0.0.1:8080

Then pack

2. Create a Dockerfile

# Create a Dockerfile file
# Edit Dockerfile
FROM java:8

COPY hello-0.0.1-SNAPSHOT.jar /app.jar

CMD ["---server.port=8080---"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

3. Create a mirror container

docker build -t how .
docker -d -P --name how how

cca30ab3faf91376ba7a7e6dffefd325dd0e3887 192.168.0.12:6379@16379 master - 0 1630764995096 2 connected 5461-10922
44d1fc8286dc8d0f95514c9e9f1dec715c25fab7 192.168.0.14:6379@16379 myself,master - 0 1630764996000 7 connected 10923-16383
2fc2a259cc36419457e1576aa8e5f92b8c966b30 192.168.0.11:6379@16379 master - 0 1630764996000 1 connected 0-5460

5.docker Redis setup is complete!

SpringBoot microservice packaging Docker image

  1. Building a spring boot project
  2. Package project
  3. Write dockerfile
  4. Build mirror
  5. Publish run

1. Create a Spring Boot project

# Create a Spring Boot project
# Create a new package controller and create a HelloController class
package com.example.hello.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @RequestMapping("/hello")
    public String hello(){
        return "hello,howhacker";
    }
}

Local self test: 127.0.0.1:8080

Then pack

2. Create a Dockerfile

# Create a Dockerfile file
# Edit Dockerfile
FROM java:8

COPY hello-0.0.1-SNAPSHOT.jar /app.jar

CMD ["---server.port=8080---"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

3. Create a mirror container

docker build -t how .
docker -d -P --name how how

Tags: Operation & Maintenance Docker Redis Spring Boot Container

Posted on Sat, 04 Sep 2021 16:54:28 -0400 by billcoker