Docker learning-2-Docker basic operation

Two, the basic operation of Docker

2.1 Install Docker

# 1. 安装Docker的依赖环境
yum install yum-utils device-mapper-persistent-data lvm2
# 2. 设置一下下载Docker的镜像源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3. 安装Docker
yum makacache fast   # 安装makacache fast的缓存
yum -y install docker-ce 	#安装Docker服务
# 4. 启动,并设置为开机自启动,测试
# 启动Docker服务
systemctl start docker
# 设置开机自启动
systemctl enable docker
# 测试
docker run hello-world

2.2 Docker's central warehouse

  1. Docker official mirror central warehouse, features: slow download of all mirrors (the server is abroad) https://hub.docker.com/
  2. Domestic mirroring websites: NetEase Honeycomb, daoCloud. . . http://hub.daocloud.io/
  3. The company will use private servers to pull the mirror (add configuration)
# 在/etc/docker/daemon.json
{
    
    
	"registry-mirrors": ["https://registry.docker-cn.com"],
    "insecure-registries": ["ip:port"]  #这里写内部的ip和端口
}
# 重启两个服务
systemctl daemon-reload
systemctl restart docker

2.3 Mirror operation

# 1. 拉取镜像到本地
docker pull 镜像名称[:tag]
# 例
docker pull daocloud.io/library/tomcat:8.5.15-jre8
# 2. 查看全部本地镜像
docker images 
# 3. 删除本地镜像
docker rmi 镜像的唯一标识
# 4. 镜像的导入与导出(不规范)
# 将本地的镜像导出
docker save -o 导出的路径 镜像id
# 加载本地镜像文件
docker load -i 镜像文件
# 修改名称
docker tag 标识id 新名称:Taget(版本)

2.4 Container operation

# 1. 运行容器
# 简单操作
docker run 镜像的标识|镜像名称[:tag]
# 常用操作
docker run -d -p 宿主机端口:容器端口 --name 容器名称 镜像的标识|镜像名称[:tag]
# -d 代表后台运行容器
# -p 宿主机端口:容器端口  为了映射当前Linux的端口和容器的端口  例如tomcat默认是8080,linux的给的端口是8081 则需要映射: 8081:8080
# --name 指定容器的名称
# 2. 查看正在运行的容器
docker ps [-qa]
# -a 查看全部的容器,包括没有运行的容器
# -q 只查看容器的标识其他的不查看

# 3. 查看容器的日志
docker logs -f 容器id
# -f 可以滚动查看日志的最后几行

# 4. 进入到容器内部
docker exec -it 容器id /bin/bash
exit #返回

# 5. 删除容器 (删除容器前需要先停止容器)
docker stop 容器id
docker stop $(docker ps -qa) #停止全部容器
docker rm 容器id
docker rm $(docker ps -qa)  #删除全部容器

# 6. 重新启动容器
docker start 容器id

Guess you like

Origin blog.csdn.net/weixin_43988498/article/details/108933735