Skip to content

Latest commit

 

History

History
258 lines (228 loc) · 6.66 KB

File metadata and controls

258 lines (228 loc) · 6.66 KB

Docker

> convert applications into docker images which can be run as docker containers on other machines with docker installed

Commands

> docker {command}

run {-d} {application}

> docker run nginx

  • run instance of nginx application in docker container if it already exists
    • else we pull the image from docker hub and use that

-d

  • runs a detached instance in the background

attach {container_name/id}

> docker attach cool_container

  • reattach container instance

ps

  • list running containers

ps -a

  • list all containers

stop {container_name/id}

> docker stop cool_container > docker stop 123456789

  • stop a container

rm {container_name/id}

> docker rm cool_container

  • remove a container
    • can type only first part of container id if there are no conflicts

images

  • list images

rmi {image}

  • remove an image
    • ensure no containers are running off the image
    • delete all dependent containers

pull {image}

> docker pull nginx

  • download an image from docker hub

exec {container_name} {command}

> docker exec ubuntu_container cat /etc/hosts

A container only lives as long as the process inside it is alive

  • if the process stops/crashes, the container exits

inspect {container_name}

> docker inspect cool_container

  • inspect a container in JSON form

logs {container_name}

> docker inspect cool_container

  • view output from a container running in the background

Run arguments

Tags

> docker run redis:latest (default) > docker run redis:4.0

  • docker image versions

Input

-i

  • interactive mode
    • read input from stdin

-t

  • terminal
    • display terminal prompts

-it

  • combine -i, -t

Mapping external (host):internal (container)

Port Mapping

  • allow users outside of the container to access the docker host

> docker run -p 80:5000 webapp

  • map external port 80 to internal port 5000
    • run multiple instances of applications and map to different ports

Volume Mapping

  • docker containers have their own filesystem
  • map a directory outside the docker host to a directory inside the container

> docker run -v /external/directory:/container/directory/sql sql_container

  • ex. save db data before we destroy a sql container

Environment Variables

> docker run -e ENV_VARIABLE=epic cool_container

  • we can find the list of environment variables using `docker inspect“

Images

Containerizing

  • creating you own images for shipping/debugging

Dockerfile

  • the steps required to set up an image

{instruction} {argument}

{instruction}

  • a docker command

{argument}

  • the argument for the docker command

Commands

FROM

> FROM Ubuntu

  • OS from base/image

RUN

> RUN pip install flask

  • install dependencies

COPY

> COPY . src

  • copy all from src/
  • copy files from host to image

ENTRYPOINT

> ENTRYPOINT FLASK_APP=/src/main.py flask run

  • app entrypoint

build -t {image_name} .

  • save image locally
    • each build step is a layer
    • we only need to rebuild updated layers

-t

  • specify image name

.

  • use the dockerfile in the current directory

push {image_name}

  • push image to docker hub

CMD/Entrypoint

CMD

  • when running an image, there is a default CMD command
    • ex. nginx has “nginx” as its command

> docker run ubuntu

  • ubuntu uses bash
    • bash is a terminal that listens to inputs, but by default docker does not attach a terminal
      • so it exits immediately

> docker run ubuntu [command]

  • overwrite the default command
    • we can also overwrite in the dockerfile > CMD sleep 5 > CMD [“sleep”, “5”]

Entrypoint

  • like CMD but instead of hard-coding CMD parameters (cmd 5), we append them

> ENTRYPOINT [“sleep”] > docker run image 10

  • command: sleep 10
  • we can use CMD to as a default value for the parameter > ENTRYPOINT [“sleep”] > CMD[“5”]

Networking

Default Networks

Bridge

  • default network
  • private, internally created on host
  • 172.17.x
    • containers access each other from bridge
    • we can map to these networks for external usage

None

  • containers are not connected to any network

Host

  • external usage
  • associate container to host network
  • no port mapping needed, direct access

User-Defined Networks

> docker network create {args}

  • create multiople bridge networks in the host

network ls

  • view all networks

Embedded DNS

> mysql.connect(172.17.0.1) > mysql.connect(sql_container)

  • we can use the container name instead of its address

File System

Layers

Image Layers

  • created on build
    • read-only
  • docker will reuse identical image layers when building similar applications
    • shared by containers using image

Container Layer

  • created on run
    • read-write
  • store data created by the container
    • destroyed on container destruction

Modifying Image Layer Files

  • we can “modify” files in the image layer from our container as a copy saved to our container layer

Volumes

> volume create data_volume

  • create a new volume called data_volume under the docker/volumes/

Volume Mounting

> docker run -v data_volume:/var/lib/sql sql_container

  • mount volume to /var/lib/sql inside the container; all data is written to data_volume

> docker run -v new_volume:/var/lib/sql sql_container

  • automatically creates volume called new_volume

> docker run -v /external/directory:/var/lib/sql sql_container

  • we can store our data anywhere, not just a docker volume
    • bind mounting

Storage Drivers

Docker Compose

  • running multi-container docker applications

Linking (deprecated)

run –link external (host):internal (container)

> docker run -d –link redis:redis app

docker-compose v1

> docker-compose.yml

container_name:
  image: img_name
  build: ./src
  ports:
    - external:internal
  links:
    - link:link
    - link (same as link:link)
  • instead of using an image from docker hub, we can build our own image
redis:
  image: redis
db:
  image: postgres
worker:
  build: ./worker
  links:
    - redis
    - db

docker-compose up

  • build, create, start container

check out docker-compose v2, v3

  • services, dependencies, networks, auto linking

Docker Registry

  • when we pull nginx from docker hub, we are actually pulling nginx/nginx > image: docker.io/nginx/nginx
    • registry/username/image

Private Registry

  • private pics

Orchestration

  • automate running containers, monitor host/container health
    • create new containers if containers fail, scale containers to usage, load balancing
  • multple instances (replicas)
  • docker swarm, kubernetes

Docker Swarm

  • combine docker machines into clusters
    • distribute application instances into hosts
  • swarm manager/workers
    • join, nodes

Swarm Orchestration

Docker Services

  • instances of a service that run on nodes