Skip to content

Instantly share code, notes, and snippets.

@londoncalling
Last active May 23, 2019 09:59
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save londoncalling/8a8dc031f5d5150b2d76 to your computer and use it in GitHub Desktop.
Save londoncalling/8a8dc031f5d5150b2d76 to your computer and use it in GitHub Desktop.
My Docker Cheatsheet

My Docker Cheat Sheet

Practical basic concepts

Files and folders

Matching up client and server versions

Commands

New CLI variations on original commands

Creating new images

Using Dockerfiles and building new images

Ports and IP addresses

Cloud servers hosting docs drafts (OUT OF DATE)

How to set up a cloud server for docs drafts on Digital Ocean

Using containers for development environments w/ATBaker's tutorials

Practical basic concepts

  • Applications run in containers
  • You start a container with the docker run command. For example, to start a new container from the Docker image called busybox with the command echo "hi there", run this command docker run busybox /bin/echo "hi there"
  • Docker images are how we start containers. They are saved states of containers. Every Docker image was once a container that someone committed into an image.
  • To see a history of layers on an image, run the command docker history <image_name>. Images are a collection of layers of previous commands that have been run in them.
  • Dockerfiles are a series of commands used to build an image.
  • The Docker Engine manages containers on your machine.
  • The Docker Hub is a website and remote, public registry of Docker images for sharing.
  • A Docker image is the saved state of an application and environment at a point in time.
  • Docker containers are for local execution of the code (image).

Files and Folders

