3. docker-compose

最後更新: 2019-08-26

介紹

 

Compose is a tool for defining and running multi-container Docker applications

A YAML file to configure your application

 


Install

 

dnf install podman-compose -y

podman-compose version

['podman', '--version', '']
using podman version: 4.4.1
podman-composer version  1.0.3
podman --version
podman version 4.4.1
exit code: 0

 


Usage

 

help

podman-compose help

opts

  • -p PROJECT_NAME, --project-name PROJECT_NAME          # Default 係當前 Folder Name
  • --dry-run

up

Create and Start the entire stack

i.e.

# -d, --detach              Detached mode: Run containers in the background, print new container names.

podman-compose up -d

# -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)

podman-compose -f proxy.yml up -d

Info

ps                 List containers

podman ps -a --filter label=io.podman.compose.project=program
CONTAINER ID  IMAGE                 COMMAND               CREATED         STATUS         PORTS               NAMES
d79d5c0e41df  localhost/web:latest  /bin/sh -c /usr/b...  14 minutes ago  Up 14 minutes  0.0.0.0:80->80/tcp  program_myweb_1
exit code: 0

Execute a command in a running container (exec)

podman-compose exec [OPTIONS] SERVICE COMMAND [ARGS...]

i.e

podman-compose exec myweb env

opts

  • --user , -u         Run the command as this user.
  • --workdir , -w         Path to workdir directory for this command.
  • --env , -e         Set environment variables
  • --detach , -d         Detached mode: Run command in the background.

Stop/start/restart

Stops running containers without removing them.

They can be started again with docker-compose start.

i.e.

podman-compose stop

down

Stops containers and removes containers, networks, volumes, and images created by up.

Networks and volumes defined as external are never removed.

 


YML

 

Example 1

mkdir -p /data/www/html

echo "Server is working" > /data/www/html/index.html

docker-compose.yml

services:
  www:
    image: docker.io/library/nginx
    volumes:
     - /data/html:/usr/share/nginx/html
    ports:
     - "80:80"
    environment:
     - NGINX_HOST=foobar.com
     - NGINX_PORT=80

podman-compose up

Example 2

一次過 start 2 個 Container. 它們分別是使用現成的 image 及 Dockerfile 建立

services:
  my-custom-app:
    image: my-vue-app
    ...
  my-custom-app:
    build: /path/to/dockerfile/
    ...

 


設定 hosts 檔

 

extra_hosts:
  - "db:192.168.200.21"
  - "www:192.168.200.11"  
  - "api:192.168.200.12"

docker run --add-host "host:ip" ...       # Add a custom host-to-IP mapping

 


Variables

 

services:
  database:
    image: "postgres:${POSTGRES_VERSION}"
    environment:
      DB: mydb

[1]

export POSTGRES_VERSION=x.x.x

docker-compose up

[2]

POSTGRES_VERSION=x.x.x USER=foo docker-compose up