Software DevelopmentLearn How To Use Docker Images Part 1

Learn How To Use Docker Images Part 1

In the ‘Docker overview tutorial’ we discussed the main components that make up the Docker platform and how they work together. In that tutorial, we noted that images are the blueprints that we use to create containers. In the learn ‘how to set up Docker tutorial’ we demonstrated how to set up Docker on the ubuntu Xenial 16.04, Windows 10, 8 and 7. In this tutorial, we will build on the material presented in the two articles identified previously to demonstrate practical aspects of Docker images. In this tutorial, we assume you have an idea of what is Docker and you have properly set up Docker. For a review of concepts, please refer to the two tutorials mentioned earlier.

In the ‘learn how to set up Docker tutorial’ we used the sudo docker run hello-world command to confirm that our Docker set up is okay. Run the command and make sure you get the Docker is working message, otherwise, you need to set up your environment correctly.
docker-check
The command we used to test our Docker set up downloads the hello-world image from the Docker hub and runs it. Docker hub is a cloud-based central repository that facilitates the distribution of Docker images. Whenever you try to run an image docker searches for it on your host and when it is not found it is downloaded from the Docker hub. This is what happens when we run the hello-world image.

Docker hub has very friendly and easy to use features to support the developer. The first feature that you should be aware of the Docker hub is that as a developer you are able to search and download images that are ready to use. By visiting the Docker hub website at https://hub.docker.com/ you are able to search images and know the process of their creation and maintenance. However, you won’t be able to download the images from the website. To interact with the Docker repository, we will have to use commands available at the terminal.

To search for images on Docker hub you use the search command and pass the name of the image you would like to search. For example, if we are interested in running wordpress, we can search for wordpress images using the command shown below. The command will return a list of images with the name of the image, a brief description, a star rating, and an indicator if the image is official or automated.

sudo docker search wordpress

wordpress-search
There are two types of images available on the Docker hub, official and automated. Official images are developed by Docker while automated images are developed by individuals. Official images get validation and maintenance from Docker. As a developer, to upload your images you are simply required to create an account.

Once you have identified an image that meets your needs you use the pull command to download it. For example, let us download the latest bitnami Docker image. The command shown below will do that

sudo docker pull bitnami/wordpress:latest

download-wordpress
The image will take a while to download depending on your Internet speed. Once your image has been downloaded, you can use docker commands to manage it. To get a list of all images on your system you use this command sudo docker images.
list-images
You will get a list of all images with their ID, when created, and size.

To get the entire image ID pass –no trunc flag to images as shown below.

sudo  docker images –no-trunc

full-id
The images command has several optional flags that you can use to filter the images you want returned. The [REPOSITORY:[TAG] option returns only the images that match the specified repository and tag. If you omit tag images that match, the repository will be returned.

For example to list images from bitnami/wordpress repository we would run the command below. Exact matching tags are used so in the example below no image will be returned for repository bitnami/word.

sudo docker images bitnami/wordpress

To return images from a specific repository and a tag you use the images command as shown below

sudo docker images bitnami/wordpress:latest

To demonstrate exact matching, if we use no image is returned as shown in the output below.
exact
The filter flag –f or –filter gives you more flexible options to restrict the images returned. You specify a filter using the key=value format. The dangling filter returns images that are tagged or untagged. When dangling is set to true images without tags are returned, when set to false images with tags are returned. Use of dangling is shown in the commands below

sudo docker images --filter "dangling=true"
sudo docker images --filter "dangling=false"

dangling
To run our downloaded image we use the run command by giving it a container name to use, the host and container port and the image name. To run our wordpress image, we can use the command shown below.

sudo docker run --name=firstcontainer -p 80:80 -p 443:443 bitnami/wordpress

run-image
In the command we ran above, the name of the container is firstcontainer and we are using ports 80 and 443.

If you no longer need an image you can delete it using the rmi command by passing the name of the image as returned by images command. Before deleting any images make sure there no running containers based on the image you are deleting. For example to delete the hello-world image, we use the command shown below.

sudo docker rmi hello-world

Downloaded images are a good starting point but at times they do not meet all your requirements. When this happens you need to modify your image. There are two ways in which you can modify images:

  • By using Dockerfile that contains instructions that will be used to create an image
  • By updating a container that was created from the image you intend to modify and committing to the image

We will discuss modifying containers in detail in part 2 of this tutorial.

Conclusion:
In this tutorial, we briefly reviewed material that was covered in other tutorials and was required to understand this tutorial. We discussed how to search and download images from Docker hub using docker commands. We discussed other commands important in managing containers. Finally, we identified two ways in which we can modify existing images to suit our needs.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -