Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning DevOps with Docker

You're reading from   Beginning DevOps with Docker Automate the deployment of your environment with the power of the Docker toolchain

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781789532401
Length 96 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Joseph Muli Joseph Muli
Author Profile Icon Joseph Muli
Joseph Muli
Arrow right icon
View More author details
Toc

Building Images

Before we begin building images, let's understand the context first. An image is a standalone package that can run an application or allocated service. Images are built through Dockerfiles, which are templates that define how images are to be built.

A container is defined as a runtime instance or version of an image. Note this will run on your computer or the host as a completely isolated environment, which makes it disposable and viable for tasks such as testing.

With the Dockerfiles ready, let's get to the Python Dockerfile directory and build the image.

docker build

The command to build images is as follows:

docker build -t <image-name> <relative location of the Dockerfile>

-t stands for the tag. The <image-name> can include the specific tag, say, latest. It is advised that you do it this way: always tagging the image.

The relative location of the Dockerfile here would be a dot (.) to mean that the Dockerfile is on the same level as the rest of the code; that is, it is at the root level of the project. Otherwise, you would enter the directory the Dockerfile is in.

If, for example, it is in the Docker folder, you would have docker build -t <image-name> docker, or if it is in a folder higher than the root directory, you would have two dots. Two levels higher would be three dots in place of the one dot.

Note

The output on the terminal and compare to the steps written on the Dockerfiles. You may want to have two or more Dockerfiles to configure different situations, say, a Dockerfile to build a production-ready app and another one for testing. Whatever reason you may have, Docker has the solution.

The default Dockerfile is, yes, Dockerfile. Any additional one by best practices is named Dockerfile.<name>,say, Dockerfile.dev.

To build an image using a Dockerfile aside from the default one, run the following: docker build -f Dockerfile.<name> -t <image-name> <relative location of the Dockerfile>

Note

If you rebuild the image with a change to the Dockerfile, without specifying a different tag, a new image will be built and the previous image is named <none>.

The docker build command has several options that you can see for yourself by running docker build --help. Tagging images with names such as latest is also used for versioning. We will talk more on this in the Topic F.

To build the image, run the following command in the Python workspace:

>$ docker build -t python-docker .

Note

The trailing dot is an important part of the syntax here:

docker build

Note

The trailing dot is an important part of the syntax here:

docker build

Open the JavaScript directory and build the JavaScript image as follows:

>$ docker build -t js-docker .

Running the commands will outline the four steps based on the four lines of commands in the Dockerfile.

Running docker images lists the two images you have created and any other image you had pulled before.

Removing Docker Images

The docker rmi <image-id> command is used to delete an image. Let me remind you that the image ID can be found by running the docker images command.

To delete the images that are non-tagged (assumed not to be relevant), knowledge of bash scripting comes in handy. Use the following command:

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

This simply searches for images with <none> within their row of the docker images command and returns the image IDs that are in the third column:

Removing Docker Images

Activity 4 — Utilizing the Docker Image

Ensure you have the Docker CLI running by typing docker on your terminal.

To get you conversant with running containers out of images.

You have been asked to build an image from the Dockerfile written in Activity C. Stop the running container, delete the image, and rebuild it using a different name.

  1. Is Docker up and running? Type docker on the terminal or command-line application.
  2. Open the JavaScript example directory.
  3. Run docker build -t <choose a name> (observe the steps and take note of the result).
  4. Run docker run <the-name-you-chose>.
  5. Run docker stop <container ID>.
  6. Run docker rmi <add the image ID here>.
  7. Run docker build -t <choose new name>.
  8. Run docker ps (note the result; the old image should not exist).
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime