使用compose构建镜像

  • 在docker-compose文件中使用build参数来构建镜像
    注意:需要将构建镜像所需要的文件以及Dockerfile放到一个目录中

    1. version: "3.8"
    2. services:
    3. flask-demo:
    4. build: ./flask
    5. environment:
    6. - REDIS_HOST=redis-server
    7. networks:
    8. - demo-network
    9. ports:
    10. - 8080:5000
    11. redis-server:
    12. image: redis:latest
    13. networks:
    14. - demo-network
    15. networks:
    16. demo-network:
  • 然后使用docker-compose build来构建

    1. [root@localhost docker_exec]# docker-compose build
    2. redis-server uses an image, skipping
    3. Building flask-demo
    4. Sending build context to Docker daemon 3.072kB
    5. Step 1/8 : FROM python:3.9.5-slim
    6. 3.9.5-slim: Pulling from library/python
    7. b4d181a07f80: Pull complete
    8. a1111a8f2ec3: Pull complete
    9. 445d04774519: Pull complete
    10. 24f3f85d41f3: Pull complete
    11. d299f7fb612d: Pull complete
    12. Digest: sha256:9828573e6a0b02b6d0ff0bae0716b027aa21cf8e59ac18a76724d216bab7ef04
    13. Status: Downloaded newer image for python:3.9.5-slim
    14. ---> c71955050276
    15. Step 2/8 : RUN pip install flask redis && groupadd -r flask && useradd -r -g flask flask && mkdir /src && chown -R flask:flask /src
    16. ---> Running in 61a9ea4f8b0c
    17. Collecting flask
    18. Downloading Flask-2.0.2-py3-none-any.whl (95 kB)
    19. Collecting redis
    20. Downloading redis-4.0.2-py3-none-any.whl (119 kB)
    21. Collecting Werkzeug>=2.0
    22. Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)
    23. Collecting click>=7.1.2
    24. Downloading click-8.0.3-py3-none-any.whl (97 kB)
    25. Collecting itsdangerous>=2.0
    26. Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
    27. Collecting Jinja2>=3.0
    28. Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
    29. Collecting MarkupSafe>=2.0
    30. 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)
    31. Collecting deprecated
    32. Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
    33. Collecting wrapt<2,>=1.10
    34. 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)
    35. Installing collected packages: wrapt, MarkupSafe, Werkzeug, Jinja2, itsdangerous, deprecated, click, redis, flask
    36. 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
    37. 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
    38. WARNING: You are using pip version 21.1.3; however, version 21.3.1 is available.
    39. You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
    40. Removing intermediate container 61a9ea4f8b0c
    41. ---> af1a64177a64
    42. Step 3/8 : USER flask
    43. ---> Running in 649da782ea69
    44. Removing intermediate container 649da782ea69
    45. ---> 684e7f278baf
    46. Step 4/8 : COPY app.py /src/app.py
    47. ---> 9e0ec93ea547
    48. Step 5/8 : WORKDIR /src
    49. ---> Running in 785abca85c97
    50. Removing intermediate container 785abca85c97
    51. ---> fdb04de07568
    52. Step 6/8 : ENV FLASK_APP=app.py REDIS_HOST=redis
    53. ---> Running in 25d62f18b650
    54. Removing intermediate container 25d62f18b650
    55. ---> bbcd791e0d7b
    56. Step 7/8 : EXPOSE 5000
    57. ---> Running in f6f37e829600
    58. Removing intermediate container f6f37e829600
    59. ---> f000ea534a32
    60. Step 8/8 : CMD ["flask", "run", "-h", "0.0.0.0"]
    61. ---> Running in 34c9dde3da21
    62. Removing intermediate container 34c9dde3da21
    63. ---> 7e1bb6b79a10
    64. Successfully built 7e1bb6b79a10
    65. Successfully tagged docker_exec_flask-demo:latest
    66. [root@localhost docker_exec]#
    67. [root@localhost docker_exec]#
    68. [root@localhost docker_exec]# docker image ls
    69. REPOSITORY TAG IMAGE ID CREATED SIZE
    70. docker_exec_flask-demo latest 7e1bb6b79a10 33 seconds ago 127MB
    71. python 3.9.5-slim c71955050276 5 months ago 115MB

    镜像的命名

  • 在docker-compose文件中加上image参数可指定镜像名称,如果不指定的话镜像名称前缀就会加上project(当前目录)的名称

    1. version: "3.8"
    2. services:
    3. flask-demo:
    4. build: ./flask
    5. image: flask-demo:latest
    6. environment:
    7. - REDIS_HOST=redis-server
    8. networks:
    9. - demo-network
    10. ports:
    11. - 8080:5000
    12. redis-server:
    13. image: redis:latest
    14. networks:
    15. - demo-network
    16. networks:
    17. demo-network:

    指定Dockerfile进行构建

  • 在build中加上context以及dockerfile参数,这里把标准的Dockerfile名称改为Dockerfile.dev

    • context:指定文件目录
    • dockerfile:指定dockerfile的名称
      1. version: "3.8"
      2. services:
      3. flask-demo:
      4. build:
      5. context: ./flask
      6. dockerfile: Dockerfile.dev
      7. image: flask-demo:latest
      8. environment:
      9. - REDIS_HOST=redis-server
      10. networks:
      11. - demo-network
      12. ports:
      13. - 8080:5000
      14. redis-server:
      15. image: redis:latest
      16. networks:
      17. - demo-network
      18. networks:
      19. demo-network:

      拉取镜像

  • 使用docker-compose pull来拉取镜像

    1. [root@localhost docker_exec]# docker-compose pull
    2. Pulling flask-demo ... done
    3. Pulling redis-server ... done
    4. WARNING: Some service image(s) must be built from source by running:
    5. docker-compose build flask-demo

    Note 通过docker-compose build以及docker-compose pull可以提前准备好镜像。