Docker Installation and Mirror Configuration (Introduction to Common Commands)

1. Install Docker

Official Web https://docs.docker.com/engine/install/centos/

# 1. Uninstall the old version
yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine
    
# 2. Installation packages required
yum install -y yum-utils

# 3. Set up a mirror warehouse
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo #defaults to foreign mirror address

yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #Ali Cloud Mirror    

# (Optional) Update package index
yum makecache fast

# 4. Install docker ce-community ee-enterprise
yum install docker-ce docker-ce-cli containerd.io

# 5. Start docker
systemctl start docker

# 6. See if docker was installed successfully
docker version

# 7.hello-world
docker run hello-world

# 8. View that the hello-world mirror does not exist
docker images





# Uninstall docker
# 1. Uninstall docker dependency, corresponding to installation step 4
yum remove docker-ce docker-ce-cli containerd.io
# 2. Delete resources (docker's default working path/var/lib/docker)
rm -rf /var/lib/docker

2. Ali Cloud Mirror Acceleration

1. Log on to Ali Cloud Console

2. Search Container Mirror Service

3. Configure according to the accelerator address provided by the Mirror Accelerator

Common commands for Docker

Help Command

# docker version information
docker version 
# docker system information
docker info 
# Help Command
docker command --help 

Official Web Help Documentation: https://docs.docker.com/reference/

Mirror Command

docker images

# View mirrors on all local hosts
docker images
-a Show all
-q Show only mirrors id



docker search

# Search Mirror
docker search image

docker pull

# Download the mirror, using the latest version by default
# docker pull mirror [: tag]
[root@porty ~]# docker pull mysql
Using default tag: latest # No tag defaults to latest
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete # Hierarchical download, docker image core
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
ffe10de703ea: Pull complete
657af6d90c83: Pull complete
98bfb480322c: Pull complete
9f2c4202ac29: Pull complete
a369b92bfc99: Pull complete
Digest: sha256:365e891b22abd3336d65baefc475b4a9a1e29a01a7b6b5be04367fcc9f373bb7 # autograph
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # Real Address
# Specified version download
[root@porty ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Already exists
fedd960d3481: Already exists
7ab947313861: Already exists
64f92f19e638: Already exists
3e80b17bff96: Already exists
014e976799f9: Already exists
59ae84fee1b3: Already exists # Layered download, existing layers can be shared
7d1da2a18e2e: Pull complete
301a28b700b9: Pull complete
979b389fc71f: Pull complete
403f729b1bad: Pull complete
Digest: sha256:d4ca82cee68dce98aa72a1c48b5ef5ce9f1538265831132187871b78e768aed1
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi

# Delete the specified mirror Id
# Docker rmi-f mirror ID (name can also be specified if multiple spaces are available)
docker rmi -f image id
# Delete Multiple Mirrors
docker rmi -f image id image id image id...
# Delete all, find all mirror IDs to delete recursively
docker rmi -f $(docker images -aq)

Container commands

Description: To create a container with a mirror, download a centos as a test

docker pull centos

New Container and Start

docker run [Optional parameters] image

# parameter
--name="Name" # Container name
-d # Background running
-it # Enter the container interactively
-p # Specify the port of the container  
	-p ip:Host Port:Container Port
	-p Host Port:Container Port(Common)
	-p Container Port
	Container Port
-P # Randomly specified port

# Test, start and enter container
[root@porty ~]# docker run -it centos /bin/bash
[root@0bb81f60254c /]#
# Exit Container
docker exit

View running containers

# View running containers
docker ps
-a # Include historically run containers
-n666 # Show 666 recently created containers
-q # Display container number only

Exit Container

# Container stops and exits
exit
# Container does not stop exiting
Ctrl + P + Q

Delete Container

# Delete specified container, cannot delete running container, force deletion requires option-f
docker rm container id
# Delete all containers
docker rm -f $(docker ps -aq)
# Delete all containers
docker ps -aq | xargs docker rm -f

Start and Stop Containers

# Start Container
docker start container id
# Restart Container
docker restart container id
# Stop Container
docker stop container id
# Force Kill Container
docker kill container id

Common Other Commands

Background startup container

# Create a container in the background because there is no foreground process and the container will stop automatically when it is started
docker run -d image id
# Create containers in the background and keep them running
docker run -itd image id 

view log

docker logs container id
-t # Time stamp showing log
-f # Uninterrupted
-n10 # Show the latest 10 records
docker logs -tfn10 container id

View process information in containers

docker top container id
# Example
[root@porty ~]# docker top b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                25250               25230               0                   16:22               ?                   00:00:00            /bin/bash

View mirrored metadata

docker inspect container id

Enter the currently running container

# Interactively enter a running container, open a new terminal after entering the container
docker exec -it container id /bin/bash
# Interactively enter a running container, enter a terminal that the container is executing, and do not start a new terminal
docker attach container id 

Copy files from container to host

docker cp container id:Source File Location Host Target Location
# Example
docker cp b:/porty.java /root/

Docker Install Nginx

# 1. Search for nginx mirrors
docker search nginx
# 2. Pull nginx mirror
docker pull nginx
# 3. Create nginx containers and match ports
[root@porty var]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    ae2feff98a0c   2 days ago    133MB
centos       latest    300e315adb2f   10 days ago   209MB
[root@porty var]# docker run -d --name nginx01 -p 3344:80 nginx
13389d744c10d169c67bbb11b28f1ea2caebe2d9d1b18d8066010fb1159b4522
[root@porty var]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                  NAMES
13389d744c10   nginx     "/docker-entrypoint...."   5 seconds ago   Up 3 seconds   0.0.0.0:3344->80/tcp   nginx01
# 4. Testing
[root@porty var]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@porty var]#

