Oleksiy Mark - Fotolia

Tip

Dockerfile commands for containerizing applications

Admins can use Dockerfiles to containerize their applications and ease deployment. They can implement several commands to help them do so successfully.

IT administrators can manually create Docker containers with a few commands at a terminal prompt, but they should consider memory and storage requirements while doing so. Once admins get beyond creating basic containers, they should use a Dockerfile to define those commands.

Dockerfiles provide efficient application development, decreased resource use and faster deployment compared to VMs. Admins can make the process of creating a container repeatable through Dockerfiles, which also facilitates automation.

After using Dockerfile, the next step admins can take involves using Docker Compose and a YAML file to describe multiple containers.

Common Dockerfile commands

Each Dockerfile uses a series of commands to configure an image with a specific set of capabilities. The most common commands include:

  • To copy files from a source on the host to the container's filesystem at a specific destination, use the command: ADD
  • To execute a command within the container, use: CMD
  • Set the default application to be used each time a container is created with the image with the command: ENTRYPOINT
  • You can set environment variables the same way admins would in a startup script with: ENV
  • Expose a specific port to enable networking to and from the container using: EXPOSE
  • Define the base image from which to initiate the build process with the command: FROM
  • Run the central executing directive for Dockerfiles with: RUN
  • Set the user ID to own the container when executed with: USER
  • Enable access from the container to a path name on the host machine with: VOLUME
  • Set the working directory in which to execute the command -- defined with CMD -- with: WORKDIR

Dockerfiles frequently execute installing specific packages the application requires. For Debian-based Linux distributions, such as Ubuntu, admins use the apt-get command. For example, the following command creates an image based on Ubuntu with the basic software-build tools necessary to compile applications:

FROM ubuntu
RUN apt-get update && apt-get install -y \
    build-essential \
    gcc \
    curl \
    make

Admins can build an image on Linux or Windows. For either OS, admins can easily change the directory to where their Dockerfiles reside and issue the following command:

docker build --tag="build-essential:dockerfile" .

The period at the end of the command tells the system to use the current path to find the Dockerfile instructions.

Containerize an application

Many admins use Docker to build a containerized version of an application. For example, consider a Dockerfile that can turn a Python application into a container. In Python 3, the standard library provides an HTTP server which admins can start with a single line of code:

python -m http.server 7000

A few simple Dockerfile lines enable the system to run the one-line web server inside a container image.

FROM python:latest
COPY index.html /
EXPOSE 7000
CMD python -m http.server 7000

To build the image, an admin uses the command:

docker build -t my-python-web .

The following command runs this image:

run --rm -it --name my-python-instance -p 80:7000 my-python-image

The build command assumes a file named index.html resides in the same directory as the Dockerfile and contains appropriate HTML to display in a browser. For testing purposes, admins can use a simple HTML file:

<!doctype html>
<html>
  <head>
    <title>This is the title of our webpage!</title>
  </head>
  <body>
    <p>
      This is our sample index.html file. 
    </p>
  </body>
</html>

Things to consider

Admins concerned about memory usage must pay attention to how they build their containers. The previous example uses a Python image, which is 932 MB in size. If admins spin up several of these containers, this equates to almost 1 GB of memory per instance. Admins with memory limitations can use a more austere base image such as Alpine. In addition, if admins' applications must permanently store data, those apps require access to either a local or network-based storage volume.

The other big piece of the Docker puzzle is orchestration. Kubernetes is currently the frontrunner for container management, although Docker supports its own orchestration tool called Docker Swarm. Still, admins should get up to speed with Kubernetes because all major cloud vendors currently support it more than any other container orchestration system.

Dig Deeper on Containers and virtualization

Software Quality
App Architecture
Cloud Computing
SearchAWS
TheServerSide.com
Data Center
Close