Docker is an open-source container management platform that automates the process of creating, managing, and deploying applications in containers. With the platform, your application is packaged in a self-contained, portable unit that provides all the benefits of an application server, but with a lighter footprint and faster deployment times.
Docker images are, ultimately, just another way of packaging code that can be shared and versioned. This guide will show you how to create a Docker image for an application. We'll also cover a few additional features to help you get the most from your images.
Packagecloud is a cloud-based service for distributing software packages in a unified, reliable, and scalable way, without owning any infrastructure. You can keep all of the packages and images that need to be distributed across your organization's machines in one repo. Check out our free trial to get your packages set up quickly.
Docker is, at its core, a platform for building and running applications in containers. The platform is built on the open-source engine called LXC, which allows you to run applications in virtual machines. Docker extends that concept to embrace the flexibility of running applications in containers.
What Is A Docker Image?
An image is a file that contains all the code and dependencies needed to run an application server. The image can then be used to deploy the code to a remote server, without the need for additional external dependencies. The full source code of an application is bundled into a single file that can be distributed to numerous computers.
What’s In A Docker Image?
A Docker image is a representation of a container. It includes the base operating system and all the applications that live inside the container. What’s important to note here is that the image isn’t the actual application code; rather, it’s a layer that contains the code and the operating system that the application will run on. On top of the image is the application code. The image contains everything necessary to run an entire application, including things like the database and web server code.
The image is then placed into a container that is run on a server. The operating system launches the container, which runs the application code that lives inside the image. When the application is done, it stops running. All the application data is then disconnected from the container and discarded.
The Dockerfile is the heart of any image. It’s a plain text file with a set of instructions that tells the Docker engine how to build an image. The instructions are organized in sections, each of which defines a task necessary to build the image. The first section defines the base layer of the image; it defines things like the operating system, the programming language, and the package manager. The second section defines the build environment; this is where any build steps will be performed. The third section defines the image itself. Below are a few of the most common commands you’ll find in a Dockerfile.
- FROM — This command tells the platform what image base to use as the foundation of your image. For example, if you have a container image that has already been built, you can use that image as the base of your new image.
- COPY — COPY allows you to copy files into your image.
- WORKDIR — WORKDIR allows you to list the files within your image. It also allows you to create new directories within your image.
- RUN — RUN is used to tell Docker to run a command in the container. For example, if you want to run the "apt-get update" command in a container, you can use the following:
Basic Dockerfile Example
For example, a Dockerfile for an application that runs on Node.js would look something like this:
FROM node:8 RUN bash -c 'npm install && npm run build'
RUN bash -c 'node index.js'
The FROM command instructs Docker to pull the code from a remote source and use it to create a new image. The FROM node command specifies the operating system and version of Node.js that the image should run on.
The RUN command then issues all of the commands needed to run the application code. The command uses Bash to run the npm installation and then issues the Node.js command to run the application.
Packagecloud is a cloud-based service for distributing software packages to your environment from one repo. This allows you to distribute your packages efficiently, reliably, and securely. Check out our free trial to get your packages set up quickly.
How to Create a Docker Image For an Application From a Dockerfile
The first step is to create a Dockerfile. The steps to do so are as follows:
If you have an application called my-app that lives in a directory called /my-app on the local machine, the following Dockerfile would create an image called my-app:
FROM ubuntu:latest WORKDIR /my-app COPY . /my-app RUN apt-get update RUN apt-get install -y --no-install-recommends python RUN rm -rf /var/lib/apt/archives CMD python app.py
- The instructions start by creating a directory called /my-app on the local machine. This creates the basis for the image, and any subsequent instructions will be applied to this directory.
- Next, we specify that the directory should be mounted as a volume (that is, attached to the container) so we can write to the local machine.
- The Dockerfile next copies the source code from the current directory into the image. We use the WORKDIR instruction to specify that this directory should be the root of the image.
- Next, the COPY command copies all the source code into the image.
- The RUN instruction runs the apt-get update command, which updates the local package cache.
- Next, we run the apt-get install -y command to install all dependencies.
- Finally, we run the rm -rf command to remove the directory from the system.
How to Create a Docker Image For an Application From a Template
If you prefer to start from a template, you can use an existing Dockerfile as the base image. The main advantage to this is you can use a single Dockerfile for multiple applications with different environments (development, QA, staging, production). This is helpful when developing multiple applications using a single docker image as well as when deploying different versions of your application to different environments (dev, QA, staging, prod).
How to Create a Docker Image For an Application That Uses Environment Variables
Another approach to creating images that you can use is to use environment variables. This approach works best when your application requires key/value pair data to run. For example, you may have an application that requires a database connection string that you can use to connect to a database. Since you won’t know the data until runtime, you can create a Dockerfile that uses environment variables.
How to Create a Docker Image From an Existing Container
Finally, you can create an image from an existing container. You can use the Docker Command Line Interface (CLI) to pull an existing container image into your local registry. From there, you can use the build command to create an image based on the existing container.
Docker is an incredibly powerful tool for automating the creation, management, and deployment of applications. The primary benefit of using the platform is that it takes the heavy lifting out of creating, managing, and deploying applications. You don’t need to install different versions of software, manage dependencies, or worry about your applications running on the right hardware. Docker helps you create reproducible builds and version your applications across multiple environments.
Packagecloud is a cloud-based service for distributing software packages to your machines and environments. Packagecloud enables users to store all of the packages that are required by their organization, regardless of OS or programming language, and repeatedly distribute them to their destination machines. This enables users to efficiently, reliably, and securely set up and update machines without owning any of the infrastructure that is typically required to do that.
Check out the Packagecloud free trial to see how easy it is to distribute packages throughout your entire organization. Never worry about the scaling, consistency, or security of your packages again. Sign up for your Packagecloud free trial here.