Technology / DevOps

How to Build Docker Images

How to Build Docker Images
Follow us
Published on December 6, 2022

Docker has quickly become a pivotal tool in the world of DevOps. However, how it works—or even its purpose—is often misunderstood. In this article, we will clear up some of the confusion by discussing what Docker is, what problem it solves, and how to create a Docker container. 

What is Docker?

Docker is a software application that uses OS-level virtualization to “containerize” applications. This enables organizations to deploy applications easily anywhere, including the cloud.

At a high level, OS-level virtualization means that the kernel provides multiple, independent user space systems. These independent user space systems are then used to run the containerized application independent of the rest of the hardware.

This containerized application can run on several different operating systems, chiefly Linux, Windows 10 and above, and OS X. This means you can create a web application for example, put it into a container, and send it to anybody and the person will be able to run it. 

What Problems Does Docker Solve?

Docker solves problems that have plagued the software industry for years. For example, let’s say you are a developer who needs to share an application with another developer. Before Docker, you would send it to the developer in some packaged format, who would then run the application. However, the developer would run into problems, for example, if he or she didn’t have the right version of Java. Or the developer would experience a dependency error, conflicts, or a multitude of esoteric issues.

That’s where Docker comes into play. Now developers can simply containerize the image and send it to whomever they wish. It is completely OS agnostic and will run on any other developer’s computer.

Not only does Docker facilitate collaboration, but it also makes running applications in a production environment far more reliable. Since the Docker container is completely isolated, we know it will run the exact same whether it is in development, QA, or production environment. 

How Do You Use Docker?

Docker is used by creating a Dockerfile. Docker containers can also be created purely from a command line, but we will focus on Dockerfiles here. 

A Dockerfile is a basic file with a set of commands that serve as a “blueprint” for the image. Let’s look at an example of packing an angular project. (Note: This is theoretical only and won’t run unless you have an existing angular application with a package.json.)

FROM node:12-alpine
WORKDIR /app
COPY package*.json /app/
RUN npm ci
CMD ["node", “—version"]
EXPOSE 8080
RUN ng build — —output-path=./dist

The first thing to notice is the line FROM. Every Dockerfile will start with the FROM command. That’s because all images must be based off an existing image. The next line sites the directory all subsequent commands should run in. It is like create a local context. 

In the example above we are creating a frontend node application. So, we copy all the dependencies into the WORKDIR, and run NPM CI, which installs all the dependencies into the container. Next, we use the RUN command to verify the version of node. After that, we EXPOSE port 8080, which allows the container to listen to TCP/HTTP traffic. Lastly, we build an angular project and output it to a /.dist folder.

How Do I Run a Dockerfile?

Now that we have gone over the anatomy of a Dockerfile, it’s time to run one. Make sure that Docker is installed on your computer. In my experience, Docker for Desktop is the best way to run and manage docker containers.

Once you have Docker installed, run a Dockerfile with this command:

docker build -t my-application.

Docker build is the command used to execute the docker build.

The -t my-application is optional. It tags the image to make it easier to identify. The period at the end is important—it provides the location of the Dockerfile. If it is just a period, that means the Dockerfile is in the current directory you are running in. After executing this command, you should see a screen that looks like this: This provides all the detail of what the Dockerbuild is doing, step by step. In fact, you can recreate the Dockerfile just by looking at the steps.

Next, run the command docker images, and it will display all the images on your machine.

Notice it says "my-server," which is the same name we used to tag the file. If you have Docker Desktop installed, you can view your images that way as well.

Finally, run the container using the following command: docker run -p 3000:8080 my-server.

The command is saying to run docker with an external port of 3000, an internal port of 8080, and its name.

Final Thoughts

Docker will seem abstract and complicated at first. I would recommend perusing Docker Hub and pulling down images. Also, gain familiarity with the Docker CLI

If you want an in-depth look at how to build a Docker image, check out this online training from CBT, How to Build Docker Images Online Training. Not a CBT Nuggets subscriber? Sign up for a free 7-day free trial to check out this course and others.


Download

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.


Don't miss out!Get great content
delivered to your inbox.

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.

Recommended Articles

Get CBT Nuggets IT training news and resources

I have read and understood the privacy policy and am able to consent to it.

© 2024 CBT Nuggets. All rights reserved.Terms | Privacy Policy | Accessibility | Sitemap | 2850 Crescent Avenue, Eugene, OR 97408 | 541-284-5522