Header left.png

Docker

From Systems Group
Jump to: navigation, search

Docker Basics (410)

What is Docker?

Docker is a container system available for all major operating systems. Docker facilitates the creation and management of Linux containers. A container is an isolated process that runs on the kernel of the host operating system, but does not necessarily share its resources directly. Containers can be packaged as "images", which contain the container's filesystem. This allows containers to be portable, in that they contain an application and all of its dependencies. This has many advantages, including ease of setup, easier version control, and avoiding the 'version purgatory' that can occur when multiple applications depend on different versions of the same software (i.e. python 2 vs python 3).

Images

A docker image contains the filesystem for a container. This typically includes a basic Linux filesystem, any applications running in the container, and configuration files. Images are built using a Dockerfile. A dockerfile defines images in layers. Each layer is build sequentially from the layer before it. Typically, dockerfiles start with a base image, and add necessary files to the container, then run any necessary setup commands. The entrypoint and startup command for the container can also be defined. There are many directives that can be specified in a dockerfile, and further documentation is available at https://docs.docker.com/engine/reference/builder/. An example dockerfile can be found below.

FROM php:7.2-apache #base image ADD ./samplesite /var/www/html #copy files into the container's /var/www/html COPY ./startup.sh /startup.sh #another method to copy, this copys a startup script RUN docker-php-ext-enable ldap #runs a command inside the container CMD /startup.sh #sets the container's startup command

Dockerfiles can be built by running "docker build -t <name> ." in the directory containing the dockerfile. By default images are stored locally, however they can also be pushed to a registry, where they can then be pulled on demand from any other machine with access to that repository. For more information about registries, visit this page https://docs.docker.com/docker-hub/.

Docker Compose

Docker Compose is a YAML based wrapper for docker commands that allows containers to be created and managed in a declarative manner. Services are described in a docker-compose.yml file, including the image, exposed ports, volumes, networks, and any other settings. Services described in a docker-compose file can be started and stopped by running docker-compose up or docker-compose down. For docker-compose up, the -d flag detaches the containers from STDOUT, allowing them to run in the background. More information about docker compose can be found here https://docs.docker.com/compose/compose-file/compose-file-v3/. An example docker-compose.yml is seen below.

version: '3.3' services:

 websrv:
   image: nginx
   ports:
     - "80:80"
     - "443:443"
   volumes:
     - ./html:/var/lib/html
     - ./sites:/etc/nginx/sites-available

Docker Commands

Listed are a few basic docker commands, and explanations.

docker run <options> <image>

Creates and runs a container as specified in the command

docker exec <container> <command>

Runs a new command in a running container

docker ps

Lists containers and their status

docker attach <container id>

Attaches to the STDOUT of a running container, usually will display output of whatever the entrypoint or cmd is

docker inspect <container id>

Shows detailed information about the specified container. container ID can be obtained from docker ps

docker system prune

Cleans up (removes) unused containers and images. Use with caution

docker build <tag> <location>

builds an image from a dockerfile

docker-compose up -d

Applies a docker-compose file in the background. Any containers modified in the docker-compose file will be replaced.

docker-compose down

Stops containers from a docker-compose file

docker-compose restart

Restarts containers from a docker compose, without recreating them

Binds and Volumes

Containers are normally not persistent, in that any changes made inside the container will be removed when the container is replaced or restarted. To store changes persistently, the files modified must be in either a volume or exist as a bind on the host. Volumes are a directory on the host managed by docker, and mapped to a directory in the container. Any files that exist in the container will be copied to a volume if the volume is mapped to the directory they are in. Binds are based on the docker volume system, but allow any directory or file on the host to be mapped to any directory or file in the container. Containers will not copy their contents into a bind, which is a major difference from volumes. Binds are useful for data directories or configuration files.

To create a volume and attach it, either pass it as an option on a docker run ( docker run -v <name of volume>:<path in container> ) or specify it in a docker-compose file as shown above.

To create a bind, either pass it as an option on a docker run ( docker run -v </path/to/directory/or/file>:<path in container> ) or specify it in a docker-compose as shown above.