Docker related commands

Container related commands

Mirroring : Docker mirroring is superimposed by the file system (a form of file storage); it is the core concept in docker. It can be considered that mirroring is a package for certain operating environments or software, and users can download it from the docker warehouse The basic image is localized. For example, developers can pull (download) a basic image that only contains the centos7 system from the docker warehouse, and then install jdk, mysql, Tomcat, and self-developed applications in this image, and finally make these environments into one new mirror. The developer submits the new image to the tester for testing, and the tester only needs to run the image in the test environment, so that the developer's environment and the tester's environment are completely consistent.

View local mirror

# 查看镜像可以使用如下命令: 
docker images

REPOSITORY: mirror name

TAG: mirror tag

IMAGE ID: Image ID

CREATED: The date the image was created

SIZE: image size

search mirror

# 如果你需要从网络中查找需要的镜像,可以通过以下命令搜索 
docker search 镜像名称

NAME: image name

DESCRIPTION: image description

STARS: user evaluation, reflecting the popularity of a mirror image

OFFICIAL: whether it is official

AUTOMATED: Automatically built, indicating that the image was created by the Docker Hub automatic build process

pull image

# 拉取镜像就是从Docker仓库下载镜像到本地,镜像名称格式为 名称:版本号,如果版本号不指定则是最新的版本 命令如下: d
docker pull 镜像名称 
# 如拉取centos 7; 
docker pull centos:7

delete mirror

# 可以按照镜像id删除镜像,命令如下: 
docker rmi 镜像id

1. docker rmi $IMAGE_ID: delete the specified image

2. docker rmi  docker images -q: delete all images

Container related commands

Container is also the core concept in docker. A container is a running instance generated by running a mirror image. The relationship between images and containers is like the relationship between classes and objects in the Java language.

1. Check the container

查看正在运行的容器使用命令:
docker ps
查看所有容器使用命令:
docker ps -a

2. Create and start the container

You can create and start a container based on an existing image, and use the command to create and start a container: docker run

Create a daemon container; for a long-running container, we can create a daemon container. The command is as follows (the container name cannot be repeated):

#创建并启动守护式容器 
docker run -di --name=centos2 centos:7 
#登录进入容器命令为:docker exec -it container_name (或者 container_id) /bin/bash(exit退出 时,容器不会停止) 
docker exec -it centos2 /bin/bash

3. Stop and start the container

# 停止正在运行的容器:docker stop 容器名称或者ID 
docker stop centos2 
# 启动已运行过的容器:docker start 容器名称或者ID 
docker start centos2

4. File copy

# docker cp 需要拷贝的文件或目录 容器名称:容器目录 

# 创建一个文件abc.txt 
touch its.txt 

# 复制abc.txt到mycentos2的容器的 / 目录下 
docker cp it.txt centos2:/root

# 进入mycentos2容器 
docker exec -it centos2 /bin/bash 

# 查看容器 / 目录下文件 
ll

Copy the file from the container to the linux host using the command:

# docker cp 容器名称:容器目录 需要拷贝的文件或目录

#进入容器后创建文件cba.txt 
touch it.log 

# 退出容器 
exit 

# 在Linux宿主机器执行复制;将容器mycentos2的/cba.txt文件复制到宿主机器的/root目录下 
docker cp centos2:/root/it.log /root

5. Directory mount

When creating a container, the directory of the host machine can be mapped with the directory in the container, so that we can modify the files in a certain directory of the host machine to affect the container.

Add the -v parameter when creating a container, followed by the host directory: container directory, for example: docker run -di -v /root/binlog:/root/binlog --name=centos3 centos:7

# 创建linux宿主机器要挂载的目录 
mkdir /root/binlog 

# 创建并启动容器centos3 ,并挂载linux中的/root/binlog 目录到容器的/root/binlog ;也就是在 linux中的/root/binlog 中操作相当于对容器相应目录操作 
docker run -di -v /root/binlog:/root/binlog --name=centos3 centos:7

# 在linux下创建文件 
touch /root/binlog/mysql.log 

# 进入容器 docker exec -it centos3 /bin/bash

# 在容器中查看目录中是否有对应文件def.txt 
ll /root/binlog

6. View container ip

# 在linux宿主机下查看 mycentos3 的ip 
docker inspect centos3

7. Delete the container

# 删除容器 
docker rm centos3

Supongo que te gusta

Origin blog.csdn.net/qq_44872509/article/details/130714212
Recomendado
Clasificación