SpringBoot learning (four)-Docker

Four, Docker

Docker is installed directly on the operating system.

1. Core concepts

1. Docker image (images)

Similar to Windows mirroring

2. Docker container (Container)

Similar to virtual machine, lightweight virtual machine

3. Docker client (CLIENT)

The client uses the DOCKER through the command line or other tools

4. Docker host (HOST)

A physical or virtual host used to execute the Docker daemon and container

5. Docker warehouse (Registry)

Where to store the Docker image

2. Docker installation

1. WINDOWS installation

Install directly...

1. Encountered problem one: Cannot enable Hyper-V service

Look at it later.

2. Linux installation (CENTOS7)

Linux installation of Docker requires a kernel version greater than 3

[[email protected] /]# uname -r

yum install docker

yum install docker

Confirm successful installation

[[email protected] /]# docker -v
Docker version 1.13.1, build 7d71120/1.13.1

Start docker

systemctl start docker

Set boot up

systemctl enable docker

Stop docker

systemctl stop docker

Three. Use of Docker

1. Search for mirrors (docker search xxx)

docker search mysql

2. Download the mirror

Download the default version

docker pull mysql

Download the specified version

docker pull mysql:版本号

3. View the downloaded image

docker images

4. Delete (there must be no generated container, regardless of whether the container is running or not)

docker rmi imageid

5. Start

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
# 运行tomcat镜像的lastest版本,命名为mytomcat
docker run --name mytomcat -d tomcat:lastest

OPTIONS description:

-a stdin: Specify the standard input and output content type, STDIN/STDOUT/STDERR can be selected;

-d: Run the container in the background and return the container ID;

-i: Run the container in interactive mode, usually used together with -t;

-P: random port mapping, the internal port of the container is randomly mapped to the port of the host

-p: Specify port mapping, the format is: host (host) port: container port

-t: Reallocate a pseudo input terminal for the container, usually used at the same time as -i;

--Name="nginx-lb": Specify a name for the container;

--Dns Specify the DNS server used by the container, which is the same as the host by default;

--Dns-search example.com: Specify the container DNS search domain name, the default is the same as the host;

-h "mars": Specify the hostname of the container;

-e username="ritchie": set environment variables;

–Env-file=[]: read environment variables from the specified file;

–Cpuset=“0-2” or --cpuset=“0,1,2”: Bind the container to the specified CPU to run;

-m: Set the maximum memory usage of the container;

--Net="bridge": Specify the network connection type of the container, support bridge/host/none/container: four types;

–Link=[]: add a link to another container;

–Expose=[]: open a port or a group of ports;

--Volume, -v: bind a volume

6. Stop

docker stop ID OR NAME
docker stop mytomcat

7. View all containers (including those that have been stopped)

docker ps -a

8. Delete the container (the container must be stopped)

docker rm id or name

9. Restart the stopped container

docker start id or name

10. Port mapping

Map the server port and the port in docker

# 9000为服务器端口,6379为docker容器端口
-p 9000:6379 

Start the port-mapped container

docker run -d --name tomcattest -p 9000:8080 tomcat:latest

11. View the log

docker logs id or name

12. Mount


13. Enter the container (exit without stopping)

docker exec -it  CONTAINER_ID OR CONTAINER_NAME  bash

-it means

 exec:                                     Run a command in a running container(在运行的容器中运行命令)
exec -i:  --interactive(相互作用的)       Keep STDIN open even if not attached(即使没有连接,也要保持STDIN打开)
exec -t:   --tty                          Allocate a pseudo-TTY(分配一个 冒充的终端设备)

Three. Docker environment construction

1. mysql

If you only do this, you won’t be able to start

docker run -d --name dockermysql -p 13306:3306 mysql:latest

See why it can't start

docker logs dockermysql 

The database password is not configured and must be specified or set to no password.

2021-06-09 11:59:05+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    You need to specify one of the following:

If the sequence is different, the startup may fail, and the test sequence succeeds.

docker run -p 13306:3306  --name dockermysql  -e MYSQL_ROOT_PASSWORD=pwd-d bingozhou/mysql5.7

Connecting to Navicat reports an error, you need to enter the docker container

docker exec -it dockermysql bash

Re-enter the database

mysql -u root -p
# 使用MYSQL库
use mysql
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';  ### 123456 mysql的登录密码
flush privileges;

2. Redis

docker run -p 16379:6379  --name dockerredis   -d redis:latest

3. Rabbitmq

4. elasticsearch

5. Nacos

1. Download

docker pull nacos/nacos-server

2. Start (stand-alone mode)

docker run \
  --name nacos \
  --restart=always \
  -d \
  -e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
  -e NACOS_USER=nacos\
  -e NACOS_PASSWORD=nacos\
  -e JVM_XMS=512m \
  -e JVM_XMX=512m \
  -e JVM_XMN=256m \
  -e JVM_MS=32m \
  -e JVM_MMS=80m \
  -p 8848:8848 \