闲来无事,温习docker的时候,正好写一篇疾速入门
根底环境
零碎为centos7.7,发现没有docker,嗯,一条命令搞定
yum -y install docker #yum装置docker
systemctl start docker.service #启动docker
定义Dockerfile
dockerfile用来构建镜像的文本文件,文本内容为构建镜像所须要的指令和阐明
定义一个超级简略的Dockerfile
[root@te1 app]# cat Dockerfile
FROM python:2.7-slim #跟面向对象语言的类一样,示意以哪个根底镜像定制
WORKDIR /data/docker/app #docker工作目录
COPY . /data/docker/app #复制以后文件到/data/docker/app目录
RUN pip install -r requirements.txt #在docker build时运行,此处通过pip装置flask
CMD ["python", "hello.py"] #在docker run时运行此命令
[root@te1 app]# cat requirements.txt
flask
[root@te1 app]# cat hello.py
#一个简略的flask web站点
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return 'hello world'
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
通过Dockerfile编译镜像
cd /data/docker/app && docker build -t hello .
查看镜像
docker images
通过镜像运行一个容器
docker run -it -d --name 'first-hello' -p80:8000 hello
-it -i 规范输出,-t示意容许一个终端
-d 后盾运行
–name 容器名字
-p 对外裸露端口,此处将docker的8000端口映射为80向外裸露
查看容器过程
docker ps -a
拜访这个flask的web容器
[root@te1 app]# curl 127.0.0.1:80
hello world
进入容器
有些时候须要进入容器调试排查故障等
[root@te1 app]# docker exec -it first-hello bash
root@64660eb25be0:/data/docker/app# ls
Dockerfile dump.rdb hello.py requirements.txt
发表回复