使用compose构建镜像
在docker-compose文件中使用
build参数来构建镜像
注意:需要将构建镜像所需要的文件以及Dockerfile放到一个目录中version: "3.8"services:flask-demo:build: ./flaskenvironment:- REDIS_HOST=redis-servernetworks:- demo-networkports:- 8080:5000redis-server:image: redis:latestnetworks:- demo-networknetworks:demo-network:
然后使用
docker-compose build来构建[root@localhost docker_exec]# docker-compose buildredis-server uses an image, skippingBuilding flask-demoSending build context to Docker daemon 3.072kBStep 1/8 : FROM python:3.9.5-slim3.9.5-slim: Pulling from library/pythonb4d181a07f80: Pull completea1111a8f2ec3: Pull complete445d04774519: Pull complete24f3f85d41f3: Pull completed299f7fb612d: Pull completeDigest: sha256:9828573e6a0b02b6d0ff0bae0716b027aa21cf8e59ac18a76724d216bab7ef04Status: Downloaded newer image for python:3.9.5-slim---> c71955050276Step 2/8 : RUN pip install flask redis && groupadd -r flask && useradd -r -g flask flask && mkdir /src && chown -R flask:flask /src---> Running in 61a9ea4f8b0cCollecting flaskDownloading Flask-2.0.2-py3-none-any.whl (95 kB)Collecting redisDownloading redis-4.0.2-py3-none-any.whl (119 kB)Collecting Werkzeug>=2.0Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)Collecting click>=7.1.2Downloading click-8.0.3-py3-none-any.whl (97 kB)Collecting itsdangerous>=2.0Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)Collecting Jinja2>=3.0Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)Collecting MarkupSafe>=2.0Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)Collecting deprecatedDownloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)Collecting wrapt<2,>=1.10Downloading wrapt-1.13.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (81 kB)Installing collected packages: wrapt, MarkupSafe, Werkzeug, Jinja2, itsdangerous, deprecated, click, redis, flaskSuccessfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 deprecated-1.2.13 flask-2.0.2 itsdangerous-2.0.1 redis-4.0.2 wrapt-1.13.3WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venvWARNING: You are using pip version 21.1.3; however, version 21.3.1 is available.You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.Removing intermediate container 61a9ea4f8b0c---> af1a64177a64Step 3/8 : USER flask---> Running in 649da782ea69Removing intermediate container 649da782ea69---> 684e7f278bafStep 4/8 : COPY app.py /src/app.py---> 9e0ec93ea547Step 5/8 : WORKDIR /src---> Running in 785abca85c97Removing intermediate container 785abca85c97---> fdb04de07568Step 6/8 : ENV FLASK_APP=app.py REDIS_HOST=redis---> Running in 25d62f18b650Removing intermediate container 25d62f18b650---> bbcd791e0d7bStep 7/8 : EXPOSE 5000---> Running in f6f37e829600Removing intermediate container f6f37e829600---> f000ea534a32Step 8/8 : CMD ["flask", "run", "-h", "0.0.0.0"]---> Running in 34c9dde3da21Removing intermediate container 34c9dde3da21---> 7e1bb6b79a10Successfully built 7e1bb6b79a10Successfully tagged docker_exec_flask-demo:latest[root@localhost docker_exec]#[root@localhost docker_exec]#[root@localhost docker_exec]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEdocker_exec_flask-demo latest 7e1bb6b79a10 33 seconds ago 127MBpython 3.9.5-slim c71955050276 5 months ago 115MB
镜像的命名
在docker-compose文件中加上
image参数可指定镜像名称,如果不指定的话镜像名称前缀就会加上project(当前目录)的名称version: "3.8"services:flask-demo:build: ./flaskimage: flask-demo:latestenvironment:- REDIS_HOST=redis-servernetworks:- demo-networkports:- 8080:5000redis-server:image: redis:latestnetworks:- demo-networknetworks:demo-network:
指定Dockerfile进行构建
在build中加上
context以及dockerfile参数,这里把标准的Dockerfile名称改为Dockerfile.devcontext:指定文件目录dockerfile:指定dockerfile的名称version: "3.8"services:flask-demo:build:context: ./flaskdockerfile: Dockerfile.devimage: flask-demo:latestenvironment:- REDIS_HOST=redis-servernetworks:- demo-networkports:- 8080:5000redis-server:image: redis:latestnetworks:- demo-networknetworks:demo-network:
拉取镜像
使用
docker-compose pull来拉取镜像[root@localhost docker_exec]# docker-compose pullPulling flask-demo ... donePulling redis-server ... doneWARNING: Some service image(s) must be built from source by running:docker-compose build flask-demo
Note 通过
docker-compose build以及docker-compose pull可以提前准备好镜像。
