Book Image

Beginning DevOps with Docker

By : Joseph Muli
5 (1)
Book Image

Beginning DevOps with Docker

5 (1)
By: Joseph Muli

Overview of this book

Making sure that your application runs across different systems as intended is quickly becoming a standard development requirement. With Docker, you can ensure that what you build will behave the way you expect it to, regardless of where it's deployed. By guiding you through Docker from start to finish (from installation, to the Docker Registry, all the way through to working with Docker Swarms), we’ll equip you with the skills you need to migrate your workflow to Docker with complete confidence.
Table of Contents (7 chapters)

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:

Note

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

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:

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).