Skip to content

Instantly share code, notes, and snippets.

@swinton
Last active October 7, 2015 21:51
Show Gist options
  • Save swinton/4711176339bf04d326bf to your computer and use it in GitHub Desktop.
Save swinton/4711176339bf04d326bf to your computer and use it in GitHub Desktop.

Notes on Docker.

Starting a Docker machine

docker-machine start :name: && eval "$(docker-machine env :name:)"

Where :name: is the name of the machine you want to start, e.g. to start default:

docker-machine start default && eval "$(docker-machine env default)"

Containers

docker run runs containers.

E.g.

docker run ubuntu:14.04 /bin/echo 'Hello world'

Interactive Run

docker run -t -i ubuntu:14.04 /bin/bash
  • -t: assigns a pseudo-tty or terminal inside our new container
  • -i: allows us to make an interactive connection by grabbing the standard in (STDIN) of the container

You can also express this as:

docker run -it ubuntu:14.04 /bin/bash

Daemonized Run

docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

This time we get a container ID. Inspect the container ID with docker ps, then inspect the container logs with docker logs :name:, where :name: is the name assigned by Docker, as shown in docker ps. Stop the container with docker stop :name:.

Next.

Running a web app

docker run -d -p 80:5000 training/webapp python app.py

Where:

  • -d: runs container in background and prints container ID
  • -p 80:5000: maps port 5000 inside the container to port 80 on the local Docker host

NOTE: To access the running web app, get the IP address of the local Docker host (via docker-machine ip :name:). E.g., if this is 192.168.99.100, then the web app will be available at http://192.168.99.100:80/.

Next.

Stopping that web app

Find the container name, via docker ps, then:

docker stop :container_name:

Where :container_name: is the name of the container, obvs. E.g.

docker stop stoic_albattani

NOTE: We can also start it again! docker start :container_name:. Restarts too! docker restart :container_name:. And even remove it! docker rm :container_name:.

Next.

Images

Via.

Listing images

docker images

Produces something like:

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mongo               latest              9108562f532a        5 days ago          261.3 MB
ubuntu              14.04               91e54dfb1179        6 weeks ago         188.4 MB
ubuntu              latest              91e54dfb1179        6 weeks ago         188.4 MB
hello-world         latest              af340544ed62        8 weeks ago         960 B
training/webapp     latest              02a8815912ca        4 months ago        348.8 MB

When we run a container, we refer to a tagged image, i.e. docker run -t -i ubuntu:12.04 /bin/bash will run an Ubuntu 12.04 image.

Preloading images

Use docker pull, e.g.:

docker pull centos

Searching for images

On the Docker Hub, via docker search, e.g.

docker search nodeschool

Rolling our own

2 ways to do this:

  1. Update an image
  2. Describe an image using a Dockerfile

The latter is easier when sharing an image with a team, easier to collaborate, track changes.

Example Dockerfile:

# This is a comment
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

Dockerfile reference.

Build an image from a custom Dockerfile using docker build, e.g. assuming Dockerfile is in the cwd:

docker build -t ouruser/example:v2 .

Then create a container for the new image:

docker run -t -i ouruser/example:v2 /bin/bash

NOTE: You can also tag an image (docker tag), push an image to Docker Hub (docker push), remove an image from the host (docker rmi).

Next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment