View on GitHub

Wiki

A Wiki which shows the results of different courses

Download this project as a .zip file Download this project as a tar.gz file

Navigation

AWS

Microservices

Docker

Kubernetes

ServiceNow

JavaScript

React

Ethical Hacking

Docker Compose

Docker Compose File

Good alternative to start multiple containers in a robust manner. It helps by also giving some more networking options, so you don’t need to use the more complex Docker CLI’s Networking Features.

The docker-compose file has a basic structure like the one below and its written in YAML format:

version: '3'
# in case of Docker a service is most likely a container
services: 
    redis-server:
        # Specify image instead of own build dokerfile
        image: 'redis'
    node-app:
        # Specify dockerfile in the directory
        build: .
        # Specify ports <localPort>:<ContainerPort>
        ports:
            - "4001:8081"

Docker-Compose

Every Container specified in this YAML file will be created in the same network, so the containers have access to each other and you don’t need to specify any ports to open for each other!

So if you want to acces tham you can write something like this in the index.js file:

// In a traditionall environment you would specify a host like 'https://my-redis-server.com' 
// but since we are using docker-compose we can simply specify the name
// port definition is optional
const client =redis.createClient({
    host:'redis-server',
    port: 6379
});

REMARK: The Port definition in the docker-compose file is only to open Ports to the local machine!

Docker Compose Commands

Always run the Docker Compose commands within the directory where the docker-compose file exists, because docker-compose is specifically looking for that file!

Restart policies

There are four restart policies:

Example:

version: '3'
services: 
    redis-server:
        # Specify image instead of own build dockerfile
        image: 'redis'
        restart: "no"
    node-app:
        # Restart policy:
        restart: always
        # Specify dockerfile in the directory
        build: .
        # Specify ports <localPort>:<ContainerPort>
        ports:
            - "4001:8081"