Friday, February 24, 2017

Introduction to Docker

Notes from Introduction to Docker by Andrew Tork Baker

  1. Two main parts of docker - Docker engine and docker hub
  2. Essential Docker commands:
  • docker run [options] image [command] [args…]
    • docker run busybox /bin/echo "hello world"
    • docker run -it ubuntu /bin/bash - gives interactive shell
    • Docker run -p 8000:80 atbaker/nginx-example
    • Docker run -d -p 8000:80 atbaker/nginx-example - runs container in background (detached mode)
    • Docker run -d -p 8000:80 --name webserver atbaker/nginx-example - to name containers
  • Docker images
    • docker images -q - will list all image ids alone
  • Docker ps - shows active/running containers
    • Docker ps -a - shows all containers (even once we exited)
    • docker ps -a -q - shows all container ids.
  • Docker stop
  • Docker start
  • Docker rm
    • Docker rm -f   - to even remove running container
    • Docker rm -f $(docker ps -a -q) - will remove all containers from the system running or not
Where containerId = short form can be used first 4 unique chars from SHA id of container
  • Docker logs webserver - inspect the logs
    • Docker logs -f webserver -- follows the logs on the container
  • Docker attach webserver - attaches to the running container in detached mode but the disadvantage is if you attached and you exit the container shell then container will exit too. So if you need to check the logs alone then just use docker logs instead.
  • Docker port webserver 80 -- will show the mapping for the port 80 on the container to the port on the docker host
  • Docker diff webserver - what has changed on filesystem of container since we started it
  • Docker cp webserver:/usr/local/nginx/html/index.html . - will copy the index.html from container to local directory
  • Docker inspect webserver -- low level info on container (environment variables, hostname of container etc)
  • Docker history atbaker/nginx-example - will show when each layer in the image was applied to the image
  • Docker search postgres - will search docker hub registry for all images for postgres
  • Postgres:
    • Docker pull postgres:latest - will pull down each layer in the image to local host
    • Docker run -p 5432:5432 postgres
    • psql -U postgres -h localhost - will connect to postgres DB container at port 5432 (need to install psql for this)
  • Redis:
    • Docker pull atbaker/redis-example
    • Docker run -it atbaker/redis-example /bin/bash - to run interactive shell
    • Change the file /usr/src/custom-redis.conf - uncomment requirepass line, exit the shell
    • Docker commit -m "message"   - to save the change done to container as a new image
    • Docker commit -m "setting password" redis-passwd
    • Docker run -p 6379:6379 redis-passwd redis-server /usr/src/custom-redis.conf
    • Redis-cli -h localhost -p 6379
    • Auth foobared
    • Set foo bar
    • Get foo
  • Once image is created you can push it to the docker hub account:
    • Docker login - login to docker hub
    • Docker tag redis-passwd sjsucohort6/redis
    • Docker push sjsucohort6/redis -  by default it will tag remote image as latest
    • docker push sjsucohort6/redis:ver1 - will tag remote image as ver1
  • Mongodb:
    • git clone
    • Docker build -t mongodb . - assuming current working dir has Dockerfile
    • Docker run -P mongodb - the -P option maps any port on localhost to exposed port (27017) of mongodb container
    • Docker ps - find what port on localhost is mapped to mongodb port
    • mongo localhost:32768 -- connect to the mongodb assuming here that local port 32768 was mapped to 27017 of mongodb

TODO - cover dockerfiles, docker compose in a later post.

No comments:

15 sorting algorithms visualized in 5 minutes, with awesome arcade sounds

15 sorting algorithms visualized in 5 minutes, with awesome arcade sounds from r/programming