Modify configuration files inside containers outside containers

-v 

Docker Install Tomcat

# Official use, used for testing, cleared when used--rm
docker run -it --rm tomcat:9.0
docker run -it --rm -p 8888:8080 tomcat:9.0

# Pull tomcat image
docker pull tomcat:9.0
# Background Start Container
docker run -d --name=tomcat01 -p 8888:8080 image id
# test
[root@porty ~]# curl localhost:8888
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.41</h3></body></html>

Docker Installation ElasticSearch, Kibana and Configuration

# elasticsearch is memory intensive
# --net somenetwork network configuration 
# Start elasticsearch
docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

# linux will special card docker stats to check cpu status after boot
# Small memory is best set a memory limit for elasticsearch to restart
# Normally modify the configuration file, with -e environment configuration modifications
docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2


# Install kibana version for elasticsearch version
#  --net somenetwork network configuration
docker run -d --name kibana -p 5601:5601 kibana:7.6.2
# Because docker's container environment is isolated from each other, all kibana s cannot connect directly to elasticsearch and require intranet forwarding




# Here, for elasticsearch to connect to kibana, both need to be configured on the same network
# Create a network
docker network create somenetwork
# Limit memory startup elasticsearch container to use network blocks just created
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
# Start the kibana container to use the network block you just created
docker run -d --name kibana --net somenetwork -p 5601:5601 kibana:7.6.2

visualization

  • portainer
# Start the portainer visualization panel container
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
# Visit the visualization interface localhost to replace the server address
localhost:8088
  • Rancher(CI/CD)

Docker Mirror

What is Mirror

Mirroring is a lightweight, executable, stand-alone package that packages software running environments and software developed based on them. It contains all the internal clients required to run a software, including code, runtime, libraries, environment variables, and configuration files.

Docker mirror loading principle

Doker mirror loading is hierarchical and involves UnionFS (Federated File System).

bootfs(boot File System): boot bootstrapper + kernel, boot completion will give the system usage rights to the kernel management, uninstall the bootstrapper. Each system is undifferentiated and shared

rootfs(root File System):Root is Ubuntu, centos, etc., and has formed various file systems, which may be different and not shared

If two mirrors are loaded, his bootfs do not need to be unmanaged, because the docker relies on the host's kernel. So docker loads the image only to rootfs, and again, if the second image is loaded with some mirroring layers loaded on the first image. Then he won't need to load again.

Hierarchical Understanding

Commit mirror

command

# Submit Container to a New Copy
docker commit
# option
docker commit -m="Descriptive information submitted" -a="author" container id Target Mirror Name:[Edition]

test

# Start a tomcat with nothing in the default webapps folder
# Will webapps. All files in the dist folder are copied to the webapps folder
# Package this container into a mirror
docker commit -a="porty" -m="add some default pages." 00ac20eb7e81 porty_tomcat:1.0
# After submitting, a new image is generated in the local mirror library, which docker images can find
docker images 
# 

Container data volume

What is a container data volume

docker concept

Package applications and environments into a single image!

Container data volume

Mounting directories

Using data volumes

Mode 1: Use commands directly to hang on -v

docker run -it -v Host Directory Address:Catalogs inside containers

# test
docker run -it -v /root/porty:/home centos /bin/bash
# The host or what the container creates in the directory is synchronized to the container or host.
# The container stops, the host is hanging in the directory to operate, and the container synchronizes
# Even if the container is deleted, files hanging under the directory will not be lost

Actual Mysql

# mysql's data is important, so you need to hang it in the directory
# When the mysql container is created, the password-e mysql_needs to be set ROOT_ PASSWORD=my-secret-pw
# The -e option represents the environment variable configuration
# Official: docker run--name some-mysql-e MYSQL_ ROOT_ PASSWORD=my-secret-pw-d mysql:tag
docker run -d -p 3310:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 image id

# Connect to the server using remote tools after successful startup, port 3310, if successful proof mysql container creation and successful operation
# Check the hanging directory for data files

Anonymous and Anonymous Mounts

# Anonymous mount
-v Path inside container
docker run -d -P --name nginx01 -v /etc/nginx nginx
# View all volume information
docker volume ls
DRIVER    VOLUME NAME
local     8efbbdb6edc2e189a8412ad9673a9ccf044f2d9f16cd65b2729078d87d5ee0dd


