1. Docker commands
# centos 7
systemctl start docker # 启动服务
systemctl stop docker
systemctl restart docker # 重启服务
systemctl status docker
systemctl enable docker # 开机自启动
1.1 Mirror related commands
# 查看镜像
docker images
docker images -q # 查看所有的镜像ID
# 搜索镜像
docker search redis #可以直接去官网查看docker官方支持的镜像 hub.docker.com
# 拉取镜像
docker pull redis[: 版本号]
# 删除镜像
docker rmi IMAGEID
1.2 Container-related commands
# 查看容器
docker ps -a # -a:查看所有的容器包括历史的
# 创建并启动容器
# -i:容器一直运行 -t:分配一个伪终端 --name:起名字
docker run -i -t --name=c1 image:版本 /bin/bash # 交互式容器
# -d:后台运行容器
docker run -i -d --name=c2 image:版本 # 守护式容器
# 退出容器 利用-it创建的容器exit后容器会立刻关闭
exit
# 进入容器
docker exec -i -t 容器名 /bin/bash
# 停止容器
docker stop 容器名
# 启动容器
docker start 容器名
# 删除容器(不能删除运行的容器)
docker rm 容器名
# 查看容器信息
docker inspect 容器名
1.3 Container data volume
1.3.1 The concept and function of data volume
-
After the Docker container is deleted, the data generated in the container is gone
-
Docker containers cannot directly exchange files with external machines
-
How to exchange data between containers? —> data volume
- A data volume is a directory or file on the host
- When the container directory and the data volume directory are bound, the modification of the other party will be synchronized immediately
- A data volume can be mounted by multiple containers at the same time
- A container can be attached to multiple data volumes
-
The role of data volumes
- Persistence of container data
- Indirect communication between external machines and containers
- Data exchange between containers
1.3.2 Configuration of data volume
-
When creating a container,
-v
set the data volume usingdocker run ... -v 宿主机目录:容器内目录 [-v 宿主机目录:容器内目录]
-
Notice:
- The directory must be an absolute path
- If the directory does not exist, it will be created automatically
- Can be attached to multiple data volumes
-
Data interaction between containers can be realized by mounting the same data volume through multiple containers
docker run -it --name=c3 -v /root/data:/root/data centos:7 docker run -it --name=c4 -v /root/data:/root/data centos:7 # 容器中在挂在的目录修改添加文件后,两个容器的数据是同步的 echo hhhh>a.txt
1.3.3 Data volume container
-
Create and start the c3 data volume container, and use
-v
parameters to set the data volumedocker run -it --name=c3 -v /volume centos:7 -v /volume 没有指定宿主机的数据卷目录,docker会自动分配一个目录,可使用docker inspect c3查看
-
Create and start c1 c2 container, use
–-volumes-from
mount to container c3docker run -it --name=c1 --volumes-from c3 centos:7 docker run -it --name=c2 --volumes-from c3 centos:7
-
At this time, c1 and c2 are mounted on c3, and c3 is mounted on the host machine. Essentially, c1, c2, and c3 are all mounted to the same location of the host machine.
2. Docker application deployment
2.1 MySql deployment
- Deploy MySQL in a Docker container and operate MySQL Server through an external mysql client
question:
- The network service in the container cannot directly communicate with the external machine
- The external machine and the host can communicate directly
- Containers and hosts can communicate directly
Solution :Port Mapping
When the network service in the container needs to be accessed by an external machine, the port providing the service in the container can be mapped to the port of the host machine . The external machine can access the service of the container by accessing the port of the host machine
step:
-
search mirror
docker search mysql
-
pull image
docker pull mysql:5.6
-
Create container, set port mapping, directory mapping (data volume)
#先在宿主机/root目录下创建一个mysql目录,用于存储mysql数据信息 mkdir ~/mysql cd ~/mysql/
createPort Mapping, data volume
docker run -id \ # 以后台运行的方式创建容器 -p 3307:3306 \ # 端口映射 宿主端口:容器服务端口 --name=c_mysql \ -v $PWD/conf:/etc/mysql/conf.d \ # 必要的数据卷设置 -v $PWD/logs:/logs \ # 同上 -v $PWD/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ # 初始化root用户的密码 mysql:5.6 # 镜像以及版本的选择
into the container
docker exec -it c_mysql /bin/bash
After entering the container, you can directly log in to mysql and perform corresponding operations on mysql
At this point, you can access the container through the mapped port on the external host
-
operating container
2.2 Deploy tomcat
mkdir ~/tomcat
cd ~/tomcat
docker run -id --name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat
在宿主机器上创建HTML网页,例如创建在$PWD/test/index.html
浏览器访问:http://192.168.148.129:8080/test/index.html
2.3 deploy redis
- First, download the image of a redis version (such as 5.0) according to the above command, and then execute the following command
docker run -id --name=c_redis -p 6379:6379 redis:5.0
3. Dockerfile
- Main role: used to make Docker images
3.1 Docker image principle
-
Mirror principle
- The Docker image is superimposed by a special file system
- The bottom is bootfs, using the host's bootfs
- The second layer is the root file system rootfs, called base image
- Then superimpose his mirror image file on top
- unified file systemThe technology integrates different layers into a file system, and provides a unified perspective for these layers, so as to hide the existence of multiple layers. From the user's point of view, there is only one file system
- A mirror can be placed on top of another mirror. The image below is called the parent image, and the bottommost image is the base image
- The image is read-only, but Docker can load a readable and writable file system as a container, and then form a new image
3.2 How to make a Docker image
-
Convert container to image
# 将一个容器转化为镜像 docker commit 容器id 镜像名称:版本号 # 镜像无法传输,因此需要将镜像转化为压缩文件以便于传输 docker save -o 压缩文件名称 镜像名称:版本号 # 将压缩文件解压为镜像 docker load -i 压缩文件名称
- When a docker container is converted into a mirror image, only the files or data generated in the container will be saved as a mirror image.
-
Dockerfile
make-
Writing Dockerfile can give priority to copying the wording on the official website
-
Dockerfile Keywords Keyword Guidelines
Write a good Dockerfile
Use the docker bulid command to build an image, and then the image can be compressed and transmitted to other users
docker build -f dockerfile文件路径 -t 镜像名称:版本 .
Dockerfile case practice:
To customize a centos7 image, requires:
- The default login path is:
/usr
- can use
vim
The Dockerfile is as follows:
# dockerfile 注意dockerfile中需要的镜像资源,本机如果有就会使用本机的 # 若本机没有会自动下载 FROM centos:7 MAINTAINER dzc<12345@qq.com> RUN yum install -y vim WORKDIR /usr CMD /bin/bash
#docker bulid 构建镜像 # -f docker文件路径 # -t 镜像名称:版本号 # .搜索路径 docker build -f ./centos_dockerfile -t centos_with_vim:1.0 .
docker build
The last number is actually the directory of the context environment.
in the specified image building process# 由镜像创建并启动容器,即可进入 docker run -it --name=centos_vim centos_with_vim:1.0
-
4. Docker service orchestration
4.1 Service orchestration concept
docker-compose.yml
Define the relationship between containers, startup sequence, etc. in the file
4.2 docker-compose
Installation
# 二进制包的方式安装到linux中(命令多试几次就行了)
curl -L https://github.com/docker/compose/release/download/1.22.0/docker-compose-`uname-s`-`uname-m` -o /usr/local/bin/docker-compose
# 设置文件的可执行权限
chmod +x /usr/local/bin/docker-compose
# 查看版本
docker-compose --version
4.3 An example video explanation of docker-compose.yml
5. Docker private warehouse
5.1 Build a private warehouse (just follow the instructions)
5.2 Transfer the image to the private warehouse
①Tag the mirror first (mark the mirror as a mirror of the private warehouse)
# 查看有啥镜像
docker iamges
>>---------------------------虚线下为终端输出
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_with_vim 1.0 087e14c3fff7 About an hour ago 477MB
nginx latest 605c77e624dd 19 months ago 141MB
tomcat latest fb5657adc892 19 months ago 680MB
redis 5.0 c5da061a611a 19 months ago 110MB
mysql 5.6 dd3b2a5dcb48 19 months ago 303MB
registry latest b8604a3fe854 20 months ago 26.2MB
centos 7 eeb6ee3f44bd 22 months ago 204MB
# 打标签(勿忘端口号)
docker tag centos_with_vim:1.0 192.168.148.129:5000/centos_vim
# 再查看下镜像
docker iamges
>>---------------------------虚线下为终端输出
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_with_vim 1.0 087e14c3fff7 About an hour ago 477MB
192.168.148.129:5000/centos_vim latest 087e14c3fff7 About an hour ago 477MB
nginx latest 605c77e624dd 19 months ago 141MB
tomcat latest fb5657adc892 19 months ago 680MB
redis 5.0 c5da061a611a 19 months ago 110MB
mysql 5.6 dd3b2a5dcb48 19 months ago 303MB
registry latest b8604a3fe854 20 months ago 26.2MB
centos 7 eeb6ee3f44bd 22 months ago 204MB
②push to private warehouse
docker push 192.168.148.129:5000/centos_vim
>>>----------------------------------------
Using default tag: latest
The push refers to repository [192.168.148.129:5000/centos_vim]
5f70bf18a086: Pushed
e125f7c01fa0: Pushed
174f56854903: Pushed
latest: digest: sha256:eadf3fbeaabf26be9b490a31dcd395899a2025deee37fa5be0045a7df30f7a39 size: 947
5.3 Pull the mirror image of the private warehouse to the local
# 192.168.148.129:5000/centos_vim 分别是私有仓库的地址和端口以及其镜像的名称
docker pull 192.168.148.129:5000/centos_vim
6. Docker related concepts
If this article is useful to you, you can like and bookmark this article. It will be much easier to find next time you use it. It would be great
if you can follow the author. The author will continue to learn, output, and share! Thank you for your encouragement!