Build your own images
Docker images are the basis of containers. Each time you’ve used docker run
you told it which image you wanted. In the previous sections of the guide youused Docker images that already exist, for example the ubuntu
image and thetraining/webapp
image.
You also discovered that Docker stores downloaded images on the Docker host. Ifan image isn’t already present on the host then it’ll be downloaded from aregistry: by default the Docker Hub Registry.
In this section you’re going to explore Docker images a bit moreincluding:
- Managing and working with images locally on your Docker host.
- Creating basic images.
- Uploading images to Docker Hub Registry.
Listing images on the host
Let’s start with listing the images you have locally on our host. You cando this using the docker images
command like so:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 1d073211c498 3 days ago 187.9 MB
busybox latest 2c5ac3f849df 5 days ago 1.113 MB
training/webapp latest 54bb4e8718e8 5 months ago 348.7 MB
You can see the images you’ve previously used in the user guide.Each has been downloaded from Docker Hub when youlaunched a container using that image. When you list images, you get three crucial pieces of information in the listing.
- What repository they came from, for example
ubuntu
. - The tags for each image, for example
14.04
. - The image ID of each image.
Tip:You can use a third-party dockviz toolor the Image layers site to display
visualizations of image data.
A repository potentially holds multiple variants of an image. In the case ofour ubuntu
image you can see multiple variants covering Ubuntu 10.04, 12.04,12.10, 13.04, 13.10 and 14.04. Each variant is identified by a tag and you canrefer to a tagged image like so:
ubuntu:14.04
So when you run a container you refer to a tagged image like so:
$ docker run -t -i ubuntu:14.04 /bin/bash
If instead you wanted to run an Ubuntu 12.04 image you’d use:
$ docker run -t -i ubuntu:12.04 /bin/bash
If you don’t specify a variant, for example you just use ubuntu
, then Dockerwill default to using the ubuntu:latest
image.
Tip:You recommend you always use a specific tagged image, for example
ubuntu:12.04
. That way you always know exactly what variant of an image isbeing used.
Getting a new image
So how do you get new images? Well Docker will automatically download any imageyou use that isn’t already present on the Docker host. But this can potentiallyadd some time to the launch of a container. If you want to pre-load an image youcan download it using the docker pull
command. Suppose you’d like todownload the centos
image.
$ docker pull centos
Pulling repository centos
b7de3133ff98: Pulling dependent layers
5cc9e91966f7: Pulling fs layer
511136ea3c5a: Download complete
ef52fb1fe610: Download complete
. . .
Status: Downloaded newer image for centos
You can see that each layer of the image has been pulled down and now youcan run a container from this image and you won’t have to wait todownload the image.
$ docker run -t -i centos /bin/bash
bash-4.1#
Finding images
One of the features of Docker is that a lot of people have created Dockerimages for a variety of purposes. Many of these have been uploaded toDocker Hub. You can search these images on theDocker Hub website.