Setting up the prerequisites
Listing images on the host
$ docker images
If you do not have ubuntu:14.04 images, please download it using following commands.
$ docker pull ubuntu:14.0
Verify the ubuntu:14.0 image by running the following commands.
$ docker run -t -i -d ubuntu:14.04 /bin/bash
Method 1: You can update a container created from an image and commit the results to an image.
We will use training/sinatra image pretty useful for creating the images for Method1.
To update an image you first need to create a container from the image you’d like to update.
$ docker run -t -i training/sinatra /bin/bash
Inside our running container first let’s update apt, httpd and install git.
$ root@0b2616b0e5a8:/# apt-get update
$ root@0b2616b0e5a8:/# apt-get install -y git
$ root@0b2616b0e5a8:/# apt-get install -y httpd
Once this has completed let’s exit our container using the exit command. Now you have a container with the change you want to make. You can then commit a copy of this container to an image using the docker commit command.
$ docker commit -m "Added json gem" -a "Rajesh Kumar" 0b2616b0e5a8 scmgalaxy/sinatra:v2
Here you’ve used the docker commit command. You’ve specified two flags: -m and -a. The -m flag allows us to specify a commit message, much like you would with a commit on a version control system. The -a flag allows us to specify an author for our update.
You’ve also specified the container you want to create this new image from, 0b2616b0e5a8 (the ID you recorded earlier) and you’ve specified a target for the image:
It consists of a new user, scmgalaxy, that you’re writing this image to. You’ve also specified the name of the image sinatra. Finally you’re specifying a tag for the image: v2.
You can then look at our new scmgalaxy/sinatra image using the docker images command.
$ docker images
To use our new image to create a container you can then:
$ docker run -t -i ouruser/sinatra:v2 /bin/bash
Method 2: You can use a Dockerfile to specify instructions to create an image.
Using the docker commit command is a pretty simple way of extending an image but it’s a bit cumbersome and it’s not easy to share a development process for images amongst a team. Instead you can use a new command, docker build, to build new images from scratch.
To do this you create a Dockerfile that contains a set of instructions that tell Docker how to build our image.
First, create a directory and a Dockerfile.
$ mkdir sinatra $ cd sinatra $ touch Dockerfile
Each instruction creates a new layer of the image. Try a simple example now for building your own Sinatra image for your fictitious development team.
FROM ubuntu:14.04 MAINTAINER Rajesh Kumar "firstname.lastname@example.org" RUN apt-get update && apt-get install -y git httpd
Examine what your Dockerfile does. Each instruction prefixes a statement and is capitalized.
The first instruction FROM tells Docker what the source of our image is, in this case you’re basing our new image on an Ubuntu 14.04 image. The instruction uses the MAINTAINER instruction to specify who maintains the new image.
Lastly, you’ve specified two RUN git and httpd
Now let’s take our Dockerfile and use the docker build command to build an image.
$ docker build -t scmgalaxy/sinatra:v3 ./
You’ve specified our docker build command and used the -t flag to identify our new image as belonging to the user scmgalaxy, the repository name sinatra and given it the tag v2.
$ docker run -t -i scmgalaxy/sinatra:v2 /bin/bash/
Setting tags on an image
$ docker tag 5db5f8471261 scmgalaxy/sinatra:dev
The docker tag command takes the ID of the image, here 5db5f8471261, and our user name, the repository name and the new tag.
$ docker images scmgalaxy/sinatra
Remove an image from the host
$ docker rmi training/sinatra
- Docker Tutorials: Volume – Deep Dive - October 3, 2022
- Docker Tutorials: Remove all Docker Containers, all Images, all Volumes, and all Networks - October 3, 2022
- Docker Tutorials: How to configure HTTP Proxy with Docker? - October 3, 2022