# Hang Named
-v Host Path:Path inside container
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
docker volume ls
DRIVER    VOLUME NAME
local     juming-nginx

# View Volume Details
docker volume inspect Volume Name
docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2020-12-22T10:27:17+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]
# All container volumes, without specifying a directory, are in / var/lib/docker/volumes/volume name (juming-nginx)/_data

# Expand
# Read-only, read-only in container, can only be modified by host
docker run -d -v juming-nginx:/etc/nginx:ro nginx
# Read-write
docker run -d -v juming-nginx:/etc/nginx:rw nginx

First Identity Dockerfile

Dockerfile is the build file, command script, used to build the docker image

# Using Dockerfile to build a mirror

# Create dockerfile1 file for writing
FROM centos

VOLUME ["volume1","volume2"]

CMD echo "------end------"
CMD /bin/bash

# Build the mirror last. Symbol cannot be missing
[root@porty docker-test-volume]# docker build -f /root/docker-test-volume/dockerfile1 -t porty-centos:1.0 .

Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 300e315adb2f
Step 2/4 : VOLUME ["volume1","volume2"]
 ---> Running in 6b7f951b1727
Removing intermediate container 6b7f951b1727
 ---> 27c4df35d87c
Step 3/4 : CMD echo "------end------"
 ---> Running in 224c593c89f8
Removing intermediate container 224c593c89f8
 ---> 5757bc3bc547
Step 4/4 : CMD /bin/bash
 ---> Running in 51414801f7a0
Removing intermediate container 51414801f7a0
 ---> abab4447f260
Successfully built abab4447f260
Successfully tagged porty-centos:1.0
[root@porty docker-test-volume]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED              SIZE
porty-centos   1.0       abab4447f260   About a minute ago   209MB
nginx          latest    ae2feff98a0c   6 days ago           133MB
mysql          5.7       697daaecf703   10 days ago          448MB
centos         latest    300e315adb2f   2 weeks ago          209MB

# Create a container using the created image and go into the container to see if two volume mount directories exist under the root directory
# The host uses the docker inspect container id to view the container's data and see the corresponding information for volume mounting

Data Volume Container

# Test multiple containers for shared data
# Create docker01 container (data volume container)
docker run -it --name docker01 abab4447f260
# Create docker02 container and inherit docker01
docker run -it --name docker02 --volumes-from docker01 abab4447f260
# Create docker03 container and inherit docker01
docker run -it --name docker03 --volumes-from docker01 abab4447f260
# Their data volume directory files are now shared
# Even if the parent container docker01 is deleted, the other two containers will have access to the files in the shared directory and will continue to synchronize

Synchronizing data with multiple mysql s

# mysql01
docker run -d -p 3310:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 image id

# mysql02
docker run -d -p 3311:3306 -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 image id

DockerFile

Introduction to DockerFile

DockerFile is the file used to build a docker image! Command parameter script!

Construction steps:

1. Write a dockfile

2. Command docker build to build into a mirror

3.docker run uses mirroring to create containers

4.docker push release mirror (DockerHub, Ali cloud mirror warehouse)

DockerFile build process

Be careful:

1. Each keyword (instruction) must be a capital letter

2. Execute from top to bottom

3. Indicate a comment

4. Each instruction creates and submits a new mirror layer!

DockerFile directive

FROM 			# Basic Mirror, everything built from here
MAINTAINER 		# Who wrote the mirror, name + mailbox
RUN 			# Commands to run when building a mirror
ADD				# Step, tomcat mirror, tomcat compressed package! Add Content
WORKDIR			# Mirrored Working Directory
VOLUME			# Catalog Location Hang
EXPOSE			# Expose Port
CMD				# Specifies 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
ONBUILD			# When an inherited DockerFile is constructed, the ONBUILD directive is run, triggering the directive
COPY			# Similar to ADD command, copy file to mirror
ENV				# Set environment variables when building

Field Test

Create a centos

# Write dockerfile file
cat mydockerfile-centos
FROM centos
MAINTAINER porty<1527957705@qq.com>

ENV MYPATH /use/local
WORKDIR $MYPATH

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

EXPOSE 80

CMD echo $MYPATH
CMD echo "------end------"
CMD /bin/bash

# Build Mirror
docker build -f mydockerfile-centos -t mycentos:0.1 .
# end of execution
Successfully built 282d2b0414a8
Successfully tagged mycentos:0.1

Differences between CMD and ENTRYPOINT

CMD				# Specifies 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

# CMD Test
# Write dockerfile dockerfile-test-cmd
FROM centos
CMD ["ls","-a"]
# Build Mirror
docker build -f dockerfile-test-cmd -t cmdtest .
# Running the mirror outputs everything in the directory according to the ls-a command
docker run cmdtest
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# If the command-l is appended while the container is running, an error will occur
docker run cmdtest -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
# In the case of cmd, -l replaces the ['ls', -a'] command, -l is not a command and therefore errors!

