Skip to content

Docker

Docker Commands

  • Images: docker images
  • Containers (running): docker ps
  • Containers (stopped): sudo docker ps -a
  • Pull: docker pull nginx:latest
  • Run Image: docker run nginx:latest
  • Run Interactively: docker run -it ubuntu:latest /bin/bash
Info

-it means interactive + attach terminal.

  • Attach terminal to running container: docker exec -it 1uu273u128
  • Run some command in the running container: docker exec ubuntu ls /
  • Stop container: docker stop nginx
  • Remove container: docker rm 1t1u238udhg or docker rm -f 8uw17jd182
  • Remove Image: docker rmi nginx:latest
  • Inspect: docker inspect <container_name>
  • Logs: docker logs <container_name>
  • Environment Variables: docker run -e COLOR=blue app
  • Give another name to the contaienr: docker run -d --name=myapp nginx
  • Tagging image manually: docker tag name name:2.9
  • Exporting a container image: docker save -o name.tar name
  • Load a container image: docker load -i name.tar

Docker vs VM

  • Each VM has its own OS.
  • Docker containers share the same underlying OS.

Volume Mapping

docker run -v /dir/on/system:/dir/in/container ubuntu

CMD vs ENTRYPOINT

CMD The arguments given at the runtime replace the default CMD argument in the dockerfile.

CMD sleep 5

ENTRYPOINT The arguments are appended.

Using both together

Dockerfile
ENTRYPOINT ["sleep"]
CMD ["5"]

means that if the argument is not provided in the terminal then the default 5 is used.

Docker Compose

  • All the services defined are connected to the same network and can access each other by their name.
compose.yaml
version: '3'
services:
    redis:
        image: redis

    db:
        image: postgres:9.4
        environment:
            POSTGRES_KEY: adsd

    vote:
        image: voting-app
        port:
            - "8080:80"

build directive

build: ./urlShortener
  • Should specify the image to be built from the urlShortener directory which has the application code and the corresponding Dockerfile.

About Docker

Docker Engine

Docker engine has 3 components in the following order:

1) Docker CLI - The CLI doesn't need to be on the same system. It can also reference REST API from another remote server using: docker -H=10.0.0.2:2375 run nginx

2) Docker REST API: To talk to daemon

3) Docker Daemon: Manages containers

cgroups

To control the usage of the docker containers.

  • Control CPU

    • docker run --cpus=.5 ubuntu (not occupy more than 50%)
  • Control Memory

    • docker run --memory=100m ubuntu

Default Networks

  • Bridge (default)

    • docker run nginx
    • Each container is assigned an internal IP and is connected to every other container.
  • None

    • docker run nginx --network=none
    • Not attached to the host port.
    • Isolated one.
  • Host

    • docker run nginx --network=host
    • Maps the internal port automatically to the host port.
    • Can only run one container on the same port.
Info

Docker automatically creates a DNS entrypoint for all the container names and their respective IPs, so we can reference them with just their name.

Docker Storage

Docker Directory
1
2
3
4
5
/var/lib/docker
    - aufs
    - containers
    - image
    - volumes

Docker Volume

Layered Architecture

  • Docker has a layered way of storing/creating changes.
  • Each line in Dockerfile is created as a seperate layer and any change in any line is only recreated keeping other layers cached.
  • But all the layers created in the image are READ-only. When we run an image, docker creates a layer called container layer which is WRITABLE. So, any change the user has to do can be done in that layer.
  • No change is done in image layer. The files can be overriden only in the container layer.

Volume Mounting

  • All the mapped volumes are visible under /var/lib/docker/volumes/.
  • To do hot-reload, create a mapped volume:

    • OLD WAY

      • docker run -v /dir/changes:/var/lib/mysql mysql
    • NEW WAY

      • docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql

Creating a volume

  • To create a volume, do: docker volume create my_vol
  • Then, run with: docker run -it -v my_vol:/app image_name
    • For read-only, do docker run -it -v my_vol:/app:ro image_name
  • For copying files from local to inside the volume:
    • After the container is running:
      • docker cp <path> <container-name>:/path/in/data/volume
    • Before the container is running (in Dockerfile):
      • COPY /path/ /path/in/data/volume
Create Volume
sudo docker run -it -d -v /home/neo/Documents/projs/torchflowv2/model:/home/model --env MODEL_PATH="/home/model/model-ckpt.pt" fastapi-server:1.0

Docker Orchestration

  • Can be done through:

    • Docker Swarm
    • K8s
  • Docker Swarm

    • docker service create --replicas=3 -p 8080:80 my-web-server
  • Kubernetes

    • Next Course.
Important
  • To remove unused images and stopped containers, do docker system prune -a / docker system prune -a -f.
  • Remove all the images: docker image prune -a / -f
  • Images are read-only environments.