Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Docker Tutorials: Image – Creating Docker Image Step by Step

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:scmgalaxy/sinatra:v2

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 "rajesh@scmgalaxy.com"
RUN apt-get update && apt-get install -y git httpd

Examine what your Dockerfile does. Each instruction prefixes a statement and is capitalized.

INSTRUCTION statement

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
Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x