Docker
Install
create /etc/apt/sources.list.d/docker.list
with this content:
deb https://get.docker.io/ubuntu docker main
Then run:
curl -s https://get.docker.io/gpg | sudo apt-key add - sudo apt-get update sudo apt-get install lxc-docker
confirm:
sudo docker info
update firewall
The FORWARD chain should be changed from DROP to ACCEPT
Containers
docker run -i -t ubuntu /bin/bash # create a container docker run --name bob_the_container -i -t ubuntu /bin/bash # create a container with specified name docker ps # show running containers docker ps -a # show all containers docker start bob_the_container # start a stopped container docker attach bob_the_container # attach to a container # NOTE: hit Enter to get a prompt # create daemonized container docker run --name daemon_dave -d ubuntu /bin/sh -c \ "while true; do echo hello world; sleep 1; done" # view terminal output inside the container docker logs daemon_dave docker logs -f daemon_dave # like tail -f docker logs -t daemon_dave # print timestamps docker top daemon_dave # see what's happening inside the container # run a process inside the container docker exec -d daemon_dave touch /etc/config_file # background docker exec -t -i daemon_dave /bin/bash # interactive docker stop daemon_dave # stop a daemonized container # automatic container restarts docker run --restart=always --name daemon_dave -d ubuntu \ /bin/sh -c "while true; do echo hello world; sleep 1; done" # might be better to use this flag --restart=on-failure:5 # max of 5 restarts docker inspect daemon_dave # get container details # get IP address docker inspect --format '{{ .NetworkSettings.IPAddress }}' daemon_dave docker rm 80430f8d0921 # delete a stopped container
Images
Each image belongs to a repository. Repositories are either maintained by Docker, Inc. or by users. Official Docker repositories have a single name like ubuntu
or mysql
, while user-contributed repositories include a username, e.g. jamtur01/puppet
. All public repositories are generally hosted on Docker Hub
The ubuntu
repository contains images for each version. By default you get the image tagged latest
. You could also specify ubuntu:12.04
or ubuntu:precise
for version 12.04.
Docker images are read-only filesystems that get layered on top of each other in a union mount to form a container's filesystem. A read-write layer is always available on top.
docker images # list all local docker images docker images ubuntu # list only ubuntu-related images docker pull ubuntu:12.04 # download image without immediately running it docker inspect ubuntu:12.04 # show properties of a specific image docker search puppet # search Docker Hub
Docker Hub Account
First create an account on the web, then run
docker login
and enter your credentials. The credentials will be stored on your machine at $HOME/.dockercfg
Build an image with a Dockerfile
- create a directory with a
Dockerfile
in it:
$ mkdir static_web $ cd static_web $ touch Dockerfile
- edit the
Dockerfile
to look like this:
# Version: 0.0.1 FROM ubuntu: 14.04 MAINTAINER James Turnbull "james@example.com" RUN apt-get update RUN apt-get install -y nginx RUN echo 'Hi, I am in your container' > /usr/share/nginx/html/index.html EXPOSE 80
- build the image:
sudo docker build -t= "jamtur01/static_web:v1" .
Build cache gotcha
Each step of the build is cached as a distinct image layer. As long as your Dockerfile doesn't change, these cached images get re-used for the build. You can use an ENV
variable to flush the cache at a particular step:
FROM ubuntu:14.04 MAINTAINER James Turnbull "james@example.com" ENV REFRESHED_AT 2014-07-01 RUN apt-get -qq update
Manually change the date in the ENV
line to get the latest updates.