Thanks to tools like vagrant, puppet/chef, and Platform as a Service services like Heroku, developers are extremely used to being able to spin up a development environment that is the same every time. What if we could go a step further and make sure our development environment is not only using the same software, but 100% configured and set up like production. Docker will let us do that, and so much more. We'll look at what Docker is, why you should look into using it, and all of the features that developers can take advantage of.
2. What Is Docker?
“Docker is an open platform for developers and sysadmins to build,
ship, and run distributed applications. Consisting of Docker Engine, a
portable, lightweight runtime and packaging tool, and Docker Hub, a
cloud service for sharing applications and automating workflows,
Docker enables apps to be quickly assembled from components and
eliminates the friction between development, QA, and production
environments.”
Sunshine PHP 2017 2
https://www.docker.com/whatisdocker/
13. Running a container
• `docker run` will run a container
• This will not restart an existing container, just create a new one
• docker run [options] IMAGE [command] [arguments]
• [options ]modify the docker process for this container
• IMAGE is the image to use
• [command] is the command to run inside the container
• [arguments] are arguments for the command
Sunshine PHP 2017 13
26. Some Notes
• All three containers are 100% self contained
• Docker containers share common ancestors, but keep their own files
• `docker run` parameters:
• --rm – Destroy a container once it exits
• -d – Run in the background (daemon mode)
• -i – Run in interactive mode
• --name – Give the container a name
• -p [local port]:[container port] – Forward the local port to the container port
Sunshine PHP 2017 26
28. Modifying a running container
• `docker exec` can run a command inside of an existing container
• Use Volumes to share data
Sunshine PHP 2017 28
29. Persistent Data with Volumes
• You can designate a volume with –v
• Create a named volume with `volume create`
• Volumes can be shared amongst containers
• Volumes can mount data from the host system
Sunshine PHP 2017 29
35. Mounting from the host isn’t perfect
• The container now has a window into your host machine
• Permissions can get screwy if you are modifying in the container
• Most things it creates will be root by default, and you probably aren’t root on
the host machine
• Host-mounted volumes are not portable at all
• OSX and Hyper-V VMs have limited pathings to mount
• OSX has poor I/O performance
Sunshine PHP 2017 35
36. Named Data Volumes
• Creates a space that becomes persistent
• Can be mounted anywhere inside your images
• Have our app containers use the data volume to store data
• Use ‘editor containers’ to go in and modify data when needed
Sunshine PHP 2017 36
37. vim Tutorial
• vim is a Modal text editor
• ESC will drop you back to default mode
• :new /opt/webconfig/default to create a new file
• In default mode, i will get us into interactive (edit) mode
• :w to save a file
• :q will quit
Sunshine PHP 2017 37
44. Why go through the hassle?
• Data volumes are portable, depending on the driver
• Data volumes are safer
• Separates the app containers from data
• Production can use a data volume, dev can use a host volume
• Our app containers stay small
• Works directly with other tools
Sunshine PHP 2017 44
46. Networking
• Docker can create multiple network “pools”
• Each container gets an IP address
• Containers can be attached to multiple networks
• Docker network allow service discovery inside networks
Sunshine PHP 2017 46
47. Legacy - Docker Links
• Legacy Links work with `--link`
• Only works on the legacy “bridge” network
• Doesn’t support service discovery
• Not worth it to use anymore
Sunshine PHP 2017 47
48. Docker Networks
• Discreet IP pool for containers
• Containers can be added and removed to the network at whim
• Service discovery though ‘--network-alias’
• Can be set up to work across hosts
Sunshine PHP 2017 48
55. Inspect a container
docker inspect [options] CONTAINER_NAME
• Returns a JSON string with data about the container
• Can also query
• docker inspect -f “{{ .NetworkSettings.IPAddress }}” web_server
• Really handy for scripting out things like reverse proxies
Sunshine PHP 2017 55
56. Work with images
• docker pull IMAGE – Pulls down an image before using
• docker images – Lists all the images that are downloaded
• docker rmi IMAGE – Deletes an image if it’s not being used
Sunshine PHP 2017 56
63. Custom Images
• PHP images are pretty bare
• Lots of times need to install extensions
Sunshine PHP 2017 63
64. Dockerfile
• Dockerfile is the configuration steps for an image
• Can be created from scratch, or based on another image
• Allows you to add files, create default volumes, ports, etc
• Can be used privately or pushed to Docker Hub
Sunshine PHP 2017 64
66. Build it
docker build -t tag_name ./
• This runs through the Dockerfile and generates the image
• We can now use the tag name to run the image
Sunshine PHP 2017 66
82. What is Docker Compose?
• Multi-container orchestration
• A single config file holds all of your container info
• Works with Docker Swarm and a few other tools, like Rancher
Sunshine PHP 2017 82