Docker
Hub.docker.com // Image address
Docker installation
Y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y y Lvm2 / / modify yum installation source, the default download slow yum abroad - config - manager - add - 'http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo // install yum -y install docker-ceCopy the code
Start the validation
Docker pull helloworld // check whether the docker version is available. Docker run hello-worldCopy the code
Ali Cloud Docker image acceleration
To accelerate the operation of visiting foreign warehouses through ali Cloud acceleration service, you need to open the service in Ali Cloud
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": [" https://xxxxx.mirror.aliyuncs.com "] / / accelerator address} EOF sudo systemctl daemon - reload sudo systemctl restart docker // Verify that the docker pull tomcat is successfulCopy the code
Common Docker commands
- Docker pull tomcat:9.0-jdk8- AdoptopenJDK – openJ9
- Docker Images – View local images
- Docker run image name <:tags> – Create container, start application
- Docker PS – View images in progress
- Docker stop Container ID Stop a container
- Docker rm <-f(mandatory)> Container ID – Delete the container
- Docker rmI <-f> Image name: – Delete image
The host communicates with the Docker container
Clients cannot access docker directly, they need to access the host and map to the Docker service
Configure the mapping and start the Docker container
- docker run -p 8000:8080 tomcat
- Docker run -p 88:8080 -d Tomcat background run
Execute commands in containers
- Docker exec [-it] container id command
- Exec executes the command in the corresponding container
- -it Executes commands in interactive mode
- Docker exec-it 0738ed2fe68b /bin/bash
Container life cycle
Dockerfile build image
introduce
Docker automatically generates images by reading the instructions in the Dockerfile
The command
The FROM tomcat: latest picture / / set the base MAINTAINER HTTP: / / pengasan.com / / set the owner of the current scene WORKDIR/usr/local/tomcat/webapps/ADD/switch working directory Docker-web./docker-web // Copy docker-web to the container docker-webCopy the code
- <:tags> Dockerfile directory // Build a custom image
- Eg. The docker build -t pengasan.com/mywebapp:1.0
Dockerfile basic command
- FROM centos:lastest
- LABEL & MAINTAINER – Provides information
- MAINTAINER pengasan.com
- LABEL version = “1.0”, LABEL Description = “mall item”
- WORKDIR – Sets the working directory
- WORKDIR /usr/local/workdir /usr/local/newdir
- ADD & COPY – Copies files
- ADD hello / # copy to root
- ADD test.tar.gz / # ADD the root directory and unpack it
- ADD has the function of adding remote files in addition to replication
- ENV – Sets environment constants
- ENV JAVA_HOME /usr/local/openjdk8
- RUN ${JAVA_HOME}/bin/java -jar test.jar
- The expose-exposed container port is just a statement
- EXPOSE 8080
- docker run -p 8000:8080 tomcat
Dockerfile Run the command
- RUN: Executes commands during Build builds
- ENTRYPOINT: command executed when the container is started
- CMD: Executes default commands or parameters after the container is started
RUN runs at build time
- RUN yum install -y vim #Shell
- RUN [“yum”,”install”,”-y”,”vim”] #Exec
ENTRYPOINT Startup command
- ENTRYPOINT(ENTRYPOINT) is used to execute commands when the container is started
- Only the last ENTRYPOINT in a Dockerfile will be executed
- ENTRYPOINT [“ps”] # Exec format is recommended
CMD Default command
- CMD is used to set the command to be executed by default (the default command will be overridden if there are additional commands in the start command)
- If more than one CMD appears in a Dockerfile, only the last one is executed
- CMD is ignored if the container is started with instructions attached
- CMD [“ps”, “-ef”] # Exec format is recommended
Link one-way communication between containers
Each time the container is created, it will be given an IP, which is dynamic. Of course, the IP between containers is naturally available for communication, but once restarted, the IP will change, which greatly increases the difficulty of our daily work, so we use link communication to optimize this problem
Docker run -d --name web tomcat # it /bin/bash Docker run -d --name database -it centos /bin/bash docker inspect container ID/ name Docker run -d --name web --link database tomcat docker run -d --name web --link database tomcatCopy the code
Bridge bidirectional communication
introduce
Realize the principle of
The command
- Docker network ls // View network services
- Docker network create -d bridge my-bridge // Create a custom bridge
- Docker network connect my-bridge docker network connect my-bridge
All containers bound to a bridge can communicate with each other.
Volume Data is shared between containers
background
If the data in a container changes, we do not need to manually update the data from one container to another. We can directly use the volume source data to let other containers read the data
Mount host command
The first way (the -v way)
- Format: docker run –name Container name -v host path: mount path image name in the container
- Example: docker run – name tomcat1 – v/usr/webapps: / usr/local/tomcat/webapps tomcat
Second way (by sharing in-container mount points)
- Create a shared container :docker create –name webpage -v /webapps:/tomcat/webapps tomcat/ bin/true
- Docker run –volumes-from webpage –name t1-d tomcat
Docker Compose
Container Orchestration tool Single-node multi-container deployment tool. For cluster deployment, other tools such as K8S define multi-container deployment through YML files
The installation
- Sudo curl – L “github.com/docker/comp… -s)-$(uname -m)” -o /usr/local/bin/docker-compose
- sudo chmod +x /usr/local/bin/docker-compose
The deployment of
Write yML build scripts
Version: '3.3' services: db: image: mysql:5.7 volumes: -db_data :/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {}Copy the code
Compile the yML file docker-compose up -d
In actual combat
Build Dockerfile
Building a Web container
FROM openjdk:8u222-jre
WORKDIR /usr/local/webapp
ADD ucas.jar .
ADD application.yml
EXPOSE 80
CMD ["java","-jar","ucas.jar"]
Copy the code
Create image docker build-t pengasan.com/ucas-web
Run docker run pengasan.com/ucas-web
Creating a database container
Mysql :5.7 WORKDIR /docker-entrypoint-initdb.d ADD init-db.sqlCopy the code
Create image docker build-t pengasan.com/ucas-db
Run docker run -d -e MYSQL_ROOT_PASSWORD=root pengasan.com/ucas-db
DockerCompose is published in one click
The command format is docker-comemage. yml
Version: '3.3' services: db: build:./ucas-db MYSQL_ROOT_PASSWORD: root // Add environment variable Change root password app: build:./ucas-web/ depends_on: -db ports: - "80/80" restart: alwaysCopy the code