@[toc]

Docker installation

Installation requirements

#View the current system kernel version (kernel version higher than 3.10)
uname -r
#Check the CentOS version (later than 7)
cat /etc/redhat-release
Copy the code

Docker installation

#1. Uninstall the old version of Docker and its dependencies
yum remove docker docker-common container-selinux docker-selinux docker-engine
#2. Update the yum
yum update
#3. Install yum-utils, which provides yum-config-manager for managing yum sources
yum install -y yum-utils
#4. Add the yum source
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#5. Update indexes
yum makecache fast
#6. Install the docker
yum install -y docker-ce docker-ce-cli containerd.io
#7. Start the docker
systemctl start docker
Copy the code

Uninstall Docker

yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
Copy the code

Configuring Mirror Acceleration

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://eekuwuxe.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Copy the code

Docker command

#Start the Docker service
systemctl start docker.service
#Set docker to start automatically after startup
systemctl enable docker
#Viewing the Version Number
docker version  
#More detailed version information
docker info  
docker --help
#View all Docker networks
docker network ls
Copy the code

Docker operation image

#Common commandsDocker run [options] IMAGE [command][arg...] Docker commit -m 'commit description' -a 'author' container ID Docker build -t new image name :TAG
#List the mirrorDocker images -q # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Images --no-trunc # Displays complete image information
#The query imageDocker Search -s 40 image name # List the number of favorites is not less than the specified value of the image name -automated Docker search Only images of the automated Build type are listed
#Remove the mirrorDocker rmI -f $(docker images-QA) # delete all images from docker image Docker run [options] IMAGE [command][arg...] # start the container -- the name 'new container name' # specify a name for the container - restart = # # always let since the launch of the container - d background container, and return the container ID, or start the tutelary container - I # in interactive mode container, usually used in conjunction with - t - t # for the container to redistribute a pseudo input terminal, usually used in conjunction with the -i - P - P # # random port mapping specified port mapping, IP has the following four formats: hostPort: containerPort IP: : containerPort hostPost:containerPort containerPort		
#If --restart=always is not specified, run the update command to set it
docker update --restart=always xxx
Copy the code

Docker operates on containers

#List the containerDocker ps # lists currently running containers - A # Lists all currently running and historically running containers -l # displays the most recently created containers -n 2 # Displays the most recently created containers -q # Silent mode displays only container numbers --no-trunc # does not truncate the output#Out of the containerCTRL + P + Q # The container does not stop exiting
#Remove the containerDocker rm container # Delete container docker rm -f container # Delete container docker rm container1 container2... # to delete multiple containers docker rm -f $# (docker ps - a - q) one-time delete multiple containers docker ps - a - q | xargs docker rm # one-time delete multiple containers
#Operation container container(referring to container ID or container name)Docker restart Container # Stop container # Stop container docker kill Container # Forcibly stop container docker run -d container Docker logs -f -t --tail container # Check the container log -t add the timestamp -f # follow the latest log --tail 5 # Display the last number of top Docker containers Docker inspect container # Inspect container details (in json form) docker execit container bash # Re-enter container (open new terminal) docker execit Container sh # OCI Runtime exec failed Exec failed docker attach container # Re-enter the container docker cp container: destination host path # Copy files from the container to the hostCopy the code

Docker appends container port mappings

  1. Gets the ID of the container
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9d673f254336 nginx "nginx -g 'daemon Of..." 25 hours ago Up 21 hours 0.0.0.0:80->80/ TCP nginxCopy the code
  1. Stop all containers
[root@localhost ~]# docker stop nginx
nginx
Copy the code
  1. Stop the Docker service
systemctl stop docker
Copy the code
  1. Modify the file
#Here container_id is the folder starting with 9D673F254336 obtained in the first step
vim   /var/lib/docker/containers/{container_id}/hostconfig.json
Copy the code

Find the PortBindings value. Append a new port to the JSON collection

{..."PortBindings": {
        "80/tcp": [{"HostIp": ""."HostPort": "80"}]."81/tcp": [{"HostIp": ""."HostPort": "81"}}],... }Copy the code

If Ports are recorded in config.v2.json, modify config => ExposedPorts and NetworkSettings => Ports