# ENTRYPOINT Test
# Write dockerfile dockerfile-test-entrypoint
FROM centos
ENTRYPOINT ["ls","-a"]
# Build Mirror
docker build -f dockerfile-test-entrypoint -t entrypointtest .
# Running the mirror outputs everything in the directory according to the ls-a command
docker run entrypointtest
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# If you append command-l while running a container, you can append it directly
docker run entrypointtest -l
total 56
drwxr-xr-x   1 root root 4096 Dec 22 08:08 .
drwxr-xr-x   1 root root 4096 Dec 22 08:08 ..
-rwxr-xr-x   1 root root    0 Dec 22 08:08 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x   5 root root  340 Dec 22 08:08 dev
drwxr-xr-x   1 root root 4096 Dec 22 08:08 etc
drwxr-xr-x   2 root root 4096 Nov  3 15:22 home
lrwxrwxrwx   1 root root    7 Nov  3 15:22 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3 15:22 lib64 -> usr/lib64
drwx------   2 root root 4096 Dec  4 17:37 lost+found
drwxr-xr-x   2 root root 4096 Nov  3 15:22 media
drwxr-xr-x   2 root root 4096 Nov  3 15:22 mnt
drwxr-xr-x   2 root root 4096 Nov  3 15:22 opt
dr-xr-xr-x 100 root root    0 Dec 22 08:08 proc
dr-xr-x---   2 root root 4096 Dec  4 17:37 root
drwxr-xr-x  11 root root 4096 Dec  4 17:37 run
lrwxrwxrwx   1 root root    8 Nov  3 15:22 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3 15:22 srv
dr-xr-xr-x  13 root root    0 Oct 21 07:30 sys
drwxrwxrwt   7 root root 4096 Dec  4 17:37 tmp
drwxr-xr-x  12 root root 4096 Dec  4 17:37 usr
drwxr-xr-x  20 root root 4096 Dec  4 17:37 var

Actual Warfare: Tomcat Mirror

# 1. Prepare tomcat and jdk
ll
total 150964
-rw-r--r-- 1 root root  11442169 Dec  3 20:42 apache-tomcat-9.0.41.tar.gz
-rw-r--r-- 1 root root 143142634 Dec 15 07:32 jdk-8u271-linux-x64.tar.gz
# 2. Write a dockerfile, if the file name is Dockerfile, the build project does not need to specify the dockerfile file -f
FROM centos
MAINTAINER porty<1527957705@qq.com>

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

ADD apache-tomcat-9.0.41.tar.gz /usr/local/
ADD jdk-8u271-linux-x64.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

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

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.41/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.41/logs/catalina.out
# 3. Build the mirror, because the dockerfile name here is Dockerfile, you do not need to specify the dockerfile file with -f
docker build -t porty_tomcat .
# 4. View the mirror
docker images
REPOSITORY       TAG       IMAGE ID       CREATED              SIZE
porty_tomcat     latest    69e66cedbeb9   About a minute ago   639MB
# 5. Start the mirror and mount the directory
docker run -d -p 8080:8080 --name porty_tomcat01 -v /root/dockerfiles/tomcat/test:/usr/local/apache-tomcat-9.0.41/webapps/test -v /root/dockerfiles/tomcat/logs:/usr/local/apache-tomcat-9.0.41/logs porty_tomcat
# 6. Visit the test and display the tomcat page
curl localhost:8080

# Test Publishing Project
# index.jsp file
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>porty</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("I'm porty!");
%>
</body>
</html>
# WEB-INF/web.xml file
<?xml version="1.0" encoding="UTF-8"?>

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

</web-app>
# Perform access test ip:8080/test

Publish your own image

DockerHub

1.https://hub.docker.com Register Account

2. Make sure the account is correct

3. Submit a mirror to DockerHub on the server

# Logon Command
[root@porty ~]# docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username
  
# Sign in
[root@porty ~]# docker login -u porty1997 -p Porty1997
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

# Submit a mirror with a version number whenever possible
# 1. A warehouse such as porty1997/porty_needs to be built in dockerhub Tomcat
# 2. The docker image on the server needs to be copied to a mirror named warehouse name
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag porty_tomcat porty1997/porty_tomcat
# 3. Use the docker push command to upload to the corresponding repository of dockerhub, and the following tests are being submitted
[root@porty ~]# docker push porty1997/porty_tomcat
Using default tag: latest
The push refers to repository [docker.io/porty1997/porty_tomcat]
c23b49b4a91b: Pushing [================>                                  ]  18.96MB/58.02MB
a1fce1a8dd54: Pushing [==>                                                ]  19.68MB/355.3MB
35dddb931ac2: Pushing [======================>                            ]  7.154MB/15.83MB
f084de1cda6b: Pushed
2653d992f4ef: Pushing [=>                                                 ]  5.471MB/209.3MB

# Submitted at the same level as the mirror

Publish to Ali Cloud Mirror Service

1. Log on to Aliyun

2. Find Container Mirror Service

3. Create a namespace

