Technology Docker containers (lower)
A, Dockerfile basic commands
1.1.FROM - based on the reference mirror
- FROM centos # make reference image (based on centos)
FROM scratch # does not rely on any reference mirror base image
- FROM tomcat:9.022-jdk8-openjdk
Try to use the official Base Image
1.2.LABEL & MAINTAINER - descriptive information
- MAINTAINER xxx.com
- LABEL version = "1.0"
- LABEL description = "xxx what role"
1.3.WORKDIR - set the working directory
- WORKDIR /usr/local
- WORKDIR / usr / local / newdir # created automatically
- Try using an absolute path
1.4.ADD & COPY - Copy files
- ADD hello / #f copied to the root path
- ADD test.tar.gz / # add the root directory and extract
- ADD In addition to copying, but also have the added functionality of remote files, + website, similar to wget
1.5.ENV - Setting constant environment
- ENV JAVA_HOME /usr/local/openjdk8
- RUN ${JAVA_HOME}/bin/java -jar test.jar
- Try to use environment constants can improve maintenance procedures
Two, Dockerfile execution instruction
RUN&CMD&ENTRYPOINT
- RUN: Build to build upon the implementation of the equivalent of implementation of the shell
- ENTRYPOINT: start the implementation of the container
- CMD: CMD performed after the container has started [ "ps", "ef"] is equivalent to implementation of exec
Implementation modalities
RUN yum install -y vim #shell命令格式
RUN ["yum","install","-y","vim"] #Exec命令格式
Why should we offer two different ways to perform it?
- shell execution
Use Shell executed, the current shell is the parent process, generate a child process
Executed in sub-shell script, the script is finished, exit the sub-shell, back to the current shell
- exec run
The current process execution
Real
FROM centos
RUN ["echo","image building!!!"]//执行在子进程了
CMD ["echo","container starting..."]//只有这句能看到的
Note: CMD if increased, will replace CMD command, CMD command may not execute
ENTRYPOINT will perform
Third, the image constructed Redis
- Redis is a NoSQL database
- 2010.3.15 start, Redis development, chaired by VMWare
Writing Dockerfile
FROM centos
RUN ["yum","install","-y","gcc","gcc-c++","net-tools","make"]
WORKDIR /usr/local
ADD redis-4.0.14.tar.gz . //会自动解压
WORKDIR /usr/local/redis-4.0.14/src
RUN make && make install
WORKDIR /usr/local/redis-4.0.14
ADD redis-7000.conf .
EXPOSE 7000 //暴露7000端口
CMD ["redis-server","redis-7000.conf"]
After performing mirrored building on the line
docker build -t xxx/docker-redis:1.0 .
docker run -p 7000:7000 xxx/docker-redis:1.0
Dockerfile here just about writing, practical work, mirroring can be used directly redis
Fourth, one-way communication between the container Link
- After the container is created, there is a virtual IP
Container-way access
Principle: Although virtual IP, container communicate, we do not use IP communications, the use of container for messaging
We used to specify the name --name
docker run -d --name web tomcat
docker run -d --name database -it centos /bin/bash
View Virtual IP
docker inspect [containerID]
We use ping
ping 172.17.0.3 是可以ping通的
但是我们ping名称是ping不通的
So we create a second tomcat when linked to a database
docker run --name web --link database tomcat
This time we enter the tomcat, and then ping, can naturally Unicom
ping database 可以自动ping通
Five, two-way communication based Bridge Bridge
Bridge two-way communication theory
To do: Binding and database tomcat
docker run -d --name web tomcat
docker run -d --name database centos /bin/bash
docker network ls
docker network create -d bridge my-bridge
docker network connect my-bridge web
docker network connect my-bridge database
June and share data container Volume
Unused volume container
Volume container principle
method:
1. By providing host directory mount -v
- format:
- docker run --name container -v host name path: the path within the vessel mounted mirror name
- Example:
- docker run --name t1 -v /usr/webapps:/usr/local/tomcat/webapps tomcat
2. By sharing container
- format:
- docker create --name webpage -v /webapps:/tomcat/webapps tomcat /bin/true
- Share vessel mount point
- docker run --volumes-from webpage --name t1 -d tomcat
七、Docker Compose
Multi-vessel deployment will encounter a lot of trouble, so we came out of Docker compose
Case
- Docker Compose single multi-container deployment tools
- By yml file defines how to deploy multi-vessel
- WIN / MAC provided by default Docker Compose, Linux will need to install
installation steps:
1. Obtain automatically installed
pip install -U docker-compose==1.23.2
2. Execute permission
sudo chmod +x /usr/local/bin/docker-compose
Install WordPress
1. Create a directory wordpress
mkdir wordpress
2.vim docker-compose.yml
复制官网上的
3.build the project
docker-compose up -d
八、Docker-compose应用实战
案例、两个SpringBoot项目构建docker-compose
SpringBoot打包
文件 applicaion-dev.yml application.yml bsbdj.jar
vim Dockerfile
FROM openjdk:8u222-jre
WORKDIR /usr/local/bsbdj //上述肯定没有这个目录,所以会创建
ADD bdbdj.jar //加入jar
ADD application.yml .
ADD application-dev.yml .
EXPOSE 80 //暴露端口80
CMD ["java","-jar","bsbdj.jar"]
docker build -t msb.com/bsbdj-app .
docker run msb.com/bsbdj-app
数据库打包
vim Dockerfile
FROM mysql:5.7
WORKDIR /docker-entrypoint-initdb.d
ADD init-db.sql .
docker build -t msb.com/bsbdj-db .
docker run -e -d MYSQL_ROOT_PASSWORD=root msb.com/bsbdj-db
docker exec -it aae73fa77d75 /bin/bash
Docker-Compose进行关联和发布
vim docker-compose.yml
version: '3.3'
services:
db:
build: ./bsbdj-db/
restart: always //容错,自动重启
environment:
MYSQL_ROOT_PASSWORD: root
app:
build: ./bsbdj-app/
depends_on:
- db
ports:
- "80:80"
restart: always
docker-compose up
docker-compose up -d
docker-compose logs
docker-compose down
我们连接数据库,那么yml中jdbc:mysql://db:3306/xxx即可