Main ideas and docker file commands (based on this blog post).

Glossary

  • Image: blueprint to build
  • Container: instance of image
  • Dockerfile: recipe for image, a text file
  • Layer: modifications of the image

Dockerfile commands

  • FROM ubuntu:18.04: build image upon ubuntu base image
  • LABEL maintainer="Firstname Lastname email: optioal metadata
  • ENV LANG=C.UTF-8 LC_ALL=C.UTF-8: Environment variables
  • RUN apt-get update && apt-get install -y python-pip: shell commands to run
  • EXPOSE 7745: open a port (e.g. for jupyter). Publises only if used -p or -P with run
  • VOLUME /ds:
    • mount externally mounted volumes (need to specify the mountpoint at container run/creation)
    • data inside this won't be saved, but outide of it will
  • WORKDIR /ds: starting point for relative file references
  • COPY hom* /mydir/: copies new files and add them to the container's filesystem at a destination path
  • CMD ['/bin/bash']:
    • run a bash shell to prevent closing the container
    • only the last CMD will be executed

Build a Docker Image

docker build -t tutorial -f ./Dockerfile.gpu ./

(This builds an image not a container)

  • -t tutorial: name or tag
  • -f ./Dockerfile.gpu: Dockerfile location
  • ./: context host directory, the relative path refrence point

Build a Docker container from an Image

docker run -it --name container1 --net=host -v ~/docker_files/:/ds tutorial
  • -it: interactive run
  • --net=host: bind ports to host
  • -v /docker_files/:/ds: Mount host directory to you as a volume : the mount detination
  • tutorial: image name

Open a terminal iteractively in the container1 container.

docker exec -it container1 bash

Save current state of container as new image (username is e.g. for DockerHub)

docker commit container1 username/image2name:tagname

List running containers

docker ps -a -f status=running

List images

docker images

Push image to registry

docker push username/image2name:tagname