Docker is a platform that allows developers to easily create, deploy, and run applications in containers. Containers are lightweight, portable, and self-sufficient environments that encapsulate an application and all its dependencies.
Docker enables applications to run in containers that can be used on any system: a developer’s laptop, systems on premises, or in the cloud.
This guide serves as an introduction to Docker. Docker is a vast ecosystem that can take many forms. If you are interested in exploring/learning more about Docker, check out the tutorials linked in the Resources section at the bottom of this page.
To get started with Docker, you need to install it on your machine. The installation process varies depending on your operating system. You can find detailed instructions for installing Docker on various platforms on the Docker website here.
A Dockerfile is a text file that contains instructions for building a Docker image. An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
To create a Dockerfile, you need to define the base image, copy the application code into the image, and configure the image to run the application. You can also set environment variables, specify the working directory, and expose ports.
Here is an example Dockerfile for a C application that uses a Makefile to compile:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y gcc make
WORKDIR /app
COPY . .
RUN make
CMD ["./app"]
Let us take a closer look at the Dockerfile:
FROM
command specifies the base image for the Docker image, which in this case is the latest version of Ubuntu.RUN
command runs the specified commands inside the Docker container. In this case, it updates the package list and installs gcc
and make
.WORKDIR
command sets the working directory for the Docker container to /app
.
/app
directory in the Docker container represents the directory where the application code is stored. By setting the working directory to /app
, the Docker container is able to find and run the application code inside the container.COPY
command copies the contents of the current directory (the directory containing the Dockerfile) into the /app
directory in the Docker container.RUN
command runs the make
command inside the Docker container to compile the C application.CMD
command sets the default command to run when the Docker container is started, which in this case is to run the app
executable.