IngmarBoddington
11/23/2016 - 4:27 PM

Docker

Docker / Dockerfile notes

TERMINOLOGY
-----------
Images - The file system and configuration of our application which are used to create containers
	Dockerfile describes an image
	Image identifiers can be repo:tag or ID
Containers - Running instances of Docker images — containers run the actual applications. A container includes an application and all of its dependencies. It shares the kernel with other containers, and runs as an isolated process in user space on the host OS. You created a container using docker run which you did using the alpine image that you downloaded. A list of running containers can be seen using the docker ps command.
	Image ID != Container ID
	When containers are recreated they will have lost any changes made since last created
Docker daemon - The background service running on the host that manages building, running and distributing Docker containers.
Docker client - The command line tool that allows the user to interact with the Docker daemon.
Docker Store - A registry of Docker images, where you can find trusted and enterprise ready containers, plugins, and Docker editions. You'll be using this later in this tutorial.
Volumes - Virtual Discs
	Persistent or Ephemeral
	Not part of the image, local to the host
	How to create
		Add a -v option to run command
	Always host over container file system mount


GENERAL
-------
Try and use less steps and remove unwanted files in each step of a docker image build
Each step of a build is a read-only layer onto of the last
tty may keep container alive when using docker-compose
Disk limits can be expanded in the Docker Desktop UI!
If a name is not provided to build or run commands, one will be autogenerated for you

		
COMMANDS
--------
docker attach <name>
	Get into running container (started with run)
	
docker build [--no-cache] -t <name> <location of docker file>
  Build image using Dockerfile in current directory
  --no-cache prevents images being reused in the build process where the build has not changed since last run

docker commit <container_id/name> [<new_name>]
	Create a new image from the current container state
	Set the name here to avoid need for separate tag call
	Adds tags using name (can use name:version format)
	
docker exec <container> <command>
	exec runs on a current container	
	-it for interactive / tty
	docker exec -it test5 /bin/as

docker image rm <image_id>
	Delete an image
	
docker images
	View local images, including those which have been built using compose
	
docker inspect <image>
	Display image details / meta-data

docker load ...
	Load images from local file
	
docker login
	Login to docker hub (not required when using Docker Desktop)
	
docker logs [-f] <container>
  Get output logs for container
  (use if to follow / tail)

docker network connect <network> <container>
	Attache a network to a container
	
docker network create <name>
	Create a network
	
docker network ls
	List current networks
	(Defaults are Bridge, Host and None for internal only, same as host and none) Host is default

docker pull <image>
docker push <image>
	Push or pull an image to Docker Hub
	
docker port <name>
	Show exposed port details for named container

docker ps
	Show current containers
	
docker ps -a
	Show current / recent containers	
	
docker rm [-f] <container>
	Remove containter
	Use -f to shutdown before removal
	
docker rmi <image>
	Remove image
	
