作业要求
按课件内容实践,并给出实验报告
实验过程
一、准备Docker环境
按照官方指南中的 SET UP THE REPOSITORY方法在CentOS中安装Docker。
因为是第一次安装,所以不需要进行卸载和清空相关文件夹的操作,直接进行安装:
- 设置存储库
安装所需的软件包。yum-utils提供yum-config-manager 功能,devicemapper存储驱动程序需要device-mapper-persistent-data和lvm2。
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
- 使用以下命令来设置稳定的存储库。
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- 安装Docker Engine-Community
直接安装最新版本的Docker Engine-Community和containerd
sudo yum install docker-ce docker-ce-cli containerd.io
安装完成后:
4. 启动Docker。
sudo systemctl start docker
在进行下面的操作之前一定要记住启动Docker。,否则会报以下错误
5. 通过运行hello-world 镜像来验证是否正确安装了Docker Engine-Community。
sudo docker run hello-world
一般情况下如果当前用户不是root用户,这个时候是会报错的。因为docker进程使用Unix Socket而不是TCP端口。而默认情况下,Unix socket属于root用户,需要root权限才能访问。但是docker会默认赋予docker用户组读写Unix socket的权限,所以需要将当前用户加入到docker用户组中,进而也就可以执行docker相关命令。
sudo gpasswd -a 当前用户名 docker #将当前用户加入到docker用户组中
newgrp docker #更新用户组
运行结果:
[fs@fsq ~]$ sudo groupadd docker
groupadd:“docker”组已存在
[fs@fsq ~]$ sudo gpasswd -a fs docker
正在将用户“fs”加入到“docker”组中
[fs@fsq ~]$ newgrp docker
这个时候再次运行hello-world镜像时,因为是第一次运行,系统会提示:
Unable to find image ‘hello-world:latest’ locally
然后自动拉取最新的hello-world镜像:
latest: Pulling from library/hello-world
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest
接下来就是运行hello-world镜像的结果:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
6. 检查Docker版本
docker version
如果没有启动docker,则会有如下输出
二、Docker 基本操作
- 运行镜像
docker run -it ubuntu bash
运行结果:
由于本地暂无ubuntu镜像,所以需要拉取,然后运行。
- 显示本地镜像库内容
docker images
运行结果:
- 获得帮助
docker --help
运行结果:
[fs@fsq ~]$ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/fs/.docker")
-c, --context string Name of the context to use to connect to the daemon
(overrides DOCKER_HOST env var and default context set
with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/home/fs/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/home/fs/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/fs/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
- 显示容器
docker ps #显示运行中容器
docker ps -a #显示所有容器
运行结果:
[fs@fsq ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[fs@fsq ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a365d623fb0 ubuntu "bash" 4 minutes ago Exited (0) 3 minutes ago dazzling_allen
478feb2075f5 hello-world "/hello" 15 minutes ago Exited (0) 15 minutes ago optimistic_blackburn
- 继续运行原容器并进入
docker restart 容器名 #运行容器
docker ps #查看当前运行容器
docker attach 容器名 #进入容器
运行结果:
[fs@fsq ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[fs@fsq ~]$ docker restart dazzling_allen
dazzling_allen
[fs@fsq ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a365d623fb0 ubuntu "bash" 28 minutes ago Up 2 seconds dazzling_allen
[fs@fsq ~]$ docker attach dazzling_allen
root@2a365d623fb0:/# exit
exit