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

Managing Docker Containers

Managing Docker Containers

Avatar for Loh Siu Yin

Loh Siu Yin

August 22, 2014

Other Decks in Technology

Transcript

  1. Managing Docker Containers creating, killing, restarting and removing containers 27

    August 2014 Loh Siu Yin Technology Consultant, Beyond Broadcast LLP 1 of 19
  2. Why Docker? Docker is a good way to deploy a

    12 factor app. The twelve-factor app is a methodology for building software-as-a-service apps that: Use declarative formats for setup automation, to minimize time and cost for new developers joining the project; [dockerfile] Have a clean contract with the underlying operating system, offering maximum portability between execution environments; [container] Are suitable for deployment on modern cloud platforms, obviating the need for servers and systems administration; (yes) Minimize divergence between development and production, enabling continuous deployment for maximum agility; (yes) And can scale up without significant changes to tooling, architecture, or development practices. (yes) 12factor.net (http://12factor.net) 2 of 19
  3. Docker Docker allows you to package your application into a

    Docker container. This container can then be run on any Linux host running kernel 3.8 or above (RHEL 6.5 with kernel 2.6.32-431 is also supported). docker.com gives instructions for installing Docker on a variety of platforms. To get help, run the docker command without arguments docker.com (http://docker.com) 3 of 19
  4. Script to create and run a Docker container --name ger

    names the container to be created ger -d daemonize -e defines an environment variable to be applied to the container. There is also --env-file if you have lots of variables. ubuntu is the name of a Docker image from which the container will be created #!/bin/bash echo "Launching docker container named: ger" docker run --name ger -d \ -e DEEP='blue/green algae' \ -e GER=bau \ ubuntu /bin/bash -c 'while true; do \ echo $DEEP; \ echo $GER; \ echo `date`; \ sleep 3;\ done' Run 4 of 19
  5. Viewing a container's log re-run script with -t Docker captures

    your app's stdout and stderr into a json formatted log on the Docker host at /var/lib/docker/containers/{container_id} docker logs reads the json format log file in the container's folder on the host and presents it. The log files on the host are not automatically rotated. Setup logrotate on the Docker host to achieve this. #!/bin/bash echo "attaching to ger" docker logs ger Run 5 of 19
  6. Docker Image v. Container A Docker image is a software

    build. Say you have an SMTP server docker image named "postfix". This image is the executable file that can run on any Docker host. --- A Docker container is a deployment (configuration + build). Say you want an email server at example.com. So we take the postfix image and bind it to a configuration (the address: example.com). However it does not become a Docker container until you run it, creating the container from the image in the process. You now have example.com's email server running in a Docker container. When you stop the container you stop example.com's email server. 7 of 19
  7. About Docker images You download or pull images from hub.docker.com

    (think of it as github for Docker images). You can also run your own image registry. Docker registry is Open Source. You build your own Docker image for your application from a dockerfile you write or by commit ing a container you've created. push your Docker image to the Docker registry. If you have previously pushed an earlier version of your image, only the changes are pushed. Again think of applying a diff to your github repository. Similarly if you have pulled an ealier version of the image, updating the image involves downloading only the changed bits. docs.docker.com (http://docs.docker.com) 8 of 19
  8. Commit a container run to an image #!/bin/bash echo "committing

    ger to new image siuyin/ger:20140827" docker commit ger siuyin/ger:20140827 Run 9 of 19
  9. Running the image #!/bin/sh echo "creating and running container: ger1"

    docker run --name ger1 -d \ -e DEEP=purple \ -e GER=bear \ siuyin/ger:20140827 Run 10 of 19
  10. But first wdotweb wdotweb is a web application that provides

    a workflow charting service over a user-configured http port. docker ps to show exposed ports #!/bin/bash docker run --name wdot -d \ -p 3322:22 \ -p 8288:8188 \ siuyin/wdotweb:20140820 Run 12 of 19
  11. Listing Docker containers first run the above script without -a

    CONTAINER ID is a unique id for each Docker container. Docker commands to manage a container require either an ID or name to be specified. It is usually sufficient to refer to the container with the first 3 or 4 letters of the ID. IMAGE is the Docker image the container was built from NAMES is the human friendly name you can use to refer to the container rather than using the ID. #!/bin/bash # list all docker processes (containers) including stopped ones: -a docker ps Run 13 of 19
  12. Stopping a container docker stop: Stop a running container (send

    SIGTERM, and then SIGKILL after grace period default 10s, specified 3s above) The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. Useful feature: The docker daemon on the Docker host will restart containers that were running when the host is restarted. #!/bin/bash echo "stopping ger" docker stop -t 3 ger Run 14 of 19
  13. Killing a container docker kill: Kill a running container (send

    SIGKILL, or specified signal) [...] The main process inside the container will be sent SIGKILL, or any signal specified with option --signal. #!/bin/bash echo "killing ger" docker kill ger Run 15 of 19
  14. Restarting a stopped or killed container If you run docker

    ps now you will see the container ger running. docker start is idempotent. It is OK to start a container that has already been started. #!/bin/sh echo "starting ger" docker start ger Run 16 of 19
  15. Removing a container docker rm deletes the container and all

    its associated files. After rm, the container named ger is deleted and a newly created container can reuse the name ger. #!/bin/sh echo "removing ger" docker rm ger Run 17 of 19