Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker: An Introduction

Docker: An Introduction

A brief overview of how to set Docker up as a viable alternative to a WAMP installation

Avatar for Andrew Millington

Andrew Millington

November 04, 2016

More Decks by Andrew Millington

Other Decks in Programming

Transcript

  1. A SMALL HISTORY LESSON Explosion of popularity for virtualisation in

    server rooms around 2008 Vagrant created as a way to maintain virtual machines
  2. VAGRANT: THE GOOD Single interface to create, download, maintain, destroy

    virtual machines vagrant init - requires a Vagrantfile vagrant up vagrant ssh vagrant destroy Wrapper around existing mature technology e.g. VirtualBox VAGRANT: THE BAD Spinning up one, two, three or four machines inside your computer is expensive
  3. CONTAINERS TO THE RESCUE Containers are not new chroot introduced

    in 1982 Change the root directory of a process so it doesn't see anything outside
  4. LXC OR LINUX CONTAINERS Chroot alone can only restrict access

    to files. What if we could restrict access to computer resources such as processes or memory? FreeBSD had BSD Jail Solaris launched Solaris Zones in 2004 Linux created LXC in 2008 with Parallels, Google and IBM among others Windows integrated a Linux layer 2016
  5. dotCloud, a hosting company, released Docker in 2013 Did for

    containers what Vagrant did for VMs Consists of a client and server
  6. WHY SHOULD I CARE? Useful for developers as environment is

    identical Vagrant provisions box on first boot. Can cause issues... Small containers running single processes Development environment becomes modular
  7. DOWNLOAD DOCKER FOR WINDOWS Two options available Docker Toolbox Docker

    Native Recommend to use Docker Toolbox for the timebeing
  8. DOCKER TOOLBOX A suite of tools for running Docker in

    Windows or Mac OSX Docker Engine Compose Machine Kitematic
  9. RUNNING OUR FIRST CONTAINER Open up the Docker Quickstart Terminal

    and write the following command docker run ­ti ­­rm ubuntu /bin/bash A command terminal will be opened for us within our Ubuntu container
  10. CREATING A DOCKER CONTAINER Try to run only one process

    per container. For our development environment we need: Apache and PHP MariaDB Will add Node.js and Redis at a later date
  11. CREATING A DOCKER CONTAINER First we must create a simple

    Dockerfile for our Apache container # Version: 0.0.1 FROM: centos:7 MAINTAINER: Andrew Millington "[email protected]" RUN yum update ­y && yum install httpd ­y RUN yum install php EXPOSE 80 Each instruction adds a new layer to the container and commits it
  12. HOW A CONTAINER IMAGE IS CREATED 1. Docker runs container

    from image 2. Instruction executes making change to container 3. Docker commit runs to commit new layer 4. Docker runs a new container from this image 5. Next instruction executes. Process repeats
  13. BUILD THE DOCKER IMAGE docker build ­t="sephster/docker­php56" . To view

    the image you just created use the docker images command
  14. RUN OUR FIRST CONTAINER docker run ­d ­p 80:80 ­­name

    "php56" sephster/docker­php56 Note that we still have to expose our port when running the container. docker ps
  15. GET OUR CONTAINERS TO COMMUNICATE docker run ­­name mariadb ­e

    MYSQL_ROOT_PASSWORD=my­secret­pw ­­link php56: Now we have a fully functioning web server we can access at http://192.168.99.1/ (usually)
  16. OTHER USEFUL DOCKERFILE COMMANDS CMD - The command to run

    when the docker container is launched ENTRYPOINT - Pass arguments to the command specified in ENTRYPOINT ENTRYPOINT ["usr/sbin/nginx"] docker run ­p 80:80 ­­name nginx nginx ­g "daemon off;"
  17. OTHER USEFUL DOCKERFILE COMMANDS WORKDIR - Set the working directory

    of the process ENV - Set an environment variable during the build process ENV MYSQL_ROOT_PASSWORD my­secret­pw RUN mysqld Please check the Docker website for further examples
  18. GETTING OUR FILES INSIDE OUR CONTAINER docker exec ­ti php56

    /bin/bash Problem: We will lose all our changes as soon as the container is deleted
  19. SOLUTION: VOLUMES Can be shared and reused between containers Container

    doesn't have to be running to share volumes Changes to a volume are made directly Changes to a volume will not be included when you update an image Volumes persist until no container uses them # Create a mount point to any container created from the image VOLUME ["opt/project"]
  20. MOUNTING A HOST DIRECTORY AS A VOLUME It would be

    good if we could get our Mercurial/Git files on our host machine to be hosted within our container. Docker makes this easy. docker run ­d ­p 80:80 ­­name "php56" ­v C:/Users/s03am2/Documents/Mercurial_
  21. SHARING IMAGES ON DOCKERHUB docker login docker push sephster/docker­php56 Dockerhub

    also allows you to set up commit hooks for automatic builds
  22. USEFUL DOCKER-COMPOSE COMMANDS docker-compose up Builds, [re]creates, starts and attaches

    containers docker-compose start Starts existing containers docker-compose stop Stops running containers