docker run [<options>] <image> <command>
	run starts a new container, the container will stop when this process stops (even if more are started)
	-ti for interactive / terminal features to be included in produced container (use if going to use shell in container)
	--name <name> for naming the container for ease of reuse
	-rm to remove container after use
	-d to detatch / deamonize
	-v local:remote to share a directory as volume
	-v remote to create volume in container only (can be shared between volumes)
	--memory <max> to limit memory
	-cpu-shares <num> amount of proportional cpu to use
	-cpu-quota <num> amount of cpu to use
	--net <network> to set a network (host to remove protection)
	--volumes-from <name> shared volumes from another container
	--privileged=true to allow container to have control over host machine
	-e <var> set an env var
	(pretty much all the things you can put in a Dockerfile)
	--link <name> legacy one way connection for sharing env variables from target
	--restart=always to always restart container on exit
	
	docker run hello-world
		downloads and sets up a container and runs a simple hello world script from the container (the entry point)

	docker run alpine /bin/echo "Hello World"
		downloads alpine image and runs echo command
		Can also just use echo
	
	docker run --name static-site -e AUTHOR="Your Name" -d -P dockersamples/static-site
		-P will publish all the exposed container ports to random ports on the Docker host
		-e is how you pass environment variables to the container
		--name allows you to specify a container name
		AUTHOR is the environment variable name and Your Name is the value that you can pass
	
	docker run --name static-site-2 -e AUTHOR="Your Name" -d -p 8888:80 dockersamples/static-site
		Like above but with explicit exposed port mapping

	docker run -d --name test5 alpine tail -F /dev/null
		Run with name, as deamon (keeps running due to command)
	
	docker run -d --name webapp -v "$PWD":/var/www/html -p 8080:80 --storage-opts dm.basesize=20G php:5.6-apache
		Create a webapp container, share current die into specified, specify port and image
		Also sets initial disk size to 20g
	
	docker run -t -d -p 8090:8090 -p 9080:9080 -p 9191:9191 ingboss/sandbox:installed sh -c 'cd /usr/local/appdynamics/platform/platform-admin && ./bin/platform-admin.sh start-platform-admin && tail -f /dev/null'
		(Sigh)
		cd and startup enterprise console
		Example of using tail -f /dev/null to keep a container running after exit of startup script!
	
docker kill <container>
	Stop a container
	Container does not lose state unless actually removed

docker save ...
	Save images to local file
	
docker search <search>
	Search for an image
	
docker system df
	View disk usage information
	
docker system prune
	Cleanup all the things (not associated with a running container)
	
docker tag <image_id> <name>
	Set a repo / tag for an image
	
	docker tag ingboss/silly:v1
		Set repo = ingboss, name = silly and version = v1

DOCKERFILE
----------
https://docs.docker.com/engine/reference/builder/
Named simply 'Dockerfile'
Defines an image (like a recipe)
Note that each instruction is run independently, and causes a new image to be created - so RUN cd /tmp will not have any effect on the next instructions.
Comments must be o their own line....

FROM #The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.
RUN <cmd> #Run a command inside the container during build, saves the result as new image
COPY <source> <destination> #Copy resources from <source> relative to directory and <destination> on the created image, local files only
ADD <source> <destination> #Same as COPY but works for URLs and tar files
EXPOSE <port> #Define port to expose externally (this actually does nothing, need to use -p option in run command to publish ports). Default is TCP (use <port>/udp otherwise)
MAINTAINER <author> #Deprecated - use labels instead
CMD ["<cmd>"[,<args>...]] #command to run when container starts - can avoid need for command in a run command using this
CMD "<cmd>"[,<args>...] #command to run when container starts - can avoid need for command in a run command using this
ENTRYPOINT <command> #Sets start of command (so run args will be passed to this command)
LABEL <key=value> #Define meta-data, can have many lines or comma sep on one line
ENV <key> <value> #Set an ENVVAR (persists to lines further in Dockerfile and in created image)
VOLUME ["<dir>"] #Internal volume
VOLUME ["<dir>" "<dir>"] #set local dir based volume
USER <user> #Which user for commands to be run with in the container

#Create an and then reference in multi-project file
#For example for creating a large builder image but small product image
FROM <image> as <name>
...
FROM <image>
COPY --from <name> <stuff>

COMPOSE
-------
For orchestrating multiple containers

docker-compose up
  Start up based on compose file in current directory

COMPOSE EXAMPLE	
docker-compose.yml

	version: '2'
	networks:
	  my-network:
		driver: bridge

	services:
	  php:
		build:
		  context: .
		  dockerfile: php.dockerfile
		container_name: php
		networks:
		  - my-network
		tty: true
      volumes:
        - /Volumes/git:/code		
		
php.dockerfile	
	
	FROM php:5.6-alpine

	RUN apk update
	RUN apk add vim
	RUN apk add bash
	RUN apk add git
	
	COPY /path/to/composer.json /tools/composer.json

	WORKDIR /root
	
IMAGES - Public Repo = https://hub.docker.com/