mydockerbuild (created per the Getting Started is in ~/ (my home directory)

Matching up client and server versions

  • Jean-Laurent's command to create a machine using latest version of VirtualBox driver (when you have a Docker client/server mismatch): docker-machine create -d virtualbox --virtualbox-boot2docker-url https://github.com/boot2docker/boot2docker/releases/download/v1.10.0-rc2-b/boot2docker.iso rc2

Commands

Some of the commands here are docker-machine commands. You use Docker Machine to run Docker on Mac, and to provision Docker on other machines. (Orchestration and clustering uses a combo of Docker Machine and Swarm.)

  • docker-machine ls lists available Docker remote/virtual hosts created with Docker Machine, and shows their start/stopped status. These hosts could be local (on a virtual box) or in the cloud.

  • docker-machine start default starts a Docker host (in this case, "default")

  • eval "$(docker-machine env default)" connects the current shell to the given Docker remote host (in this case, the Docker host is "default")

  • docker-machine ssh default logs in to the a Docker host or VM

  • $ docker-machine ip default gets the IP address of the named Docker host (in this case, "default")

  • docker run -it ubuntu /bin/bash to get an interactive bash shell session on the Docker host directly with the docker run command, where ubuntu is a container. The host name is the id of the container, and we are logged in as root. (Control-D to exit.)

  • docker run -it busybox sh to get an interactive shell on busybox, where busybox is the image. (busybox doesn't have a bash shell. Type exit or Control-D to exit the container.)

  • docker exec -it [container-id|container-name] bash logs you into a running container; e.g., docker exec -it webserver bash

  • Type exit or Ctrl-D keyboard command to exit out of the container.

  • docker run -d [options] [image] runs a command in the background (detached mode), so you can still use the terminal, e.g., $ docker run -d -p 8000:80 atbaker/nginx-example starts a container in detached mode based on the image nginx-example, and exposes port 80 on the container through port 8000 on the host. (See the "Ports" topics in this cheatsheet for more on ports.)

  • You can use --name to name the container; e.g., docker run -d -p 8000:80 --name webserver atbaker/nginx-example

  • docker run -d -P --name webserver nginx starts a container in detached mode based on the official nginx image on Docker Hub, and the P exposes all ports on the container to random ports on the host.

  • Type docker port webserver to view the ports.

  • docker -logs shows you the log information you would see if you were not running the container in detached mode

  • docker -f <containerName> and docker attach <containerName> both follow the log output on a detached container. The attach flag also captures inputs you provide the container, so if you Ctl-C to exit it stops the container.

  • docker cp webserver:/usr/share/nginx/html/index.html . copies the index.html file from a container running the nginx server to the current directory. (The syntax is docker cp [container id | name]:path to file .)

  • See https://docs.docker.com/engine/reference/commandline/cp/ for info on how to copy files back into a container.

  • docker version

  • docker info

  • docker run <app> e.g., docker run hello-world or docker run docker/whalesay cowsay boo

  • docker images

  • docker ps

  • docker stop <container ID or name>

  • docker rm <container ID or name>

  • docker rm -f <container id or name> forces removal of a running container

  • docker ps -a -q shows just the id's for all containers

  • docker rm -f $(docker ps -aq) passes the id's of all running containers to the docker rm -f command, thereby removing all containers

  • docker rmi -f $(docker images -q -a -f dangling=true) (removes unused containers and images)

  • docker rmi -f $(docker images -q -a ) (removes all images)

  • docker ps -q -a | xargs docker rm (removes all containers)

  • For more on removing images and containers see: Removing all unused Docker containers and images and Docker - Clean Up After Yourself!

  • docker build -t docker-goldfish:latest .

    The syntax is <docker command> <options> <image name> : <tag>

    If you don't specify a tag (e.g., 1.0), it will default to "latest" for the tag.

  • docker-machine rm <machine_name>

New CLI variations on original commands

Both versions of these commands work. Here are the old and new versions. In most cases, the new versions should be more intuitive:

Original New
docker run hello-world docker container run hello-world
docker images docker image ls
TBD TBD

Creating new images

  • Start with an existing image, and modify it (e.g., docker run -it atbaker/redis-example /bin/bash and edit /usr/src/custom-redis.conf to uncomment the require-pass foobared line).

  • Do a docker commit to create a new image, building on the base image; e.g., docker commit -m "SomeMessage" [container id|container image] SomeTagName (this will return the ID of the new image we just created)

  • To run the new image in a container: docker run -p 6379:6379 TagName redis-server /usr/src/custom-redis.conf(Because we modified the image manually, we need to specify the command that should run in the container. For the redis server it's redis-server then the path to the configuration file (/usr/src/custom-redis.conf). (See http://redis.io/topics/quickstart and https://hub.docker.com/_/redis/.) This runs the rediscontainer. To run the redis-cli client, you should be able to run redis-cli -h [private IP of redis-cli VM] -p 6379. (But when I tried this, I couldn't reach the redis-cli on the VM.)

  • Log into the Docker Hub from the command line with docker login (for new users, you can register with this command, too)

  • Tag the new image in your namespace to turn it into a repository: docker tag redis-password [yourUserName]/[imageName] e.g., docker tag redis-password atbaker/redis-password

  • Push the new image to the Docker Hub with docker push [yourUserName]/[imageName] e.g., docker push atbaker/redis-password

Using Dockerfiles and building new images

A Dockerfile is a simple text-file that contains a list of commands that the docker client calls while creating an image. It is simple way to automate the image creation process. The best part is that the commands you write in a Dockerfile are almost identical to their equivalent Linux commands. This means you don't really have to learn new syntax to create your own dockerfiles.

See Dockerfiles in this tutorial: http://prakhar.me/docker-curriculum/#dockerfiles.

Ports and IP addresses

  • To start an example web application that will expose port 80 on the Docker host, type the command docker run -p 8000:80 atbaker/nginx-example. The -p option is used to expose a port from the container to our host operating system. This takes port 80 from the container and makes it accessible on port 8000 on the host machine.

  • To get to this in a browser, you need to build the URL from a special IP address (not localhost:8000). To get the IP address, run docker-machine ip <host_name>, paste the IP address you get in a browser and then add :8000 to the end of it.

  • Piñata uses docker.local to represent the local host. To find out the IP address of a container using Piñata, you can use ping; i.e., ping docker.local.

  • The information on PORTS in this output to the docker ps command shows that the port ​_inside_​ the container is 8000 and it is mapped to port 8001 on its Docker host:

 PORTS                    NAMES
40c62a77f16c        docs-base:master    "hugo server --port=8"   About a minute ago   Up About a minute   0.0.0.0:8001->8000/tcp   pensive_p

  • To see what port you are using on the host for a running container: docker port <containerName> <containerPort>, e.g., if you are forwarding port 80 on a webserver container to port 8000 on the host, then docker port webserver 80 will show you 0.0.0.0:8000. (0.0.0 indicates the container is accepting requests from all hosts.)

  • To see ip addresses used in a container use docker inspect [OPTIONS] [CONTAINER NAME|CONTAINER ID] and look for NetworkSettings. (See https://docs.docker.com/engine/reference/commandline/inspect/.)

  • To see your host ip address: ifconfig en0 | grep "inet " | cut -d " " -f2=

  • Or on the Mac, go to System Preferences -> Network -> [Ethernet | Wi-Fi] -> Advanced -> TCP/IP to see host ip address

  • Use -P to publish all the exposed container ports to random ports on the Docker host; e.g.,

     $ docker run --name static-site -e AUTHOR="Your Name" -d -P seqvence/static-site
    

    Now you can see the ports by running the docker port command.

     $ docker port static-site
     443/tcp -> 0.0.0.0:32768
     80/tcp -> 0.0.0.0:32769
    

    For more examples like this last one, see Webapps with Docker in docker/labs.

Cloud servers hosting docs drafts (OUT OF DATE)

... BUT I HATE TO DELETE IT BECAUSE IT TOOK SO MUCH FIGURING OUT!

Note: Info on how to use docker-machine to create machines on cloud servers is now at Docker Machine docs, in the topic on how to use Docker Machine to provision hosts in the cloud.

I have various docsets in draft state at any one time waiting merges, served up on Digital Ocean. Current URLs:

To serve up different docs repos from the same cloud host, just edit the Makefile in <repo>/docs/ by finding and changing the values for all mentions of DOCSPORT to something other than 8000.

# to allow `make DOCSPORT=9000 docs`
DOCSPORT := 8000

docs: docs-build
	$(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 -e DOCKERHOST "$(DOCKER_DOCS_IMAGE)" hugo server --port=$(DOCSPORT) --baseUrl=$(HUGO_BASE_URL) --bind=$(HUGO_BIND_IP)

docs-draft: docs-build
	$(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 -e DOCKERHOST "$(DOCKER_DOCS_IMAGE)" hugo server --buildDrafts="true" --port=$(DOCSPORT) --baseUrl=$(HUGO_BASE_URL) --bind=$(HUGO_BIND_IP)


docs-shell: docs-build
	$(DOCKER_RUN_DOCS) -p $(if $(DOCSPORT),$(DOCSPORT):)8000 "$(DOCKER_DOCS_IMAGE)" bash

NOTE: Any changes to the docs/Makefile must be local changes only for the purposes of a throw-away docset. Don't check any of this back into GitHub.

If we pull down another repo, and edit docs/Makefile to change those instances of 8000 to 8001, then run another make docs, we get this output to docker ps.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
7faa7887c522        docs-base:master    "hugo server --port=8"   30 minutes ago      Up 30 minutes       8000/tcp, 0.0.0.0:8001->8001/tcp   furious_northcutt
5555fad15f11        e077ca8fbc2e        "hugo server --port=8"   22 hours ago        Up 22 hours         0.0.0.0:8000->8000/tcp             jolly_joliot

How to set up a cloud server for docs drafts on Digital Ocean

Here are the steps, cobbled together from trial-and-error, Docker docs, and others.

Use Docker Machine to create the droplet

docker-machine create --driver digitalocean --digitalocean-access-token <really-long-alphanumeric-key> dock-sandbox
Creating SSH key...
Creating Digital Ocean droplet...
To see how to connect Docker to this machine, run: docker-machine env sandox

Build and publish the docs

From here, the easiest thing to do is make docs from the shell that's pointed to the new machine (e.g., sandbox in our example). That will build and publish the docs on the remote host. This is because the docs build (described in the docs Makefile) uses Docker along with Hugo and other tools to build and deploy the docs on a webserver running in a Docker container. So when you run make docs on a shell pointed to a remote, Dockerized machine, that docs build will run and deploy on the remote host.

You can also set up the remote host natively to buid the docs, as per the instructions below.

ssh into the cloud host

$ docker-machine ssh sandbox
Welcome to Ubuntu 15.10 (GNU/Linux 4.2.0-16-generic x86_64)

Check git and docker versions, and get make

root@sandbox:~# git --version
git version 2.5.0

root@sandbox:~# docker --version
Docker version 1.9.1, build a34a1d5

root@sandbox:~# apt-get install make
< lots of detail on install>
...

Generate SSH keys and add them to your account

See Generating SSH keys in GitHub Help.

Pull down the docs from Git and make

Pull down the docs from Git and run 'make docs' on the remote host. This will also publish to the same IP as in the easier method of just building from a shell pointed to the remote machine.

Using containers for development environments with ATBaker tutorials

Previously, developers were installing development apps, database software, etc. on their local systems and just using Docker to test, build, package up and deploy their apps. Now they can use containerized versions of these development environments and don't have to install all that locally.

To follow AT Baker tutorials on Docker, use this new strategy. For example, rather than installing Redis or MongoDB locally, run them in containers and link them, or use docker run exec it (with or without the bash shell) to reach into containers and use the apps.

Mongo DB example

  1. Get the ID of the running container.

    $ docker ps
    

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 911df6c90982 mongodb "mongod" 15 seconds ago Up 14 seconds 192.168.64.61:32769->27017/tcp awesome_ramanujan

```
  1. Log into the running container with docker exec -it [CONTAINER NAME | CONTAINER ID] bash

    $ docker exec -it 911df bash
    
  2. At the prompt inside the container, you can run the commands you need by calling localhost and the port. This gets you to the prompt for MongoDB.

    root@911df6c90982:/# mongo localhost:27017                                                                                  
    MongoDB shell version: 2.6.12
    connecting to: localhost:27017/test
    Welcome to the MongoDB shell.
    For interactive help, type "help".
    For more comprehensive documentation, see
    http://docs.mongodb.org/
    Questions? Try the support group
    http://groups.google.com/group/mongodb-user
    >
    

Docker Training

#Dockerfile
FROM debian

RUN apt-get update
RUN apt-get install -y postgresql

ADD README.md /.

Commands

docker build -t orangesnap/demo .
docker run --rm -i -t billmills/demo /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment