Docker

Running containers

docker run -t -i IMAGE COMMAND
docker run -t -i ubuntu /bin/bash
docker run -d ubuntu /bin/bash

docker stop CONTAINER
docker start CONTAINER                  # start previously stopped container

docker exec -it CONTAINER COMMAND       # attach to running container
docker exec -it mycontainer bash        # attach to running mycontainer and use bash shell

docker rm CONTAINER                     # remove stopped container
docker rm `docker ps -aq`               # remove all stopped containers
docker rm $(docker ps -aq)              # remove all stopped containers

- -t tty terminal inside container
- -i grab STDIN of container
- -d run as daemon
- -P automatically map container's ports to host

Monitoring containers

docker ps

- -l return last running container
- -a list all containers

docker logs CONTAINER             # see CONTAINER's outputs
docker logs -f nostalgic_morse    # follow nostalgic_morse's output
docker top CONTAINER              # list processes running inside container

- -f follow, like tail -f

docker inspect nostalgic_morse    # list JSON data about nostalgic_morse
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nostalgic_morse

Networking containers

docker run -d -P training/webapp python app.py
docker run -d -P 80:5000 training/webapp python app.py    # map host's 80 to container's 5000

docker port CONTAINER PORT          # get CONTAINER's PORT mapped to host's
docker port training/webapp 5000    # what host's port is mapped to container's 5000

Managing docker images

docker images
docker pull centos      # download centos from docker hub
docker search centos    # search for centos on docker hub
docker push CONTAINER
docker rmi CONTAINER    # remove container from host

Building docker images

Create a Dockerfile with content like below
  FROM ubuntu:14.04
  MAINTAINER Kate Smith <ksmith@example.com>
  RUN apt-get update && apt-get install -y ruby ruby-dev
  RUN gem install sinatra

Build the image with:
  docker build -t ouruser/sinatra:v2 .

  - v2 tags the image with version v2
  - the dot specifies where the **Dockerfile** file is located

  docker tag CONTAINER user/name:tag