4. Create a mirror warehouse

5. Enter the mirror warehouse to browse for details

# If you have logged in to dockerhub before, exit dockerhub first
docker logout
# Log in to Ali Cloud Mirror Service, the password is the container service password, if you forget you can reset the access credentials page of the container service
[root@porty ~]# docker login --username=18923512289 registry.cn-beijing.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
# Modify the image name based on the submission information of the mirror repository
# docker tag [ImageId] registry.cn-beijing.aliyuncs.com/porty/porty_tomcat:[Mirror Version Number]
docker tag ae2feff98a0c registry.cn-beijing.aliyuncs.com/porty/porty_tomcat:1.0
# Mirror submission based on submission information from a mirror warehouse
# docker push registry.cn-beijing.aliyuncs.com/porty/porty_tomcat:[Mirror Version Number]
[root@porty ~]# docker push registry.cn-beijing.aliyuncs.com/porty/porty_tomcat:1.0
The push refers to repository [registry.cn-beijing.aliyuncs.com/porty/porty_tomcat]
4eaf0ea085df: Pushed
2c7498eef94a: Pushed
7d2b207c2679: Pushed
5c4e5adc71a8: Pushing [======>                                            ]  8.157MB/63.7MB
87c8a1d8f54f: Pushing [=====>                                           ]  7.617MB/69.23MB

Docker Network

Docker Network-Docker0

# View the network
[root@porty ~]# ip addr
# Local loopback address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
# Ali Cloud Intranet Address
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:2c:dc:eb brd ff:ff:ff:ff:ff:ff
    inet 172.24.129.136/20 brd 172.24.143.255 scope global dynamic eth0
       valid_lft 309914003sec preferred_lft 309914003sec
# Address of docker0
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:8b:db:ca:f1 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

# Three networks represent three environments





