What is Docker Compose

It is unlikely that your application will only require a single container. Usually you will have several containers for other services like a database, web service, background tasks, etc. For this we use the docker-compose command. docker-compose uses a very simple YAML file to build multiple containers. Each container can have its own Dockerfile that customises the individual container but docker-compose will build all the containers and put them into the same virtual network. Docker containers and services do not even need to be aware that they are deployed on Docker, or whether their peers are also Docker workloads or not. Whether your Docker hosts run Linux, Windows, or a mix of the two, you can use Docker to manage them in a platform-agnostic way.

YAML

# YAML
version: '3'
services:
  web:
    build: .
    ports:
     - "4000:80"
    volumes:
     - .:/code

Compose Two Container

# YAML
version: '3'
services:
  web:
    image: wordpress
    links:
       - mysql
    environment:
       - WORDPRESS_DB_PASSWORD=sample
    ports:
       - "127.0.0.3:8080:80"
  mysql:
    image: mysql:latest
    environment:
        - MYSQL_ROOT_PASSWORD=sample
        - MYSQL_DATABASE=wordpress

Docker Compose in Depth - Volumes and Networks (MultiContainer Componse)

  • --force-recreate: This recreates the container even if there is no need to as nothing within the configuration has changed
  • --no-recreate: This doesn't recreate a container if it already exists; this flag cannot be used with the preceding flag
  • --no-build: This doesn't build the images, even if an image that needs to be built is missing
  • --build: This builds the images before creating the contain

Docker compose

volumes:
     - .:/code
vim app.py
docker-compose down 

Build:
Context: .docker
Dockerfile: myapp.docker
Container_name: myapp

Compose networking

networks:
  default:
    external:
      name: existing-network 

Compose networking

# you need to use the latest version of docker-compose yml
version: '3.5'
services:
  app:
    image: nginx
    networks:
      - my-network-name

networks:
  my-network-name:
   name: my-global-net

Docker Swarm

Create a Cluster and add worker to Cluster

Initiate swarm manager

Joining workers

Cluster Nodes


ABOUT ATABAK

Atabak is a Software and Data Engineering Consultant


FOLLOW ATABAK

© Copyright 2017-2023