python构建镜像例子(安装vim和telnet)
1.创建脚本存放目录
mkdir /root/docker_test
2.准备测试脚本
vi /root/docker_test/script.py
[root@master docker_test]# more script.py
#!/usr/bin/python
#_*_coding:utf-8_*_
import time
while True:
print("helloworld",flush=True)
time.sleep(10)
3.主备docker file文件
vi /root/docker_test/Dockerfile
#拉取Docker环境
#FROM python
FROM registry.cn-shenzhen.aliyuncs.com/hxlk8s/python:3.13
RUN apt-get update && apt install -y vim && apt install -y telnet
#设置工作目录,容器中的目录,不是宿主机的目录
WORKDIR /app
#将dockerfile同级文件copy到docker容器内app目录下
COPY . /app
#运行python的命令
CMD ["python", "/app/script.py"]
4.构建镜像
在dockerfile同级目录下运行下列指令创建一个镜像
[root@node1 docker_test]#cd /root/docker_test
[root@node1 docker_test]#docker build -t python_test .
5.查看刚才制作的镜像
[root@master docker_test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python_test latest bd5d93f9058d 7 minutes ago 1.08GB
6.运行Docker容器
[root@node1 docker_test]# docker run -d python_test:latest
700ec3da071074ea939ad80c47ef8200c0b8cebec70aba4ded4c2355e314387f
7.查看docker容器运行情况
[root@master docker_test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7b3e697f489 python_test:latest "python /app/script.…" 8 minutes ago Up 8 minutes stoic_cray
[root@master docker_test]# docker logs a7b3e697f489
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
helloworld
注意:带上flush=True才会持续打印输出
print("helloworld",flush=True)
8.登录容器查看安装的vim和telnet工具
[root@master docker_test]# docker exec -it a7b3e697f489 /bin/bash
root@a7b3e697f489:/app# which vim
/usr/bin/vim
root@a7b3e697f489:/app# which telnet
/usr/bin/telnet