# Question: The server deploys two containers, tomcat and mysql. What do they depend on for the address on which they communicate?
# test
# Create a tomcat container in the background
docker run -d -P --name tomcat01 tomcat
# Execute the container to get the network information inside the tomcat container. eth0@if186 Assign for docker
[root@porty ~]# docker exec -it tomcat01 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
185: eth0@if186: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
# Test if the host can ping through the docker to the address assigned to the container --> is pingable
[root@porty ~]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.094 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.061 ms
# Each time we start a docker container, the docker assigns an ip to the container
# As long as we have docker installed, there will be a network card docker0, using veth-pair Technology
# Test ip addr again on the host
[root@porty ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:2c:dc:eb brd ff:ff:ff:ff:ff:ff
    inet 172.24.129.136/20 brd 172.24.143.255 scope global dynamic eth0
       valid_lft 309912919sec preferred_lft 309912919sec
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:8b:db:ca:f1 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
# An additional network card was found, which happens to be the one assigned by docker to the tomcat container after executing the tomcat container
186: vethae8cfd2@if185: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 6a:c1:4b:58:63:ce brd ff:ff:ff:ff:ff:ff link-netnsid 0

       
       
# Start another tomcat container test
docker run -d -P --name tomcat02 tomcat
# View tomcat2 container network card
[root@porty ~]# docker exec -it tomcat02 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
189: eth0@if190: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
# Returning to the host to test ip addr again, we found that there is a network card with similar network card information as docker assigned to tomcat2 container in addition to the original network card information.
190: vethb92e282@if189: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether f6:19:b4:4b:ef:73 brd ff:ff:ff:ff:ff:ff link-netnsid 2

# The bridge disappears when the container is deleted

# These containers have network cards that appear one-to-one
# evth-pair technology is a virtual device interface, a link protocol, a link to each other
# Use the veth-pair feature as a bridge to connect various virtual network devices


# The test finds that the tomcat02 container can be ping ed from the tomcat01 container
# Containers and containers can be ping ed to each other, but instead of being directly connected, they are bridged by the host docker0


# Doker can specify router via --net during installation
# If docker 0 is not specified as the default router, docker assigns each container a default available IP

- link (not recommended)

Consider a scenario where we wrote a microservice, database url=ip:, project does not restart, database IP is replaced, we want to be able to handle this problem, name can be used to access the container?

# If there is no connection between the container and the container, the container name cannot be used to ping through, that is, the tomcat01 and tomcat02 containers
# If there is a connection, for example, when the container tomcat03 is created with the--link command to connect tomcat02, tomcat03 can ping through the tomcat02 container, but it is important to note that the connection is one-way, that is, tomcat02 still cannot Ping through the tomcat03 container name
[root@porty ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        5.7       f07dfa83b528   37 hours ago   448MB
tomcat       latest    feba8d001e3f   5 days ago     649MB
[root@porty ~]# docker run -d -P --name tomcat01 tomcat
98b8061f69765d839b32e86f8198b922dc84e3bae5cee2451d46f34761054449
[root@porty ~]# docker run -d -P --name tomcat02 tomcat
188ec57af07fe81f80f49b46a85225ae588a9acbc24c4b916d876b9ab1ff3aaf
[root@porty ~]# docker exec -it tomcat01 ping tomcat02
ping: tomcat02: Name or service not known
[root@porty ~]# docker run -d -P --name tomcat03 --link tomcat02 tomcat
50514c5a4785584ad060033876a24ad5a83f253d6cba910c87b32d6f8c4fb27f
[root@porty ~]# docker exec -it tomcat03 ping tomcat02
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.068 ms

# --link actually writes the ip information, container name, container id of tomcat02 to the / etc/hosts file of tomcat03, which can be explained later to
[root@porty ~]# docker exec -it tomcat03 cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3      tomcat02 188ec57af07f
172.17.0.4      50514c5a4785


Custom network

Docker network lsView all docker networks

Network mode

bridge: bridging docker

none: do not configure network

Host: Share network with host

Container: container network connectivity (rarely used)

test

# Previous startup commands that did not configure the network defaulted to bridge mode--net bridge
docker run -d -P --name tomcat01 --net bridge tomcat

# docker0 feature, by default, the domain name is not accessible--link can make a connection, but it's more cumbersome

# Custom network
# Docker network create--driver network mode (default is bridge) --subnet 192.168.0.0/16 --gateway 192.168.0.1 network name
[root@porty ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
b9d0ebd8a422e6f78c0738ccf10f639c2d9b35cece9a81dcce37ff6ec6932695
[root@porty ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
2cdff7f9bea0   bridge    bridge    local
c9a6c17a57bf   host      host      local
b9d0ebd8a422   mynet     bridge    local
c9450aafacd8   none      null      local
# View network information
[root@porty ~]# docker network inspect b9d
[
    {
        "Name": "mynet",
        "Id": "b9d0ebd8a422e6f78c0738ccf10f639c2d9b35cece9a81dcce37ff6ec6932695",
        "Created": "2020-12-23T19:40:20.161584781+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": {}
    }
]



# Test runs two tomcat containers and uses the same custom network
[root@porty ~]# docker run -d -P --name tomcat01 --net mynet tomcat
a117be620c920e31da07ea6cefa9bbf030d1eed269c2585fb734cda25c035ddd
[root@porty ~]# docker run -d -P --name tomcat02 --net mynet tomcat
487367e7bfdb1eea74c0532bf3b0b6d4b82f5c8cc9c08d7a854734bb5481908b
[root@porty ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                     NAMES
487367e7bfdb   tomcat    "catalina.sh run"   2 seconds ago   Up 2 seconds   0.0.0.0:49164->8080/tcp   tomcat02
a117be620c92   tomcat    "catalina.sh run"   7 seconds ago   Up 6 seconds   0.0.0.0:49163->8080/tcp   tomcat01
[root@porty ~]# docker exec -it tomcat01 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.057 ms
64 bytes from tomcat02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.060 ms
^C
--- tomcat02 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1ms
rtt min/avg/max/mdev = 0.057/0.058/0.060/0.007 ms
[root@porty ~]# docker exec -it tomcat02 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.039 ms
64 bytes from tomcat01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.057 ms
^C
--- tomcat01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.039/0.048/0.057/0.009 ms
# Discovering that a container name can be used to communicate between two containers

# Looking at the custom network you just used again, you can see that there are already two containers for the container label, and the relationship is maintained by the custom network
[root@porty ~]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "b9d0ebd8a422e6f78c0738ccf10f639c2d9b35cece9a81dcce37ff6ec6932695",
        "Created": "2020-12-23T19:40:20.161584781+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": {
            "487367e7bfdb1eea74c0532bf3b0b6d4b82f5c8cc9c08d7a854734bb5481908b": {
                "Name": "tomcat02",
                "EndpointID": "ec48cc82e6cc909b567d7c3d9364e230ee506f15d27102dab24c15786450fe19",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "a117be620c920e31da07ea6cefa9bbf030d1eed269c2585fb734cda25c035ddd": {
                "Name": "tomcat01",
                "EndpointID": "64a98dcd903c9a87b220489c0ff601a3c005cbd6d4a082d9d94c9cabbfc4f9f6",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

# Benefits:
# Clusters of various services use different custom networks to ensure that the cluster is safe and healthy

Network Connectivity

# Run two tomcat containers again, using the default network
docker run -d -P --name tomcat03 tomcat
docker run -d -P --name tomcat04 tomcat

# Network Connectivity
[root@porty ~]# docker network connect --help

Usage:  docker network connect [OPTIONS] NETWORK CONTAINER

Connect a container to a network

Options:
      --alias strings           Add network-scoped alias for the container
      --driver-opt strings      driver options for the network
      --ip string               IPv4 address (e.g., 172.30.100.104)
      --ip6 string              IPv6 address (e.g., 2001:db8::33)
      --link list               Add link to another container
      --link-local-ip strings   Add a link-local address for the container
      
# Testing connects the tomcat03 container to a custom network mynet, then uses tomcat03 to connect to other IPS on the mynet network
docker network connect mynet tomcat03

# Looking at the mynet network information again, we found that the container of tomcat03 has been added to the maintenance of mynet network
[root@porty ~]# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "b9d0ebd8a422e6f78c0738ccf10f639c2d9b35cece9a81dcce37ff6ec6932695",
        "Created": "2020-12-23T19:40:20.161584781+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": {
            "487367e7bfdb1eea74c0532bf3b0b6d4b82f5c8cc9c08d7a854734bb5481908b": {
                "Name": "tomcat02",
                "EndpointID": "ec48cc82e6cc909b567d7c3d9364e230ee506f15d27102dab24c15786450fe19",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "a117be620c920e31da07ea6cefa9bbf030d1eed269c2585fb734cda25c035ddd": {
                "Name": "tomcat01",
                "EndpointID": "64a98dcd903c9a87b220489c0ff601a3c005cbd6d4a082d9d94c9cabbfc4f9f6",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "a449c360468adaa6722cbf1a70d285867f817149e1f284346108c73733f37bba": {
                "Name": "tomcat03",
                "EndpointID": "557facc1cb7da810a9241ba9552463eb6eb50ce0bc14ff87551f96247455c0eb",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

# tomcat03 can be connected to tomcat01 and tomcat02
[root@porty ~]# docker exec -it tomcat03 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.079 ms
^C
--- tomcat01 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.079/0.079/0.079/0.000 ms
[root@porty ~]# docker exec -it tomcat03 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.065 ms
^C
--- tomcat02 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.065/0.065/0.065/0.000 ms
[root@porty ~]# docker exec -it tomcat01 ping tomcat03
PING tomcat03 (192.168.0.4) 56(84) bytes of data.
64 bytes from tomcat03.mynet (192.168.0.4): icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from tomcat03.mynet (192.168.0.4): icmp_seq=2 ttl=64 time=0.069 ms
^C
--- tomcat03 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1ms
rtt min/avg/max/mdev = 0.038/0.053/0.069/0.017 ms
[root@porty ~]# docker exec -it tomcat02 ping tomcat03
PING tomcat03 (192.168.0.4) 56(84) bytes of data.
64 bytes from tomcat03.mynet (192.168.0.4): icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from tomcat03.mynet (192.168.0.4): icmp_seq=2 ttl=64 time=0.073 ms
^C
--- tomcat03 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.037/0.055/0.073/0.018 ms

Actual Warfare: Deploying redis clusters

# Create Network Card
docker network create --driver bridge --subnet 172.38.0.0/16 redis

#Create six redis configurations through scripting
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/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 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

# Start 6 Nodes
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
# Test 1-6
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

# View container status after 6 containers have been started
[root@porty bin]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                              NAMES
bd0fbc00bc9e   redis:5.0.9-alpine3.11   "docker-entrypoint.s..."   5 seconds ago        Up 4 seconds        0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp   redis-6
9e48e90d0efe   redis:5.0.9-alpine3.11   "docker-entrypoint.s..."   9 seconds ago        Up 8 seconds        0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp   redis-5
0f31a695cd6f   redis:5.0.9-alpine3.11   "docker-entrypoint.s..."   13 seconds ago       Up 13 seconds       0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp   redis-4
bf06bf4a355c   redis:5.0.9-alpine3.11   "docker-entrypoint.s..."   About a minute ago   Up About a minute   0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp   redis-3
735fb1b13b2e   redis:5.0.9-alpine3.11   "docker-entrypoint.s..."   2 minutes ago        Up 2 minutes        0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp   redis-2
4f9620408053   redis:5.0.9-alpine3.11   "docker-entrypoint.s..."   5 minutes ago        Up 5 minutes        0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp   redis-1

# Enter Node 1 and configure the cluster
[root@porty bin]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof  nodes.conf
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 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 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: fe8cf8086dce179b6b08dc8a68ea525a514063f1 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: 727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: de9e8762b1f53a0a474eb2515ada8e21b31737eb 172.38.0.14:6379
   replicates 727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd
S: a80bfaea952b35794ca7cd2f621658b59e23aa54 172.38.0.15:6379
   replicates fe8cf8086dce179b6b08dc8a68ea525a514063f1
S: ff5332e913729309e00148650952af8197dcaaaa 172.38.0.16:6379
   replicates d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df
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 172.38.0.11:6379)
M: fe8cf8086dce179b6b08dc8a68ea525a514063f1 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd 172.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: a80bfaea952b35794ca7cd2f621658b59e23aa54 172.38.0.15:6379
   slots: (0 slots) slave
   replicates fe8cf8086dce179b6b08dc8a68ea525a514063f1
M: d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df 172.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: ff5332e913729309e00148650952af8197dcaaaa 172.38.0.16:6379
   slots: (0 slots) slave
   replicates d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df
S: de9e8762b1f53a0a474eb2515ada8e21b31737eb 172.38.0.14:6379
   slots: (0 slots) slave
   replicates 727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# Connect redis in a cluster and view information
/data #  redis-cli -c
# View cluster information
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:293
cluster_stats_messages_pong_sent:314
cluster_stats_messages_sent:607
cluster_stats_messages_ping_received:309
cluster_stats_messages_pong_received:293
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:607
# View node information, three masters, three slaves
127.0.0.1:6379> cluster nodes
727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd 172.38.0.13:6379@16379 master - 0 1608777454538 3 connected 10923-16383
a80bfaea952b35794ca7cd2f621658b59e23aa54 172.38.0.15:6379@16379 slave fe8cf8086dce179b6b08dc8a68ea525a514063f1 0 1608777454639 5 connected
d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df 172.38.0.12:6379@16379 master - 0 1608777454000 2 connected 5461-10922
ff5332e913729309e00148650952af8197dcaaaa 172.38.0.16:6379@16379 slave d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df 0 1608777454538 6 connected
de9e8762b1f53a0a474eb2515ada8e21b31737eb 172.38.0.14:6379@16379 slave 727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd 0 1608777455641 4 connected
fe8cf8086dce179b6b08dc8a68ea525a514063f1 172.38.0.11:6379@16379 myself,master - 0 1608777454000 1 connected 0-5460
# Add key-value pairs
127.0.0.1:6379> set name porty
-> Redirected to slot [5798] located at 172.38.0.12:6379 # Discovery is handled by 2 nodes
OK
172.38.0.12:6379> keys *
1) "name"
172.38.0.12:6379> set age 18
-> Redirected to slot [741] located at 172.38.0.11:6379 # 1 node for processing
OK
# Test stops the redis-2 container to see its status from machine redis-6
cluster nodes
727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd 172.38.0.13:6379@16379 master - 0 1608778032042 3 connected 10923-16383
a80bfaea952b35794ca7cd2f621658b59e23aa54 172.38.0.15:6379@16379 slave fe8cf8086dce179b6b08dc8a68ea525a514063f1 0 1608778032000 5 connected
d4d751b5152cbfdc1ce2e99d8ac88ac35c27a3df 172.38.0.12:6379@16379 master,fail - 1608778002575 1608778001000 2 connected # fail can see that this node is no longer valid when the redis-2 container stops
ff5332e913729309e00148650952af8197dcaaaa 172.38.0.16:6379@16379 master - 0 1608778033045 7 connected 5461-10922 # The node of the redis-6 container has been upgraded to host master
de9e8762b1f53a0a474eb2515ada8e21b31737eb 172.38.0.14:6379@16379 slave 727d2f5245803ce83feaf03d1a02c6ce3ac9e6fd 0 1608778034047 4 connected
fe8cf8086dce179b6b08dc8a68ea525a514063f1 172.38.0.11:6379@16379 myself,master - 0 1608778033000 1 connected 0-5460
# Get the key-value pair name = porty created by redis-2 again
get name
-> Redirected to slot [5798] located at 172.38.0.16:6379 # Discovery has been performed by the redis-6 container (slave to the original redis-2)
"porty"

SpringBoot Micro Services Package Docker Mirrors

1. Build a Springboot project

2. Packaging applications

3. Write dockerfile

4. Build a mirror

5. Publish Run

 docker build -t porty666 .
Sending build context to Docker daemon     17MB
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> 98268a327367
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in a3e3ea023c9a
Removing intermediate container a3e3ea023c9a
 ---> 2575d4eb31f1
Step 4/5 : EXPOSE 8080
 ---> Running in 720dae9c3328
Removing intermediate container 720dae9c3328
 ---> 75f74bac0b74
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 66940a1d8b04
Removing intermediate container 66940a1d8b04
 ---> ba3ae7ddc4de
Successfully built ba3ae7ddc4de
Successfully tagged porty666:latest
[root@porty idea]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@porty idea]# docker images
REPOSITORY   TAG                IMAGE ID       CREATED         SIZE
porty666     latest             ba3ae7ddc4de   2 minutes ago   660MB
mysql        5.7                f07dfa83b528   2 days ago      448MB
tomcat       latest             feba8d001e3f   5 days ago      649MB
redis        5.0.9-alpine3.11   3661c84ee9d0   8 months ago    29.8MB
java         8                  d23bdf5b1b1b   3 years ago     643MB
[root@porty idea]# docker run -d -P --name my-project porty666
1469490b8f817ac070f38baeaf270610763e54ed6337f8854abc03a44702d65f
[root@porty idea]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS        PORTS                     NAMES
1469490b8f81   porty666   "java -jar /app.jar ..."   2 seconds ago   Up 1 second   0.0.0.0:49168->8080/tcp   my-project
[root@porty idea]# curl localhost:49168
{"timestamp":"2020-12-24T03:12:48.547+00:00","status":404,"error":"Not Found","message":"","path":"/"}[root@porty idea]# ^C
[root@porty idea]# curl localhost:49168/hello
hello porty![root@porty idea]#

These notes summarize the contents of the Docker course in Crazy Java

Keywords: Java Docker

Added by abhishekphp6 on Thu, 17 Feb 2022 21:04:15 +0200