210 likes | 258 Views
Getting Started With Docker Containers. Who the f*** is this guy?. Snr. Software Engineer at OYE Network Previous work experience includes: Technical trainer New Horizons (South Africa) GoBLAM! / RightShift (now Derivco) Floh! Media, (UK) West One Music Group (UK)
E N D
Who the f*** is this guy? • Snr. Software Engineer at OYE Network • Previous work experience includes: • Technical trainer New Horizons (South Africa) • GoBLAM! / RightShift (now Derivco) • Floh! Media, (UK) • West One Music Group (UK) • Universal Music (charity work) • Ridango AS (Estonia) • I’m passionate about..
What are these containers of which you speak? From Docker docs themselves:“A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.” • https://www.docker.com/resources/what-container
Some history.. • Locally installed system dependencies (Nginx, Apache, PHP, MariaDB, etc) • More bloat, more installs.. What happens when one project needs PHP 5.6 and another 7.3? • Just got a new machine.. Have fun setting it all up again.. • Virtual Machines • Nice.. but let’s be honest.. The build time is insane. Each minor change requires an exhaustive rebuild. Might as well grab 2 cups of coffee.. Maybe 3 • Docker • Quick, easy, portable and fast!
So, what’s in it for me? Docker as a development environment offers YOU the following benefits: • Reduced time onboarding new admins + devs • Reduced setup time creating new environments • Build once, if it works.. It works EVERYWHERE! • Ease of portability • Moola saved because the sooner you’re up.. The sooner you can be constructive :)
Docker Overview • An open platform for developing, shipping, and running applications. • Separate your applications from your infrastructure. • Deliver software quickly. • You can manage your infrastructure in the same ways you manage your applications. • Reduce the delay between writing code and running it in production.
Docker Engine Docker Engine is a client-server application with these major components: • A server which is a type of long-running program called a daemon process (the dockerd command). • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do. • A command line interface (CLI) client (the docker command).
What Can Docker Be Used For? Fast, consistent delivery of your applications Streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide your applications and services. Responsive deployment and scaling The platform allows for highly portable workloads. Containers can run on a developer’s local laptop, on physical or virtual machines in a data center, on cloud providers, or in a mixture of environments. Running more workloads on the same hardware It’s lightweight and fast. It provides a viable, cost-effective alternative to hypervisor-based virtual machines, so you can use more of your compute capacity to achieve your business goals.
The Architecture Docker is based on a client-server environment comprised of the following components: • Docker DaemonListens for Docker API requests and manages Docker objects. • The Docker ClientThe primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. • Docker registriesA Docker registry stores Docker images. Examples include: • Docker Hub • Docker Trusted Registry (DTR) • Private Registry • Docker objectsWhen you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects.
Docker Objects IMAGES An image is a read-only template with instructions for creating a Docker container. • Often, an image is based on another image, with some additional customization. • You can create your own or use one already available on a registry. • Images are created using a Dockerfile which uses a simple syntax for defining the steps needed to create the image and run it. • Each instruction in a Dockerfile creates a layer in the image. • Only the layers which have changed are rebuilt.
Docker Objects CONTAINERS A container is a runnable instance of an image. • You can create, start, stop, move, or delete a container using the Docker API or CLI. • You can connect a container to one or more networks and attach storage to it. • Containers are relatively well isolated from other containers and the host, however you can control the isolation level. • A container is defined by its image as well as any configuration options you provide to it when you create or start it. • When a container is removed, any changes to its state that are not stored in persistent storage disappear.
Docker Objects NETWORKS • Allows containers to communicate with each other and the outside world via the host machine, there has to be a layer of networking involved. • Docker supports different types of networks, each fit for certain use cases. The simplest and easiest to use is the bridge network. • Networks allow developers to: • EXPOSE ports: Which opens ports for communication between containers • PUBLISH ports: Which opens ports for communication from outside the internal network (EG: Allowing for XDebug connections in PHP) • Docker networks will work out all the internals such as iptables, routing, etc,
Managing Data By default all files created inside a container are stored on a writable container layer. This means that: • The data doesn’t persist when that container no longer exists. • A container’s writable layer is tightly coupled to the host machine where the container is running. • Writing into a container’s writable layer reduces performance as compared to using data volumes, which write directly to the host filesystem. Developers have the following primary options for managing data: • Volumes:Are stored in a part of the host filesystem which is managed by Docker. Volumes are the best way to persist data in Docker. • Bind Mounts:May be stored anywhere on the host system. Simply put, they make files or directories on the host available within the container.
Dockerfile • Dockerfiles are used to build up Docker images. • They use a simple syntax (similar to shell scripting.. At least to me) to create the various layers that make up an image. • Dockerfiles are based on instructions# This is a commentINSTRUCTION code to run.. • Instructions are parsed line by line. • Instructions can add new layers to an image • Layers from a Dockerfile can be cached, drastically speeding up future build time. • https://docs.docker.com/engine/reference/builder/
Dockerfile Best Practices • Create “ephemeral” containers. • Where possible use a pre-built base image rather than creating your own. • Make use of .dockerignore file(s) • Include ONLY what you need • Leverage multi-stage builds! They ROCK! • Where possible use Alpine builds (if available) • Be aware of the affects changes will have on your layers and the build process • Order your layers to leverage the build-cache • Reduce the number of layers where possible • Order multi-line statements
Docker Compose • Syntactic sugar over the general Docker CLI commands • Defined as a docker-compose.yml file (YUP! It’s YAML) • Simplifies the development of containers by allowing you to define: • Networks • Volumes • Bind mounts • Services (actually containers) • Build • Can use .env files to provide default environment variables • NOTE! The syntax DOES change pretty fast as new features are added and older ones deprecated. • https://docs.docker.com/compose/compose-file/
Environment Files • Allow developers to provide dynamic environment variables to containers for both build and execution. • Follow a fairly simple syntax# DeclarationPHP_SYMFONY_VERSION_NUMBER=4.2# Simple example of usageRUN composer require symfony/symfony:${PHP_SYMFONY_VERSION_NUMBER} --prefer-dist • .env files should NEVER be committed to source control! Rather create a template (.env.dist) that can be copied and used instead
Useful Links The following are a series of blog posts and docs that have helped me IMMENSELY on my Docker journey: • https://codefresh.io/docker-tutorial/hello-whale-docker-basics-getting-started-docker/ • https://medium.com/the-code-review/top-10-docker-commands-you-cant-live-without-54fb6377f481 • https://docker-curriculum.com/ • https://blog.codeship.com/orchestrate-containers-for-development-with-docker-compose/ • https://medium.com/factualopinions/docker-compose-tricks-and-best-practices-5e7e43eba8eb • https://medium.com/@innossh/docker-compose-best-practice-73746ac3f13a • https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ • https://medium.com/@nagarwal/best-practices-for-working-with-dockerfiles-fb2d22b78186 • https://snyk.io/blog/10-docker-image-security-best-practices/ • https://docs.docker.com/develop/develop-images/multistage-build/ • https://dev.to/brpaz/using-docker-multi-stage-builds-during-development-35bc • https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae • https://nickjanetakis.com/blog/docker-tip-31-how-to-remove-dangling-docker-images • https://medium.com/the-code-review/clean-out-your-docker-images-containers-and-volumes-with-single-commands-b8e38253c271 And of course the official docs for both Dockerfile + Docker Compose in the previous slides :)
Git Repositories Due to time constraints (and general human error on my part) I’ve created some repositories to help with the demonstration process. • https://github.com/jasonplamb/wordpress-demo (General demo minus .env file examples) • https://github.com/jasonplamb/wordpress-demo-env (Extension of wordpress-demo with .env file example)