Basis of Docker

Installation

Normal Windows Installer

Command

version check

  • docker --version
  • docker-compose --version
  • docker-machine --version
  • docker version detailed description of docker
  • docker info

Images

Fixed point of a (fixed, saved)container

docker images list all the local images

Image to Container

docker run -ti ubuntu:latest bash run bash on ubuntu (image)

  • -ti: terminal interactive

Container List

docker ps

  • -l latest container,
  • -a all containers,
  • --format=$FORMAT format the out put

Container to Image

docker commit CONTAINER_ID:TAG save the container as an image

  • docker pull
  • docker push

Tag an image

docker tag IMAGE_ID IMAGE_TAG

Ubuntu

docker run -it ubuntu bash

Webserver

  • docker run -d -p 80:80 --name webserver nginx
  • docker stop wevserver
  • docker start webserver

Removal

  • docker rm -f webserver remove the container
  • docker rmi IMAGE_ID (eg. docker rmi nginx)

Docker run

docker run --rm -ti ubuntu sleep 5 exit after 5 seconds

  • --rm remove the main container when the process exist
  • --name name the container

docker run -ti ubuntu bash -C "sleep 3; echo all done"

Docker attach CONTAINER_NAME

attach to an existing container

Detaching: Ctrl+P -> Ctrl+Q

Docker logs CONTAINER_NAME

avoid logging too much data

Docker kill CONTAINER_NAME

stop a running container

Resource Constraints

Memory

docker run --memory maximum-allowed-memory image-name command

CPU

docker run --cpu-shares relative to other containers

Networking

  • docker run -p host-port:container-port/protocol(tcp/udp) port forwarding
  • docker port CONTAINER_NAME

docker run --link CONTAINER_TO_LINK_TO image-name Add container's IP to the host file

Private Network

docker network create NETWORK_NAME

docker run --net=NETWORK_NAME image-name

docker run --link CONTAINER_TO_LINK_TO --net=NETWORK_NAME image-name

Volumes

-v localfolder:container-folder volume share with the host

--volumes-from CONTAINER_NAME shared data between containers but the data will not be saved after all containers stop.

Docker Registries

docker search keyword

Image Building with Dockerfile

docker build -t TAG_NAME DOCKERFILE_LOCATION(.) resulting image will be in the local docker registry

FROM existing-image
ADD file.txt /file.txt
CMD ["nano", "/file.txt"]

FROM

FROM ubuntu:latest

MAINTAINER

MAINTAINER Firstname Lastname email@example.com

RUN

runs the commands through the shell, and save the result

ADD

  • adds local files
    ADD run.sh /run.sh

  • adds the content of tar archieves
    ADD project.tar.gz /install/ it automatically uncompresses tar files

  • works with URLs
    ADD https://project.example.com/download/1.0/project.rpm /project/

ENV

ENV DB_HOST=db.production.example.com

ENTRYPOINT and CMD

ENTRYPOINT specify the command tool to use while its argument can be set with docker run

CMD specify the whole command to run at the start of a comtainer

if argument is set for ENTRYPOINT, CMD will not run

Shell Form

nano notes.txt

Exec Form

["/bin/nano", "notes.txt"]
nor rely on shell, more efficient

EXPOSE

maps a port into the container
EXPOSE 8080

VOLUME

VOLUME ["/host/path/" "/container/path/"]

VOLUME["/shared-data"] // should be avoided

WORKDIR

WORKDIR /install/ sets directory the container starts in

USER

USER usernamesets which user the container will run as

Orchestration

Docker Compose

Built-in

Kubernetes

Containers run programs
Pods Group containers together
Services make pods available to others
Labels are used for very advanced service discovery

kubectl

EC2 Containers Service (ECS)

  • Task denfinitions
  • Tasks
  • Services and exposes it to the Net