Docker Mastery

Posted on Posted in Java, Technologies

Below are more detailed notes on various aspects of Docker, and somewhat more advanced topics with dealing with container/image management.

Docker commands are now separated into main modules. previously when you wanted to run a command, you would do docker <command>

In the newer version you can say:

docker <module> <module_command>

Modules

  • config – Manage Docker configs
  • container – Manage containers
  • image – Manage images
  • network – Manage networks
  • node – Manage Swarm nodes
  • plugin – Manage plugins
  • secret – Manage Docker secrets
  • service – Manage services
  • stack – Manage Docker stacks
  • swarm – Manage Swarm
  • system – Manage Docker
  • volume – Manage volumes

Run vs Start

  • run – run will always create a new container
  • start – will start an existing one

Images vs Container

  • Images are template that are collected from dockerhub
  • container is the running instance
    • you can have multiple containers based on one image

Note:

you can terminate docker by just specifying of container id for example our container id is 1234567, you can stop the container by: docker container stop 123

Common Docker local ip is usually: 192.168.99.100

Performance Monitoring: docker container stats

SSH inside a server  would be:

  • docker exec -it nginx
  • docker container exec -it <container_name> bash/sh/etc

-i : interactive

-t : pseudo TTY

linux alpine – small secure linux box

Networking

Common commands:

  • docker network ls
  • docker network create
  • docker network connect

You can link your containers to a specific network via the flag: –network <network_name>

link container via network:
docker container run -d –net <network_name> –net-alias <network-alias> <container>

TAGGING and PUSHING, CACHING LAYERS

Logging – docker already handles the logs, you just need to make sure it goes to stdout or stderr, current trend is leaning to stdout rather than log4j or any other logging frameworks

Dockerfile Essentials Format

FROM <IMAGE>
WORKDIR <Directory> – THIS IS a CD or Change Directory Command
ENV <environment>
RUN <RUN COMMANDS>
EXPOSE <EXPOSE PORT>
CMD <RUN COMMAND EVERYTIME CONTAINER IS LAUNCHED>
COPY <from> <to> – copy local to docker

Image building:
docker image build -t <tag> <location>
NOTE: that building the image does not mean you are running it, container and image are totally different

Named volume format:
-v <volume_name>:<container_volume>

BIND MOUNT (CANT USE IN DOCKERFILE)
-v full mount : <container_volume>

Notes/Gotchas:

  • VOLUMES on windows docker toolbox can only be accessible through c:\users via /c/users/
  • IF YOU HAVE AN EXISTING IMAGE, you will need to rebuild that image, know that -volumes does not work on dockerfile
  • in contrast if you want to just run a DockerFile you would do docker build

docker-compose up – to build images for development
docker-compose build or up –build – to rebuild images, only given that you have a build tag in your yml file

Docker Swarm

  • docker swarm init – creates raft database of root ca,configs secrets – PKI, security automation
  • docker node ls – check nodes
  • docker service create – create a service part of the swarm
  • docker service ls – list of services
  • docker service update – update existing service with regards to orchestration
  • docker service rm heuristic_shirley – to actually delete service from docker service

CREATING RANCHER MACHINE:
docker-machine create dockermachine –driver virtualbox –virtualbox-cpu-count “-1” –virtualbox-disk-size “8000” –virtualbox-memory “1024” –virtualbox-boot2docker-url=https://github.com/boot2docker/boot2docker/releases/download/v1.10.3/boot2docker.iso

docker-machine create dockermachine –driver virtualbox –virtualbox-cpu-count “-1” –virtualbox-disk-size “8000” –virtualbox-memory “1024”