Containers & Images

Linux containers are a way to create isolated environments that run code while sharing a single operating system.

Docker is a tool that makes it much easier to manage Linux containers.

Docker engine resposible for managing everything relating to the containers.

Docker use cases include:
Developer:

  • creating contained, controlled development environment
  • creating reproducible environment that can be shared across team
  • isolate state of application to help with debugging
    Sysadmin:
  • testing
  • deployment

A Docker image is a pre-packaged set of instructions (a blueprint) used to build and run a container.

Docker Inc published pre-built images in Docker Store (and Docker Hub).

To run a Docker container:
docker container run [-p port-to-open-on-host:port-to-map-to] [-d or --detach] image:version detach to run in background
docker container ls // list running containers
docker container exec ID-or-name command // run commands against running container
docker container exec -it ID-or-name /bin/bash // -it and type of shell to open a shell

Dockerfiles

A Dockerfile is used to configure a container with manually entering all commands.

A Dockerfile is a text file that contains multiple instructions which can run with single command.
The result is a brand new image that can be used to make a container.

FROM base-image creates a new image based of the FROM image
EXPOSE port exposes a port inside the container
RUN command will run any command
LABEL key="value" will label the image

docker image build --tag image-name:version /path/to/Dockerfile

Volumes

Working with data in volumes.
If the image you're building a container with doesn't already contain application files, need an extra step to include.

Various options to do so exist.

  1. Copy files into the running container from the command line.
  2. Copy files into the image with instructions in the Dockerfile. This will include the files everytime the image is used.

Containers don't persist data however, so files are gone after stop running the container.

Data volumes expose files on your host machine to the container.

docker container cp /path/on/host ID-or-name:/path/in/container copy files to running container

COPY /path/on/host /path/in/container to copy files in Dockerfile

docker run -d -p 80:80 -v /files/on/host:/files/on/container image:version -v to create a data volume