A Docker Image is the blue print of a container which will run an application. A Docker Image contains an applications binaries, dependencies and the commands required to run it. A docker image is built from a Dockerfile. You can then run a docker image to instantiate as many containers as you want.

In Official Hub you can click "Explore" to view Official Docker Images, which are created and maintained by Docker.

View Images

To see all images on your local run the following:

> docker images

Response:

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE

nginx                      latest              8b89e48b5f15        7 days ago          109MB

hello-world                latest              2cb0d9787c4d        12 days ago         1.85kB

You can inspect an existing image and see all it's meta-data.

> docker image inspect node

collapse
Pull Images

If you do not know the exact image to pull you can search for it with the search command. You can also find images at Docker Hub or Docker Store.

docker search {image}:{tag}

> docker search node

Once you have the image you want, you can pull it to your local machine. This is not required, if a Dockerfile can't find an image locally, it will automatically pull it. Pulling the image first is useful if you want to download it now so you do not have to wait later when you want to run your image.

docker pull {image}

> docker pull node

If you go to an images Docker Hub Repository, for example Docker Hub - Node Repository, and click on the 'Tags' tab you will find a list of tags you can use to select the version you want.

docker pull {image}:{tag}

> docker pull node:10.10.0-alpine

It best to be specific with your tag version with production containers. You do not want latest as it will automatically update and may cause issues.

collapse
Build Images

To build an image, you need to write a Dockerfile. In your command prompt browse to the folder of your Dockerfile and run the following:

> docker build .

The "." is because we are in the folder of the Dockerfile if not that is where you provide the path to the Dockerfile eg:

> docker build ../

> docker build myFolder

You may notice that when you build an image, it does not have a repsoitory name or tag. You can name your images and tag them with -t {imageName}:{imageTag}

> docker build -t imageName:imageTag .

Take note of the first line as you run the build.

Sending build context to Docker daemon 114.7kB

To understand Docker build context you need to understand that Docker is a client-server application:

  • Docker Client: This is where your cli is so that you can give commands to the Docker server.
  • Docker Server (daemon): A Docker server can be on your local machine but it could also be on a remote machine. The server runs commands like build.
  • Build Context: The build context is the files sent to the server to build your image. The Docker client will archive the build context files into a tar and upload it to the server. By Default all files are in the directory will be taken therefore to define the build context you need a .dockerignore file.
collapse
Tag Images

When you build the image you can tag it:

> docker build -t imageName:imageTag .

Otherwise you can tag it afterwards with "docker tag {imageId} {imageName}:{imageTag}"

> docker tag cec9 imageName:imageTag

collapse
Remove Image

remove a specific image

Remove an image using it's name or ID. Note: If the image has be built it wont delete. Type "docker ps -a" to find the container based on the image and delete it first.

> docker rmi node

> docker rmi 137948e8a084

If there are multiple images with the same name and Id you have to specify the Tag.

> docker rmi nginx:alpine

remove all unused images

Removing all images one by one can be difficult, so there is one command to prune all unused images.

The following will remove all dangling images(not tagged and not referenced by any container).

> docker image prune

The following will remove all images where there are no containers.

> docker image prune -a

collapse
Push Image to Repo

In order to push a image to your repo you need to login to docker and tag your image, if it has not been tagged already.

Note: non official images must be pushed to your a repository with {repo}/. If you dont tag it will default to "latest"

{repo}/{imageName}:{imageTag}.

> docker login

> docker tag cec9 brandon/myapp:v1

> docker push brandon/myapp:v1

collapse
What else can I do with images?

The below line will give you a list of commands you can use with images:

> docker image --help

collapse

Check out these links for more info:

My Docker Samples