关于docker:docker快速入门

3次阅读

共计 1083 个字符,预计需要花费 3 分钟才能阅读完成。

闲来无事,温习 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
正文完
 0