Skip to content

Instantly share code, notes, and snippets.

@0xDones
Last active November 13, 2018 17:36
Show Gist options
  • Save 0xDones/35f49f2623f0ef0cb5f0c8f4054d51e3 to your computer and use it in GitHub Desktop.
Save 0xDones/35f49f2623f0ef0cb5f0c8f4054d51e3 to your computer and use it in GitHub Desktop.
[Docker Documentation] This is a small summary a made about docker, docker-compose and docker swarm #docker #docker-compose #swarm

Installation

After the installation, you have to add your user to docker group to run docker without sudo permission

sudo usermod -aG docker $USER

Container Images

Dockerfile:

FROM ubuntu:tag # Sets the image
MAINTAINER Denis Policastro <denis.policastro@gmail.com>
ADD . . # Copy items from host to VM (All from dir to workdir)
COPY /src . # Files or dir
LABEL Description=”Descrição do projeto” # Metadata, D version
ENTRYPOINT [“usr/bin/apache2ctl”, “-D”, “FOREGROUND”] # Main process running on the container, if the process dies the container dies
CMD [“node”, “app.js”] # Params for the entrypoint, example if entrypoint is a bash, ls is a param
ENV TEST=”Test env” # Declare env vars
USER denis # Sets the user, default is root
WORKDIR /srv # Set working dir
VOLUME /host/dir /container/dir

CLI

Running Container:

docker run \
-d | -it \ #Default -d
[--publish | -p] \
[--memory] \
[--cpu-shares] \
[--rm] \ 
[--env | -e] \
[--network] \
[--net-alias] \
[--name] \
[-v] \
nginx
  • --publish | -p: - Specifies the exposed port
    HOST_PORT:CONTAINER_PORT

  • --cpu-shares: - Sets the cpu share percent, i.e, if(100 = total) => 25 = 25%
    512

  • --memory: - Specifies the max memory consumption for that container
    512m

  • --rm: - Automatic remove container when exit

  • --environment | -e: - Sets environment variable
    MYSQL_RANDOM_ROOT_PASSWORD=yes

  • --network: - Specifies the network
    NETWORK_NAME

  • --net-alias: - Gives an alias for load balancing the containers inside the container network
    ALIAS

  • --name: - Gives the container a name
    CONTAINER_NAME

  • -v: - Bind a custom volume to the host from the container for persistent data
    named_volume:/var/lib/mysql
    ./mount/to/bind:/var/lib/mysql

Using net alias:

Execute the commands to see it working:

docker container run -d --network es_network --net-alias search elasticsearch:2
docker container run -d --network es_network --net-alias search elasticsearch:2

# Create another one for querying:
docker container run -it --network es_network centos:7 bash
nslookp search
curl -s search:9200

Containers commands:

# Stopping
docker container top

# Inspecting
docker container inspect --format=’{{ .NetworkSettings }}’ CONTAINER_ID
docker container stats -> CPU, MEM

# Starting (--attach --interactive)
docker start --ai CONTAINER_NAME

# Enter container or execute command
docker exec [-it] CONTAINER_NAME {bash | command}

# Change runtime configuration:
docker update --help
# Limits CPU, RAM
docker run --memory 512m --cpu-shares 1024 --name nginx1 nginx:lastest
docker run --memory 512m --cpu-shares 512 --name nginx2 nginx:lastest
docker run --memory 512m --cpu-shares 512 --name nginx3 nginx:lastest
docker container -m 256m nginx

Networking

  • Overlay: - Used by docker swarm to communicate between containers
  • Bridge: - Used by containers to communicate network

Commands:

docker network ls
docker network inspect NETWORK_NAME
docker network create NETWORK_NAME
docker network connect NETWORK_NAME CONTAINER_NAME
docker network disconnect

Docker-Compose

Commands:

docker-compose -f docker-compose.yml {up|top|down|start|top|build} [-d, --build] 

YML file

version: '2'

services:
  drupal:
    image: drupal-custom
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '8080:80'
    volumes:
      - drupal-modules:/var/www/html/modules
      - drupal-profiles:/var/www/html/profiles
      - drupal-sites:/var/www/html/sites
      - drupal-themes:/var/www/html/themes
  postgres:
    image: postgres:9.6
    #ports: We dont need ports here, because we're using the docker network
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=mypass
      - POSTGRES_DB=postgres
    volumes:
      - drupal-data:/var/lib/postgresql/data

volumes:
  drupal-modules:
  drupal-profiles:
  drupal-sites:
  drupal-themes:
  drupal-data:

Docker Swarm

Commands:

docker info

# Check if swarm is enabled
docker swarm init

# Initialize swarm’s master node
docker service create alpine ping 8.8.8.8

# Creates a service
docker service ls | docker service ps SERVICE_NAME

# Check service status or config
docker service update SERVICE_NAME --replicas 3

Creating two services in the same network:

Create the overlay network, for the communication between swarm services:

docker network create --driver overlay mydrupal

Create the services, passing the --network:

docker service create --name drupal --network mydrupal -p 80:80 drupal

docker service create docker service create --name psql --network mydrupal -e POSTGRES_PASSWORD=mypass postgres

Changes service configuration dynamicly:

docker swarm update --help

Configuring swarm nodes:

# This command generates an output, that needs to be executed on worker nodes
docker swarm init --advertise-addr PUBLIC_IP

# Changes node role to manager
docker node update --role manager NODE_NAME

# Generates the output to join the service as a manager
docker swarm join-token manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment