DevOps/Docker

From Wikiversity
< DevOps(Redirected from Docker)
Jump to navigation Jump to search

Docker is a software available since 2013 that among other features performs operating-system-level virtualization also known as containerization

Features[edit]

Docker offer the following features:

Installing Docker[edit]

You can read official Docker documentation about installing Docker on your system: https://docs.docker.com/install/.

On macOS you can follow official documentation https://docs.docker.com/docker-for-mac/install/ which requires to create and account to download installer. You can also try: brew cask install docker or try to follow https://pilsniak.com/how-to-install-docker-on-mac-os-using-brew/ instructions:

brew install docker docker-compose docker-machine(to install docker and docker-compose)
brew cask install virtualbox
docker-machine create --driver virtualbox default
docker-machine ls
docker-machine env default
eval $(docker-machine env default)
docker run hello-world (will pull hello-world image and run it on docker)

On Ubuntu: snap install docker

Binaries[edit]

Following binaries will be installed: docker-init, docker-proxy, docker and dockerd

Verifying Docker installation[edit]

Once installed the Docker daemon, called "dockerd" should be running. You can also run docker run hello-world to verify docker correct installation.

Configuration files[edit]

  • Ubuntu: /etc/docker/daemon.json

Docker Releases (Docker Engine release notes)[edit]

Use: docker version[1] to check your version. You can download Docker CE source code from GitHub[2].

Docker Command Line[edit]

You can read official docker command line documentation in https://docs.docker.com/engine/reference/commandline/docker/. Before being able to run docker commands you will have to install Docker on your machine.

Some typical task using containers:

Docker verification commands[edit]

  • Verify correct installation: docker run hello-world

You will see some message similar to this one:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

Docker image and container creation[edit]

  • Create a new docker image base on alpine linux distribution and login into the new container: docker run -it alpine sh
  • Create a new docker image: docker build
    • Create a new docker image by creating a new text file with instructions, usually called Dockerfile[3]: docker build -f /path/to/a/Dockerfile .
  • Create a container: docker create
  • Generate a mediawiki:1.27 image: docker build --tag mediawiki:1.27 . (you can use -t or --tag)
  • Execute or run a container based on mediawiki:1.27 image: docker run --name wikiFGA -p 0.0.0.0:9090:80 -d mediawiki:1.27

Docker Container Operation[edit]

  • Start an existing container: docker start CONTAINER_ID OR CONTAINER_NAME. See also docker run and differences[4].
  • Stop an running container: docker stop container_id
  • Show only running containers:
  • Login/Connect into a running container:
    • docker exec[9] -it <my_container_name> bash
    • docker exec -it <my_container_name> sh
  • Start stopped containers: docker start $(docker ps -a -q -f status=exited)
  • Restart all containers: docker restart $(docker ps -aq) previously docker restart $(docker ps -q)-a all -q quiet[10]

Start configuration behavior and restart policy[11][edit]

To configure the start configuration behavior or the restart policy for a container (both of them), use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

  • Do not automatically restart the container. (the default): no
  • Restart the container if it exits due to an error, which manifests as a non-zero exit code: on-failure
  • Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted: unless-stopped
  • Always restart the container if it stops: always


The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted:

$ docker run -dit --restart unless-stopped redis With docker inspect CONTAINER[12] you can verify RestartPolicy configuration.

To update restart policy execute:

docker update[13] --restart=always CONTAINER_ID|CONTAINER_NAME[14]
If the container is started with “--rm” flag, you cannot update the restart policy for it. The AutoRemove and RestartPolicy are mutually exclusive for the container.

See also: docker compose and docker stack

Docker images management[edit]

  • List created images: docker images[15], docker image ls (both commands seems to perform the same action, note it is image instead of images)
Images are stored in docker info | grep "Docker Root Dir"[16]
  • docker image rm[17]. See also: docker rm CONTAINER

Docker Network command line commands[edit]

  • docker network ls
  • docker network inspect

Docker resource limitation[edit]

Docker: Working with remote repositories/registries[edit]

You will be using mainly the following commands docker login, docker logout, docker pull and docker push. Docker registry allow to configure notifications. [18]. Docker has a public repository called Docker Hub and cloud providers offer repositories services such as AWS Elastic Container Registry (ECR).

Docker Information[edit]

Docker Operation[edit]

Docker/cgroup Performance information[edit]

Docker Swarm[edit]

Maintenance[edit]

  • docker system prune -a remove unused and dangling images. Therefore any images being used in a container, whether they have been exited or currently running, will NOT be affected.[22]

Volumes[edit]

Docker logging[edit]

Docker support logging to format or different platforms, such as, json-file, syslog, journald, gelf, fluentd, awslogs, splunk, etwlogs, gcplogs and logentries.[24]

docker logs CONTAINER_NAME_OR_ID (Docker Community Engine only support: local, json-file and journald)
docker logs CONTAINER_NAME_OR_ID 2>&1 | grep "STRING_TO_SEARCH" (You will need to redirect outputs to be able to grep output)[25]
See https://docs.docker.com/config/containers/logging/ for more information

Misc[edit]

Activities[edit]

  1. Read docker blog: https://blog.docker.com/
  2. Read Docker Engine release notes (2017) and Docker CE or Docker EE releases.
  3. Understand the difference between an image and a container, Docker Images vs. Containers: https://stackoverflow.com/a/26960888
  4. Understand the difference between docker start and docker run: https://stackoverflow.com/questions/34782678/difference-between-running-and-starting-a-docker-container. See also runC
  5. Read Stackoverflow questions related to docker: https://stackoverflow.com/questions/tagged/docker?tab=Votes

See also[edit]

References[edit]

  1. https://docs.docker.com/engine/reference/commandline/version/
  2. https://github.com/docker/docker-ce/releases
  3. https://docs.docker.com/engine/reference/builder/
  4. https://stackoverflow.com/questions/34782678/difference-between-running-and-starting-a-docker-container
  5. https://docs.docker.com/engine/reference/commandline/container_ls/
  6. https://docs.docker.com/engine/reference/commandline/ps/
  7. https://www.docker.com/blog/whats-new-in-docker-1-13/
  8. https://stackoverflow.com/a/45254760
  9. https://docs.docker.com/engine/reference/commandline/exec/
  10. https://stackoverflow.com/questions/38221463/command-for-restarting-all-running-docker-containers
  11. https://docs.docker.com/config/containers/start-containers-automatically/#use-a-restart-policy
  12. https://stackoverflow.com/questions/43108227/is-it-possible-to-show-the-restart-policy-of-a-running-docker-container
  13. https://docs.docker.com/engine/reference/commandline/update/
  14. docker update --restart=always 90bf248337dc
  15. https://docs.docker.com/engine/reference/commandline/images/
  16. https://stackoverflow.com/questions/19234831/where-are-docker-images-stored-on-the-host-machine
  17. https://docs.docker.com/engine/reference/commandline/image_rm/
  18. https://docs.docker.com/registry/notifications/
  19. http://manpages.ubuntu.com/manpages/disco/man1/docker-inspect.1.html
  20. https://docs.docker.com/engine/reference/commandline/image_inspect/
  21. <https://docs.docker.com/engine/reference/commandline/swarm/
  22. https://stackoverflow.com/a/45143234
  23. https://stackoverflow.com/questions/30133664/how-do-you-list-volumes-in-docker-containers
  24. https://docs.docker.com/config/containers/logging/configure/
  25. https://stackoverflow.com/questions/34724980/finding-a-string-in-docker-logs-of-container
  26. https://docs.docker.com/engine/reference/commandline/tag/