Skip to content

Instantly share code, notes, and snippets.

@fuzzylimes
Last active April 15, 2018 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzylimes/0247cc03e5b7f84b6eb58231ba827179 to your computer and use it in GitHub Desktop.
Save fuzzylimes/0247cc03e5b7f84b6eb58231ba827179 to your computer and use it in GitHub Desktop.
Docker Notes

Notes from the Docker Course

Repo is available here:

git clone https://github.com/BretFisher/udemy-docker-mastery.git

Installing Docker the right way on linux

curl -sSL https://get.docker.com/ | sh

Baisc commands

  • docker pull - pull an image down from docker hub

  • docker image ls - list all downloaded images

  • docker version - check version

  • docker info - show details about docker on the system

  • docker <command> and docker <command> <subcommand>

  • docker container top - process list in the container

  • docker container inspect - details of container config

  • docker container stats - performance status of container

  • Use the --rm flag with docker container run to have the container automatically closed

  • docker container run -p - exposes port

  • docker contianer port <contianer> - check which ports a contianer is using docker container run --publish 80:80 --detach nginx - run headless docker container stop 5791 - stop container docker contianer ls - only shows running instances docker container ls -a - shows all intances

docker container logs 4dd8 - show logs docker container top 4dd8c - see top running processes

docker container rm 4dd 579 624 - cleanup multiple contianers docker container rm -f 4dd - force cleanup of running containers

Helpful Commands

  • docker rm $(docker ps -qa --no-trunc --filter "status=exited") - Cleanup all exited containers

2.2 Assignment

  1. docker run --name nginx --publish 80:80 -d nginx
  2. docker run --name httpd --publish 8080:80 -d httpd
  3. docker run --name mysql --publish 3306:3306 --env MYSQL_RANDOM_ROOT_PASSWORD=yes -d mysql
  4. docker ps
  5. docker logs mysql
  6. docker container stop mysql httpd nginx
  7. docker container rm mysql httpd nginx
  8. docker container ls -a

Getting inside containers

  • docker contianer run -it - start new container interactively
  • docker container exec -it - run additional command in existing container

Networking

  • docker network ls - list docker networks
  • docker network inspect <network> - inspect network
  • docker network create --driver - attach a new network to container
  • docker network disconnect <network> - deatch a network from computer
  • Containers on the same network are able to talk to each other using their names (DNS, NOT IP addresses) ** docker container exec -it nginx ping new_nginx

2.28 Assignment

  • docker container run -it --rm ubuntu:14.04 ** apt-get update && apt-get install -y curl ** curl version -> 7.35
  • docker container run -it --rm centos:7 ** curl version -> 7.29

2.30 Assignment (DNS Round Robin)

  • docker network create dude
  • docker container run -d --net dude --net-alias search elasticsearch:2
  • docker container ls
  • docker container run --rm --net dude alpine nslookup search
  • docker container run --rm --net dude centos curl -s search:9200
  • docker container rm -f jovial_northcutt zealous_panini

Docker image layers

  • Each of the layers get their own unique SHA value
  • Layers can be OS, install commands, environment variables, system changes, etc
  • Bundles of layers add up to make an image
  • docker image inspect nginx - returns back metadata about the image
  • docker history mysql:latest - shows the history of all the layers within an image

Image tagging

  • Tags are pointers to specific image commits
  • docker image tag nginx fuzzylimes/nginx - tag existing image with a new tag
  • docker image tag fuzzylimes/nginx fuzzylimes/nginx:testing - tag same image with a different (specific) tag name
  • latest tag is default. Used to represent the most recent and stable version

Uploading image to Docker Hub

  • docker login - Login to DockerHub
  • cat .docker/config.json - view file
  • docker logout - Delete login key
  • docker image push fuzzylimes/nginx:testing - push image file to Docker Hub
  • If using a private repo, create repo first before pushing

Dockerfile

  • docker image build -t customnginx . - build image from dockerfile with tag in the specified directory
  • FROM - Defines the image to be used as the base
  • RUN - Runs a command (bash) inside the container
  • WORKDIR - Correct way of setting the working directory in a container (i.e. same as cding to path)
  • COPY - Copy file into WORKDIR path

3.40 Assignment

  • docker image build -t node-assignment-1 .
  • docker container run -p 80:3000 --rm node-assignment-1:latest
  • docker image rm fuzzylimes/node-assignment-1
  • docker container run -p 80:3000 --rm fuzzylimes/node-assignment-1:latest
# Instructions from the app developer
# - you should use the 'node' official image, with the alpine 6.x branch
FROM node:6-alpine
# - this app listens on port 3000, but the container should launch on port 80
  #  so it will respond to http://localhost:80 on your computer
EXPOSE 3000
# - then it should use alpine package manager to install tini: 'apk add --update tini'
RUN apk add --update tini \
# - then it should create directory /usr/src/app for app files with 'mkdir -p /usr/src/app'
    && mkdir -p /usr/src/app