{..."Config": {..."ExposedPorts": {
            "80/tcp": {},
            "81/tcp": {}},... },"Image": "sha256:6678c7c2e56c970388f8d5a398aa30f2ab60e85f20165e101053c3d3a11e6663"."NetworkSettings": {..."Ports": null.// If the value is null, no change is required. },... }Copy the code
  1. Start the Docker service
systemctl start docker
Copy the code
  1. Start the container
docker start nginx
Copy the code

Once you start up, you’ll see an extra port

[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9d673f254336 nginx "nginx -g 'daemon Of..." 25 hours ago Up 18 seconds 0.0.0.0:80->80/ TCP, 0.0.0.0:81->81/ TCP nginxCopy the code

Docker data volume

#Data volume commandDocker inspect container ID # Check whether the data volume is mounted successfully docker run-it -v/ host absolute path directory :/ Container directory Ro mirror name # Set permissions#Data volume container command
#1. Start a parent container dc01 and create folder dataVolumeContainer
#2. Create DC02 to inherit DC01
docker run -it --name dc02 --volumes-from dc01 zzyy/centos
Copy the code

DockerFile architecture

Reserved words Break down
FROM Base mirror, which mirror the current new mirror is based on
MAINTAINER The name and email address of the mirror maintainer
RUN Commands that need to be run when the container is built
EXPOSE The exposed port of the current container
WORKDIR Specifies the working directory that the terminal will log in to by default after creating the container, a landing point
ENV Used to set environment variables during the image build process
ADD Copy the files in the host directory to the image and the ADD command automatically processes the RUL and decompresses the tar package
CPOY Similar to ADD, copy files and directories to the image
VOLUME Container data volumes for data preservation and persistence
CMD Specifies the command to run when the container is started, with the last CMD overwriting the previous commands
ENTRYPOINT Specifies a command to run when a container is started, without overwriting previous instructions
ONBUILD Onbuild is triggered when you run the command to build an inherited Dockerfile
—–
## DockerCompose

The installation of DockerCompose

#Docker-compose download docker-composeThe curl -l https://github.com/docker/compose/releases/download/1.25.0/docker-compose- ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-compose curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose- ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-compose#Modify the permissions
sudo chmod +x /usr/local/bin/docker-compose 
#Viewing version Information
docker-compose --version 
Copy the code

DockerCompose command

#Run in the directory docker-comemage. ymlDocker-compose ps # docker-compose logs web # docker-compose stop docker-compose Docker-compose rm: docker-compose rm: docker-compose rm: docker-compose rm Docker-compose --help # more docker-compose commandsCopy the code

Docker – compose a Tomcat installation

  1. Create the Tomcat folder in the specified location
  2. newdocker-compose.ymlFile, fill in the following
version: '3' # yML version number
services:  Docker-compose: which services to compose (which services to start)
    tomcat:  # Specific services to be choreographed
        restart: always The server will start after the VIRTUAL machine restarts
        image: tomcat:latest The mirror name of the service
        container_name: tomcat The name of the container
        ports: # mapped port
          - 8080: 8080
        volumes: # data volume
          - ./webapps:/usr/local/tomcat/webapps
          - ./logs:/usr/local/tomcat/logs
          - ./conf:/usr/local/tomcat/conf
        environment: # Environment parameters
          TZ: Asia/Shanghai
Copy the code
  1. Perform the following steps to run the following commands in the current Tomcat directory
#1. Pull the mirror
docker pull tomcat
#2. Run the container
docker run --name tomcat -p 8080:8080 -d tomcat
#3. Copy the configuration file to the host
docker cp tomcat:/usr/local/tomcat/conf ./
#5. Stop the container and delete it
docker stop tomcat
docker rm tomcat
#6. Run the container again
docker-compose up -d
Copy the code

Docker – compose the Mysql installation

Version: '3.1' services: mysql: image: mysql:latest container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: root --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true --lower_case_table_names=1 ports: - 3306:3306 volumes: - ./data:/var/lib/mysqlCopy the code

Install Redis Docker – compose

version: "3.1"
services:
  redis:
    image: redis:latest
    container_name: redis
    ports:
      - 6379: 6379
    volumes:
      - ./conf:/usr/local/etc/redis
      - ./data:/data
    command: redis-server --requirepass 123456 # Your password
Copy the code

Zookeeper Docker – compose installation

Stand-alone version

version: '2'
services:
  zookeeper:
    image: zookeeper
    restart: always
    container_name: zookeeper
    volumes:
      - ./config:/conf
    ports: 
      - "2181:2181"
    environment:
      ZOO_MY_ID: 1
Copy the code

Cluster version

version: '3'
services:
    zoo1:
        image: zookeeper
        restart: always
        container_name: zoo1
        volumes:
            - ./config:/conf
        ports:
            - "2181:2181"
        environment:
            ZOO_MY_ID: 1
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo2:
        image: zookeeper
        restart: always
        container_name: zoo2
        volumes:
            - ./config:/conf
        ports:
            - "2182:2181"
        environment:
            ZOO_MY_ID: 2
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888

    zoo3:
        image: zookeeper
        restart: always
        container_name: zoo3
        volumes:
            - ./config:/conf
        ports:
            - "2183:2181"
        environment:
            ZOO_MY_ID: 3
            ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
Copy the code

Install the RabbitMq Docker – compose

version: '3'
services:
  rabbitmq:
    image: rabbitmq:management-alpine
    container_name: rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=xxx
      - RABBITMQ_DEFAULT_PASS=xxxxx
    restart: always
    ports:
      - "15672:15672"
      - "5672:5672"
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"
Copy the code

Docker deploys the SpringBoot project

Write a Dockerfile file

# Docker image for springboot file run
# VERSION 1.0.0
# Author: Manaphy
#The base image uses Java
FROM java:8
#The author
MAINTAINER Manaphy <[email protected]>
#VOLUME specifies the temporary file directory as/TMP.
#The effect is to create a temporary file in the host /var/lib/docker directory, linked to the container's/TMP
VOLUME /tmp 
#Add the JAR package to the container and rename it app.jar
ADD hello.jar app.jar
#Run the jar package
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Copy the code

Place the JAR package and Dockerfile in the same folder and use the following command to build the image

#The -t argument is the tag name that specifies the image
docker build -t springbootdemo4docker .
Copy the code

Use docker run -d -p 8080:8080 springBootDemo4Docker to start the project

Idea integration docker image packaging one key deployment

1. Docker enables remote access

Note: Do not enable remote access on the server, you can enable testing in your own VIRTUAL machine

#Modify the Docker service file
vim /lib/systemd/system/docker.service
#Modify the line beginning ExecStartExecStart = / usr/bin/dockerd -h TCP: / / 0.0.0.0:2375 - H Unix: / / / var/run/docker. The sockCopy the code
#Reload the configuration file and start
systemctl daemon-reload
systemctl start docker
#Test whether the modification is successful
curl http://localhost:2375/version
Copy the code

2. The idea of configuration docker

Advanced versions of IDEA already integrate docker plug-in configuration by defaultConfiguring Mirror Acceleration

3. Modify the Springboot poM file

<! Add docker.image.prefix to properties -->
<properties>
    <! -- Mirror name prefix -->
    <docker.image.prefix>manaphy</docker.image.prefix>
</properties>

<! Add the following information to your plugins -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
        <! -- Mirror name -->
        <imageName>${docker.image.prefix}/${project.artifactId}</imageName>

        <! -- Specify the tag -->
        <imageTags>
            <imageTag>latest</imageTag>
        </imageTags>
        <! JDK 8-->
        <baseImage>java</baseImage>
        <! -- Producer provides personal information -->
        <maintainer>manaphy [email protected]</maintainer>
        <! -- Switch to the /root directory -- here is the path in the mirror -->
        <workdir>/ROOT</workdir>
        <cmd>["java","-version"]</cmd>
        <entryPoint>["java","-jar","${project.build.finalName}.jar"]</entryPoint>
        <! Remote docker API address -->
        <dockerHost>http://xxxxx:2375</dockerHost>

        <! Dockerfile path --><! Dockerfile -->
        <! --<dockerDirectory>${project.basedir}/src/main/java</dockerDirectory>-->

        <resources>
            <resource>
                <targetPath>/ROOT</targetPath>
                <! ${project.build.directory} specifies the target directory -->
                <directory>${project.build.directory}</directory>
                <! ${project.build.finalName}. Jar specifies the jar package file to copy -->
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
    <executions>
        <! MVN package docker:build-->
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Copy the code

4. Perform the build

Use Maven to perform packaging in the Docker toolbar to find the image you just uploadedCreate Containers Create... Click Run to complete the deployment

Docker works with Nginx to deploy vue projects

1. Docker pull nginx image 2. Write the following to vim Dockerfile

FROM nginx:latest
MAINTAINER Manaphy <[email protected]>
COPY dist/ /usr/share/nginx/html/     
Copy the code

Vue project NPM install Build 4. Build the project by placing the dist folder and Dockerfile in the same directory

docker build -t docker4vue .
Copy the code

5. Run commands to create a container

docker run -d --name docker4vue -p 8888:80 docker4vue
Copy the code