Docker Containers
Docker and Ansible

Whole isolated system

OS + Librairies + Applications

Use host ressources (RAM, Disks, ...)

Shared OS + Isolated container

A container only has differences with parent "image"

Docker Engine (Lightweight runtime)

 

  • Create and run OS

  • Build and run containers

  • In-host daemon

Docker Client

 

Communicates with Docker Engine


Execute commands to build, ship and run containers

Docker Client : Memo

sudo docker COMMAND [OPTIONS]

pull image_name:image_tag => Get an image/container
              image_tag can be 'latest' or a known tag. If not specified, takes 'latest'

search distribution => Search for images in official repos

images => List local images

build (run) image_name:image_tag  => Build a container with image (and run it)

run image_name:image_tag command  => Create, run container and execute command given

run -it image_name:image_tag  => Create, run container, allocate TTY and give a prompt

ps => list running containers

start / stop (only) / restart / rm (removes a stopped container) / kill (send SIGKILL signal)

run -m 512m image_name:image_tag  => Allocates memory (512Mo)

run -v vol_name image_name:image_tag  => Create a volume in container

run -v host_dir:vol_name image_name:image_tag  => Create a volume in container and mount host directory into volume

run -p port / -p host_port:container_port image_name:image_tag  => Publish exposed ports

DockerFiles (1/2)

A configuration file describing steps to assemble an image.

A simple text file named "Dockerfile", no extension.

Can build a container from source repository or local DockerFile.

docker build . => build image with dockerFile in current directory.

docker build aa/bb => build image with dockerFile in aa/bb repo.

docker build -t aa/bb . => build with local dockerFile and save in aa/bb repo.

DockerHub is the docker's tool which centralize source repositories. It also do automate builds of DockerFiles before publication.

DockerFiles (2/2)

INSTRUCTION arguments

FROM official_repo/public_repo => The base image

MAINTAINER name <email> => The image author

RUN COMMAND options => Execute command (NOW)

CMD SCRIPT/Command options => Execute command (after build)

LABEL "key"="value" => Add metadatas to image

EXPOSE port => Make port visible (by other containers-see run -p)

ENV key="value" => Define environment variables

ADD src dest => Copy from src (file, directory, archive, URL) to dest (path in container)

COPY src dest => Copy from src (file, directory) to dest (path in container)

ENTRYPOINT command param1 ... paramN => Run container as executable

VOLUME vol_name / host_dir:vol_name => Create volume / Create volume and mount host_dir

USER username => User to execute commands defined in RUN, CMD or ENTRYPOINT

WORKDIR path => Workdir for RUN, CMD, ENTRYPOINT, COPY and RUN

ONBUILD INSTRUCTION args => Instructions to execute after container build

Docker and Ansible

Task/Step

- name: Build my docker container
  docker:
    option: value
    option: value
    ...

Options for basic usage

See : http://docs.ansible.com/ansible/docker_module.html

docker_user : Username or UID to use within the container

env : environment variable

expose : List of exposed ports

hostname : Container hostname

image : Container image used to match and launch containers

memory_limit : Allocated RAM

name : Explicit names of container

ports : List of published ports

pull (missing/always) : When image will be pulled from registry

state : Desired state for the container

volumes : Volumes to mount within the container

volumes_from : List of named containers to mount volumes from

Thank You All

Docker containers and Docker+Ansible

By Ibrahima Sow

Docker containers and Docker+Ansible

  • 511