Docker Compose:从开发到生产

播客转录的翻译是在“ Linux管理员”课程开始时准备的








Docker Compose —

, .

, YAML-

.



docker compose v3 YAML- , Docker Swarm.



, docker-compose ? ? , — , :



  • :

    , .
  • : (

    ) docker-compose , -

    , docker compose .




, ,

. , ,

, . -

.



,

, ( ),

,

, . .



, , .

(

).





,

, , .



Docker compose compose-

. :



$ cat docker-compose.yml
version: "3.2"

services:
  whale:
    image: docker/whalesay
    command: ["cowsay", "hello!"]
$ docker-compose up
Creating network "composeconfigs_default" with the default driver
Starting composeconfigs_whale_1
Attaching to composeconfigs_whale_1
whale_1  |  ________
whale_1  | < hello! >
whale_1  |  --------
whale_1  |     \
whale_1  |      \
whale_1  |       \
whale_1  |                     ##        .
whale_1  |               ## ## ##       ==
whale_1  |            ## ## ## ##      ===
whale_1  |        /""""""""""""""""___/ ===
whale_1  |   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
whale_1  |        \______ o          __/
whale_1  |         \    \        __/
whale_1  |           \____\______/
composeconfigs_whale_1 exited with code 0


, docker compose compose-

, . :



$ cat docker-compose.second.yml
version: "3.2"
services:
  whale:
    command: ["cowsay", "bye!"]

$ docker-compose -f docker-compose.yml -f docker-compose.second.yml up
Creating composeconfigs_whale_1
Attaching to composeconfigs_whale_1
whale_1  |  ______
whale_1  | < bye! >
whale_1  |  ------
whale_1  |     \
whale_1  |      \
whale_1  |       \
whale_1  |                     ##        .
whale_1  |               ## ## ##       ==
whale_1  |            ## ## ## ##      ===
whale_1  |        /""""""""""""""""___/ ===
whale_1  |   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
whale_1  |        \______ o          __/
whale_1  |         \    \        __/
whale_1  |           \____\______/
composeconfigs_whale_1 exited with code 0


,

.



, docker compose

docker-compose.override.yml docker-compose.yml.

, , :



$ mv docker-compose.second.yml docker-compose.override.yml
$ docker-compose up
Starting composeconfigs_whale_1
Attaching to composeconfigs_whale_1
whale_1  |  ______
whale_1  | < bye! >
whale_1  |  ------
whale_1  |     \
whale_1  |      \
whale_1  |       \
whale_1  |                     ##        .
whale_1  |               ## ## ##       ==
whale_1  |            ## ## ## ##      ===
whale_1  |        /""""""""""""""""___/ ===
whale_1  |   ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
whale_1  |        \______ o          __/
whale_1  |         \    \        __/
whale_1  |           \____\______/
composeconfigs_whale_1 exited with code 0


, .







. :



services:
  my-service:
    build:
      context: .
    image: private.registry.mine/my-stack/my-service:${MY_SERVICE_VERSION:-latest}
...


docker-compose build ( push)

$MY_SERVICE_VERSION, latest,

,

private.registry.mine.





, , .

:



  • , ( )

    docker-compose.
  • , ,

    .
  • .
  • docker-compose.yml.


  • , (, ,

    ).


  • ,

    .
  • - docker stack deploy — compose-file docker-compose.yml --with-registry-auth my-stack-name.
  • docker-compose up -d.


.



# docker-compose.yml
...
services:
  my-service:
    build:
      context: .
    image: private.registry.mine/my-stack/my-service:${MY_SERVICE_VERSION:-latest}
    environment:
      API_ENDPOINT: ${API_ENDPOINT:-https://production.my-api.com}
...




# docker-compose.override.yml
...
services:
  my-service:
    ports: # This is needed for development!
      - 80:80
    environment:
      API_ENDPOINT: https://devel.my-api.com
    volumes:
      - ./:/project/src
...


docker-compose (docker-compose up),

, /project/src.



!

docker-compose.yml .

,

CI:



export MY_SERVICE_VERSION=1.2.3
docker-compose -f docker-compose.yml build
docker-compose -f docker-compose.yml push


:



export MY_SERVICE_VERSION=1.2.3
docker stack deploy my-stack --compose-file docker-compose.yml --with-registry-auth


,

:



export MY_SERVICE_VERSION=1.2.3
export API_ENDPOINT=http://staging.my-api.com
docker stack deploy my-stack --compose-file docker-compose.yml --with-registry-auth


docker-compose ,

!






« Linux»







All Articles