Containerizing with Docker

Sascha Kala
3 min readMar 22, 2021

--

A beginner’s overview

This week I learned a bit about containerizing, why it’s useful, and how to begin employing it.

Containerizing: consolidating multiple elements of an app into one package so that it’s more efficient to deploy.

Containerizing an app makes it more efficient to install, configure, deploy, and scale because all of the dependencies, source code, settings, libraries, etc. are located in a single place.

One method of containerizing an application is by using a Docker image. A Docker image is a read-only template that contains instructions for creating a container that can run on Docker. The image itself is made up of a collection of files (called layers) that bundles together all of the app essentials (dependencies, source code, settings, and the like). When the image is run, for example with the generic command:

docker run your-image-name

Docker will run the image, thereby creating a container and making the application accessible either locally or at the designated endpoint.

Building an image

One very simple way to build an image is using a Dockerfile.

A screenshot of a text editor window showing an example Dockerfile
example Dockerfile

A Dockerfile contains the specs for creating a docker image.

FROM specifies the parent or base image that serves as the foundation on which all the other layers are built.

WORKDIR sets the working directory for commands that follow.

ENV is a way to pass configuration values to your project thus providing default environmental variables.

COPY copies files/directories from a specific location.

RUN installs apps and packages required for the container.

CMD specifies the default command to execute whenever a container is started from this image.

Before an image can be run (which creates the container), as mentioned above, it needs to first be built. The generic build command is as follows:

docker build -t your-image-name .

The -t flag sets the image name and tag (in ‘name:tag’ format), and the . argument instructs Docker to build the image in the same directory as the Dockerfile.

A Dockerfile is useful for creating stand-alone containers, but realistically each container should serve only one purpose meaning that most web applications are likely to need multiple containers to deploy.

Docker Compose

A useful way to build multiple containers at once is by using Docker Compose. Using compose allows for the application and its services to be started with one command.

Docker compose uses a YAML file where both the application and its services are defined and then run with the simple command

docker-compose up

Resources

--

--

Sascha Kala
Sascha Kala

Written by Sascha Kala

Software engineer and they/them tech femme; artist // digital creator