一、实验要求
1.安装docker服务,配置镜像加速器
2.下载系统镜像(Ubuntu、 centos)
3.基于下载的镜像创建两个容器 (容器名一个为自己名字全拼,一个为首名字字母)
4.容器的启动、 停止及重启操作
5.查看正在运行的容器和所有容器
6.退出容器的两种方法,分别实现
7.连接到运行的容器
8.查看容器或镜像的内部信息
9.查看所有镜像
二、实验步骤
1. 安装docker服务,配置镜像加速器
bash
1.1 安装docker服务
[root@localhost ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
1.2 配置镜像加速器
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors":["https://registry.docker-cn.com"]
}
2.下载系统镜像Ubuntu、 CentOS
bash
2.1 重载docker启动配置
[root@localhost ~]# systemctl daemon-reload
2.2 启动 docker服务
[root@localhost ~]# systemctl start docker.service
2.3 将docker设为开机自启
[root@localhost ~]# systemctl enable docker.service
2.4 搜索官方镜像仓库
[root@localhost ~]# docker search nginx
2.5 拉取镜像
[root@localhost ~]# docker pull nginx
[root@localhost ~]# docker pull busybox
2.6 查看当前主机镜像列表
[root@localhost ~]# docker image ls
2.7 导出镜像
[root@localhost ~]# docker image save busybox > docker-busybox.tar.gz
[root@localhost ~]# ls docker-busybox.tar.gz
[root@localhost ~]# docker image save -o /mnt/busybox_nginx.tar.gz busybox:latest nginx:1.14-alpine
[root@localhost ~]# ls /mnt/busybox_nginx.tar.gz
2.8 导入镜像
[root@localhost ~]# docker image load -i docker-busybox.tar.gz
[root@localhost ~]# docker image ls
3.基于下载的镜像创建两个容器 (容器名一个为自己名字全拼,一个为首名字字母)
bash
3.1 创建名字全拼容器
[root@localhost ~]# docker run --name zhangyongkun nginx:1.14-alpine
3.2 创建首字母容器
[root@localhost ~]# docker run --name zyk nginx:1.14-alpine
3.3 查看容器
[root@localhost ~]# docker ps
4.容器的启动、 停止及重启操作
bash
4.1 容器启动
[root@localhost ~]# docker start zhangyongkun
zhangyongkun
4.2 容器停止
[root@localhost ~]# docker stop zhangyongkun
zhangyongkun
4.3 容器重启
[root@localhost ~]# docker restart zhangyongkun
zhangyongkun
5.查看正在运行的容器和所有容器
bash
5.1 查看正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
214af431456c nginx "/docker-entrypoint...." 3 minutes ago Up About a minute 80/tcp zhangyongkun
5.2 查看所有容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
120353d5796f nginx "/docker-entrypoint...." 2 minutes ago Exited (0) 2 minutes ago zyk
214af431456c nginx "/docker-entrypoint...." 4 minutes ago Up About a minute 80/tcp zhangyongkun
6.退出容器的两种方法,分别实现
bash
6.1 方法一
[root@localhost ~]# exit
6.2 方法二
Ctrl+D
7.连接到运行的容器
bash
[root@localhost ~]# docker exec -it zhangyongkun /bin/sh
# ls
bin dev docker-entrypoint.sh home lib32 libx32 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib lib64 media opt root sbin sys usr
#exit
8.查看容器或镜像的内部信息
bash
[root@localhost ~]# docker image inspect centos
9.查看所有镜像
bash
[root@localhost ~]# docker image
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Download an image from a registry
push Upload an image to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.