WORKDIR /usr/src/app
# - Node uses a "package manager", so it needs to copy in package.json file
COPY package.json package.json
# - then it needs to run 'npm install' to install dependencies from that file
RUN npm install \
# - to keep it clean and small, run 'npm cache clean --force' after above
    && npm cache clean --force
# - then it needs to copy in all files from current directory
COPY . .
# - then it needs to start container with command '/sbin/tini -- node ./bin/www'
CMD ["/sbin/tini", "--", "node", "./bin/www"]
# - in the end you should be using FROM, RUN, WORKDIR, COPY, EXPOSE, and CMD commands

Persistent data

Volumes

  • Data can be saved outside of the container using volumes
  • Some images will do this automatically (like mysql)
  • docker volume ls - view the list of created volumes
  • To create a named volume, use the -v parmaeter: docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql
  • Can also create volumes using the docker volume create command. Only needed for specifying driver related stuff.

Bind Mounting

  • Maps a host file or directory to a container file or directory
  • Can't be specified in Dockerfile, must be done with container run
  • ...run -v /Users/name/stuff:/path/container

Assignment 4.44 (Volumes)

  • docker container run -d --name psql -v psql:/var/lib/postgresql/data postgres:9.6.1
  • docker container stop psql
  • docker container run -d --name psql2 -v psql:/var/lib/postgresql/data postgres:9.6.2
  • docker container logs psql2

Assignment 4.46 (Bind Mounts)

  • cd udemy-docker-mastery/bindmount-sample-1/
  • docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve

Docker Compose

  • Configures the relationships between containers
  • saves our docker container run settings in yml file
  • create one-liner environment startups
  • Made of two things: ** YML file ** cli tool docker-compose
  • docker-compose.yml is defail, but can use any name with docker-compose -f

docker-compse.yml

  • version: always use at least 2, defaults to 1 (3.1 current)

  • services: containers. Same as docker run ** servicename: a friendly name, DNS name inside the network ** image: (Optional) ** command: (Optional) replace the defualt CMD specified by the image ** environement: (Optional) same as -e in docker run ** volumes: (Optional) same as -v in docker run

  • volumes: (Optional) same as docker volume create

  • network: (Optional) same as docker network create

Example 1

version: '2'

# same as
# docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve

services:
    jekyll:
        image: bretfisher/jekyll-serve
        volumes:
            - .:/site
        ports:
            - '80:4000'

Example 2

version: '2'

services:
    
    wordpress:
        image: wordpress
        ports:
            - 8080:80
        environment:
            WORDPRESS_DB_PASSWORD: example
        volumes:
            - ./wordpress-data:/var/www/html

    mysql:
        image: mariadb
        environment:
            MYSQL_ROOT_PASSWORD: example
        volumes:
            - ./mysql-data:/var/lib/mysql

docker-compose CLI

  • cd /udemy-docker-mastery/compose-sample-2
version: '3'

services:
    proxy:
        image: nginx:1.11 # using version 1.11.x
        ports:
            - '80:80' # expose 80 on host and sent to 80 in container
        volumes:
            # can set a file to be read only (ro) inside container
            - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
    web:
        image: httpd
  • docker-compose up - streaming logs
  • docker-compose up -d - run in background
  • docker-compose logs - get logs from compose (same as those streamed)
  • docker-compose ps - list containers run by docker-compose
  • docker-compose down - stop and cleanup

Assignment 5.50 (Compose File)

version: '2'

services:
    drupal:
        image: drupal
        ports:
            - '8080:80'
        volumes:
            - drupal-modules:/var/www/html/modules
            - drupal-profiles:/var/www/html/profiles
            - drupal-themes:/var/www/html/themes
            - drupal-sites:/var/www/html/sites
    postgres:
        image: postgres
        environment:
            POSTGRES_PASSWORD: example
volumes:
    drupal-modules:
    drupal-profiles:
    drupal-themes:
    drupal-sites:
  • docker-compose down -v: take down and remove volumes

Using docker-compose to build

  • Will build the first time when docker-compose up is run
  • Can run docker-compose build to rebuild

Assignment 5.53 (Build and Run Compose)

docker-compose.yml

version: '2'

services:
    drupal:
        image: custom-drupal
        build: .
        ports:
            - '8080:80'
        volumes:
            - drupal-modules:/var/www/html/modules
            - drupal-profiles:/var/www/html/profiles
            - drupal-themes:/var/www/html/themes
            - drupal-sites:/var/www/html/sites
    postgres:
        image: postgres
        environment:
            POSTGRES_PASSWORD: 
        volumes:
            - drupal-data:/var/lib/postgresql/data
volumes:
    drupal-modules:
    drupal-profiles:
    drupal-themes:
    drupal-sites:
    drupal-data:

Dockerfile

FROM drupal:8.2

RUN apt-get update && apt-get install -y git \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /var/www/html/themes

RUN git clone --branch 8.x-3.x --single-branch --depth 1 https://git.drupal.org/project/bootstrap.git \
    && chown -R www-data:www-data bootstrap

WORKDIR /var/www/html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment