四十六、Docker Compose-镜像构建与拉取
2021-12-31 本文已影响0人
InsaneLoafer
使用compose构建镜像
- 在docker-compose文件中使用
build
参数来构建镜像
注意:需要将构建镜像所需要的文件以及Dockerfile放到一个目录中
version: "3.8"
services:
flask-demo:
build: ./flask
environment:
- REDIS_HOST=redis-server
networks:
- demo-network
ports:
- 8080:5000
redis-server:
image: redis:latest
networks:
- demo-network
networks:
demo-network:
- 然后使用
docker-compose build
来构建
[root@localhost docker_exec]# docker-compose build
redis-server uses an image, skipping
Building flask-demo
Sending build context to Docker daemon 3.072kB
Step 1/8 : FROM python:3.9.5-slim
3.9.5-slim: Pulling from library/python
b4d181a07f80: Pull complete
a1111a8f2ec3: Pull complete
445d04774519: Pull complete
24f3f85d41f3: Pull complete
d299f7fb612d: Pull complete
Digest: sha256:9828573e6a0b02b6d0ff0bae0716b027aa21cf8e59ac18a76724d216bab7ef04
Status: Downloaded newer image for python:3.9.5-slim
---> c71955050276
Step 2/8 : RUN pip install flask redis && groupadd -r flask && useradd -r -g flask flask && mkdir /src && chown -R flask:flask /src
---> Running in 61a9ea4f8b0c
Collecting flask
Downloading Flask-2.0.2-py3-none-any.whl (95 kB)
Collecting redis
Downloading redis-4.0.2-py3-none-any.whl (119 kB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)
Collecting click>=7.1.2
Downloading click-8.0.3-py3-none-any.whl (97 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
Collecting MarkupSafe>=2.0
Downloading 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 deprecated
Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting wrapt<2,>=1.10
Downloading 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, flask
Successfully 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.3
WARNING: 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/venv
WARNING: 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
---> af1a64177a64
Step 3/8 : USER flask
---> Running in 649da782ea69
Removing intermediate container 649da782ea69
---> 684e7f278baf
Step 4/8 : COPY app.py /src/app.py
---> 9e0ec93ea547
Step 5/8 : WORKDIR /src
---> Running in 785abca85c97
Removing intermediate container 785abca85c97
---> fdb04de07568
Step 6/8 : ENV FLASK_APP=app.py REDIS_HOST=redis
---> Running in 25d62f18b650
Removing intermediate container 25d62f18b650
---> bbcd791e0d7b
Step 7/8 : EXPOSE 5000
---> Running in f6f37e829600
Removing intermediate container f6f37e829600
---> f000ea534a32
Step 8/8 : CMD ["flask", "run", "-h", "0.0.0.0"]
---> Running in 34c9dde3da21
Removing intermediate container 34c9dde3da21
---> 7e1bb6b79a10
Successfully built 7e1bb6b79a10
Successfully tagged docker_exec_flask-demo:latest
[root@localhost docker_exec]#
[root@localhost docker_exec]#
[root@localhost docker_exec]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker_exec_flask-demo latest 7e1bb6b79a10 33 seconds ago 127MB
python 3.9.5-slim c71955050276 5 months ago 115MB
镜像的命名
- 在docker-compose文件中加上
image
参数可指定镜像名称,如果不指定的话镜像名称前缀就会加上project(当前目录)的名称
version: "3.8"
services:
flask-demo:
build: ./flask
image: flask-demo:latest
environment:
- REDIS_HOST=redis-server
networks:
- demo-network
ports:
- 8080:5000
redis-server:
image: redis:latest
networks:
- demo-network
networks:
demo-network:
指定Dockerfile进行构建
- 在build中加上
context
以及dockerfile
参数,这里把标准的Dockerfile
名称改为Dockerfile.dev
-
context
:指定文件目录 -
dockerfile
:指定dockerfile的名称
-
version: "3.8"
services:
flask-demo:
build:
context: ./flask
dockerfile: Dockerfile.dev
image: flask-demo:latest
environment:
- REDIS_HOST=redis-server
networks:
- demo-network
ports:
- 8080:5000
redis-server:
image: redis:latest
networks:
- demo-network
networks:
demo-network:
拉取镜像
- 使用
docker-compose pull
来拉取镜像
[root@localhost docker_exec]# docker-compose pull
Pulling flask-demo ... done
Pulling redis-server ... done
WARNING: Some service image(s) must be built from source by running:
docker-compose build flask-demo
Note
通过docker-compose build
以及docker-compose pull
可以提前准备好镜像。