练习-3
编写一个死循环,每隔6秒显示带有自己姓名的字符串,使用 docker run 命令运行的镜像是centos。
通过 docker logs -tf --tail 10 容器ID显示出实时容器中的日志变化。
docker ps -a 观察 STATUS 的值。
通过 docker 命令停止刚才运行的容器。docker ps -a 观察 STATUS 的变化。
通过 docker 命令删除刚才的容器。docker ps -a 查看容器相关信息。
编写一个死循环,并制作成 centos 镜像
[root@master ~]# vi Dockerfile
# Dockerfile
FROM ubuntu:latest
CMD ["bash", "-c", "while true; do echo 'name_test'; sleep 6; done"]
[root@master ~]# docker build -t centos .
Sending build context to Docker daemon 9.211GB
Step 1/2 : FROM centos:latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
---> 5d0da3dc9764
Step 2/2 : CMD ["bash", "-c", "while true; do echo 'name_test'; sleep 6; done"]
---> Running in 14c500989412
Removing intermediate container 14c500989412
---> 34b5075f7f8a
Successfully built 34b5075f7f8a
Successfully tagged centos:latest
#查看镜像
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 34b5075f7f8a 3 minutes ago 231MB
centos <none> 5d0da3dc9764 2 years ago 231MB
运行镜像
[root@master ~]# docker run -itd centos
6cdc9acb734450b303233d294fac69b4511316689e77897d4befe47baabac4e3
查看镜像
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cdc9acb7344 centos "bash -c 'while true…" 44 seconds ago Up 41 seconds thirsty_easley
通过docker logs -tf --tail 10 容器ID显示出实时容器中的日志变化
[root@master ~]# docker logs -tf --tail 10 6cdc9acb7344
2024-05-26T12:33:47.484432303Z name_test
2024-05-26T12:33:53.489292565Z name_test
2024-05-26T12:33:59.502040038Z name_test
2024-05-26T12:34:05.511525140Z name_test
2024-05-26T12:34:11.517315467Z name_test
2024-05-26T12:34:17.523748479Z name_test
2024-05-26T12:34:23.536542119Z name_test
2024-05-26T12:34:29.547570593Z name_test
2024-05-26T12:34:35.552796160Z name_test
2024-05-26T12:34:41.558256446Z name_test
docker ps -a 观察 STATUS 的值
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cdc9acb7344 centos "bash -c 'while true…" About a minute ago Up About a minute objective_robinson
通过 docker 命令停止刚才运行的容器
直接强制删除容器命令 docker rm -f a60c90320f6c(容器ID)
[root@master ~]# docker container stop 6cdc9acb7344
6cdc9acb7344
docker ps -a 观察 STATUS 的变化
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cdc9acb7344 centos "bash -c 'while true…" 5 minutes ago Exited (137) About a minute ago objective_robinson
通过 docker 命令删除刚才的容器。docker ps -a 查看容器相关信息
[root@master ~]# docker rm 6cdc9acb7344
6cdc9acb7344
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES