Docker Basics

Source

Images

Images are the templates used to create Docker Containers. Container is a running instance of an image. Eg. ubuntu Image. Or Apache image.

Commands

Show all images available

docker images

Run the given image(ubuntu) in the interactive mode(-it)

docker run -it ubuntu

Get an image from repository

docker pull <image-name>[:<tag/version>]

Containers

Container is a running instance of an image.

Architechture

Host: The system where docker is run
Client: CLI interface to the docker daemon
Registry: Docker Hub or other places where images are stored.

Docker Architechture

Dockerfile

Dockerfile is a text file with insturctions to build an image. Generally, it has the filename Dockerfile. Sample…

# The base image. This search locally - if not present, it gets pulled from repository
FROM ubuntu

# Run this command
RUN apt-get update

Instructions

  • FROM: Sets the Base Image for subsequent instructions.
  • RUN: execute any commands in a new layer on top of the current image and commit the results.
  • CMD: provide defaults for an executing container.
  • EXPOSE: informs Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
  • ENV: sets environment variable.
  • COPY: copies new files or directories to container. By default this copies as root regardless of the USER/WORKDIR settings. Use –chown=USER:GROUP to give ownership to another user/group. (Same for ADD.)
  • VOLUME: creates a mount point for externally mounted volumes or other containers.
  • USER: sets the user name for following RUN / CMD / ENTRYPOINT commands.
  • WORKDIR: sets the working directory.
  • LABEL: apply key/value metadata to your images, containers, or daemons.

Dockerfile Cheetsheet
Dockerfile Reference

Run docker build . in the same folder to build an image from this Dockerfile.

Docker Compose

Its a tool for creating and running systems which has multiple containers within it. Eg. A web app will require an API Server, Frontend Server and Database Server. You can run all this together using docker compose.

You’ll have to install docker-compose before you use it – its NOT part of the default docker install in linux.

Use docker-compose.yml to design the system.

Commands

docker-compose config   # Checks validity of the docker-compose.yml file
docker-compose up       # Start all Services. Add -d flag to run in the background.
docker-compose down     # Stop all Services

Volumes

Think of Volumes as the ‘HardDisks’ for the containers – the persintent data storage location. Containers can read and write to it – and the data will stay even after the containers are shut down.

Uses

  • Decouple data storage from containers
  • Share data accross multiple containers
  • Persistant storage – when stoping the container, the data doesn’t get deleted.

Commands

docker volume create NAME
docker volume ls
docker volume install NAME # Show details about the volume.
docker volume rm NAME # Remove volume
docker run -v NAME:/var/lib/www IMAGE_NAME # Attaching a volume to an IMAGE - and mount it at location /var/lib/www

Bind Mounts

An alternative to using volumes is bind mount. Bind mount is a location in the host system that is running the container. This can be changed by the host system/user(unlike volumes).

Debugging

Comnand to run an interactive shell with in a container…

docker exec -it NAME sh

Author: Binny V A
A philosopher programmer who specializes in backend development and stoicism.

Leave a Reply

Your email address will not be published. Required fields are marked *