Matching up client and server versions
New CLI variations on original commands
Using Dockerfiles and building new images
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
- Applications run in containers
- You start a container with the
docker run
command. For example, to start a new container from the Docker image calledbusybox
with the commandecho "hi there"
, run this commanddocker 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).
mydockerbuild (created per the Getting Started is in ~/ (my home directory)
- 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
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 thedocker run
command, where ubuntu is a container. The host name is the id of the container, and we are logged in asroot
. (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. Typeexit
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 imagenginx-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 officialnginx
image on Docker Hub, and theP
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>
anddocker attach <containerName>
both follow the log output on a detached container. Theattach
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 theindex.html
file from a container running thenginx
server to the current directory. (The syntax isdocker 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
ordocker 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 thedocker 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>
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 |
-
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 therequire-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'sredis-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 theredis
container. To run theredis-cli
client, you should be able to runredis-cli -h [private IP of redis-cli VM] -p 6379
. (But when I tried this, I couldn't reach theredis-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
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.
-
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 port8000
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, rundocker-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 useping
; i.e.,ping docker.local
. -
The information on PORTS in this output to the
docker ps
command shows that the port _inside_ the container is8000
and it is mapped to port8001
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, thendocker port webserver 80
will show you0.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 forNetworkSettings
. (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.
... 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
Here are the steps, cobbled together from trial-and-error, Docker docs, and others.
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
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.
$ docker-machine ssh sandbox
Welcome to Ubuntu 15.10 (GNU/Linux 4.2.0-16-generic x86_64)
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>
...
See Generating SSH keys in GitHub Help.
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.
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.
-
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
```
-
Log into the running container with
docker exec -it [CONTAINER NAME | CONTAINER ID] bash
$ docker exec -it 911df bash
-
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 >
#Dockerfile
FROM debian
RUN apt-get update
RUN apt-get install -y postgresql
ADD README.md /.
docker build -t orangesnap/demo .
docker run --rm -i -t billmills/demo /bin/bash