Browse Courses

Building and Running Containers

Learn the complete process of building container images using Dockerfiles and creating running containers. Master essential Docker commands for image creation, container management, and registry operations.

This document covers the complete container development workflow, from creating Dockerfiles to building container images and running containers. It explores essential Docker commands for image management, container operations, and registry interactions with practical examples and step-by-step processes.


Container Development Process Overview

The container development process follows a structured workflow that transforms application code into running containers. This process ensures consistent deployment across different environments and enables efficient application distribution.

The development process consists of three primary steps that work together to create functional containerized applications. Each step builds upon the previous one, creating a seamless workflow from code to running container.

Container Development Workflow

The container development process follows a systematic approach:

  1. Create a Dockerfile - Define the container specifications and instructions
  2. Build a container image - Use the Dockerfile to create an executable image
  3. Run a container - Create and start a container instance from the image

This workflow ensures reproducible builds and consistent deployment across different environments. The process separates concerns between defining the container (Dockerfile), creating the artifact (image), and running the application (container).


Dockerfile Fundamentals

A Dockerfile serves as the blueprint for creating container images. It contains a series of instructions that define how to build the container, including the base image, dependencies, and runtime configuration.

Basic Dockerfile Structure

A simple Dockerfile contains essential commands that define the container’s behavior. The most fundamental commands include FROM and CMD, which establish the foundation and execution instructions for the container.

1FROM ubuntu:latest
2CMD echo "hello world"

Key Dockerfile Commands

The Dockerfile uses specific commands to define container behavior:

  • FROM: Defines the base image that serves as the foundation for the container
  • CMD: Specifies the default command to execute when the container starts

These commands work together to create a functional container that can execute applications and services. The FROM command establishes the operating system and base environment, while CMD defines the primary application or service to run.


Building Container Images

The container image build process transforms a Dockerfile into an executable image that can be used to create containers. This process involves parsing the Dockerfile instructions and creating layered filesystem images.

Docker Build Command

The build command creates container images from Dockerfiles using specific syntax and parameters. The command requires a tag for identification and a build context directory.

1docker build -t my-app:v1 .

Build Command Components

The build command consists of several important components:

ComponentPurposeExample
buildPrimary command for image creationdocker build
-tTag flag for naming and versioning-t my-app:v1
repositoryImage name identifiermy-app
versionImage version tagv1
contextBuild context directory. (current directory)

Build Process Output

The build process generates specific output messages that confirm successful image creation:

  • Sending build context to Docker Daemon - Confirms context transfer
  • Successfully built - Confirms image creation with unique ID
  • Successfully tagged my-app - Confirms proper image tagging

These messages provide feedback about the build process and help troubleshoot any issues that may arise during image creation.


Image Management and Verification

After building container images, verification and management become essential for maintaining organized container environments. Docker provides commands to inspect, list, and manage container images.

Listing Container Images

The images command displays all available container images on the local system, providing essential information about each image.

1docker images

Image Information Display

The images command output includes comprehensive information about each container image:

  • Repository - Image name and identifier
  • Tag - Version or variant identifier
  • Image ID - Unique identifier for the image
  • Created - Creation timestamp
  • Size - Image file size

This information helps developers track image versions, manage storage space, and identify specific images for deployment or removal.


Running Containers

Creating running containers from images represents the final step in the container development workflow. The run command instantiates containers from images and starts the application processes.

Container Creation Command

The run command creates and starts a container from a specified image, executing the default command defined in the Dockerfile.

1docker run my-app:v1

Container Execution Output

When the container runs successfully, it executes the command specified in the Dockerfile’s CMD instruction. For the example Dockerfile, the output would be:

1hello world

This output confirms that the container is running correctly and executing the intended application or command.


Container Management Commands

Docker provides various commands for managing containers throughout their lifecycle. These commands enable monitoring, inspection, and control of running containers.

Container Process Inspection

The ps command displays information about running containers, including their status, ports, and resource usage.

1docker ps

Container Information Display

The ps command output provides detailed information about active containers:

  • Container ID - Unique identifier for the container instance
  • Image - Source image used to create the container
  • Command - Currently executing command
  • Created - Container creation timestamp
  • Status - Current container state
  • Ports - Network port mappings
  • Names - Container name identifier

Registry Operations

Container registries serve as centralized repositories for storing and distributing container images. Docker provides commands for interacting with registries to share images across different environments.

Registry Command Overview

Docker registry operations enable image distribution and collaboration through centralized storage systems.

CommandPurposeUsage
pushStore images in configured registrydocker push my-app:v1
pullRetrieve images from configured registrydocker pull ubuntu:latest

Push Command Functionality

The push command uploads local container images to a configured registry, making them available for deployment on other systems. This command requires proper authentication and registry configuration.

Pull Command Functionality

The pull command downloads container images from registries to the local system. This command enables access to pre-built images and shared application components.


Essential Docker Commands Summary

Docker provides a comprehensive set of commands for managing the complete container lifecycle from development to deployment.

Core Docker Commands

CommandFunctionPurpose
buildCreate container imagesBuild images from Dockerfiles
imagesList available imagesDisplay local image inventory
runCreate and start containersExecute containers from images
psList running containersMonitor container status
pushUpload images to registryShare images with others
pullDownload images from registryObtain pre-built images

Command Usage Patterns

These commands follow consistent patterns that make them intuitive to use:

  • build - Used with Dockerfiles to create images
  • run - Used with images to create containers
  • images/ps - Used for inspection and monitoring
  • push/pull - Used for registry operations

Conclusion

Building and running containers involves a systematic process that transforms Dockerfiles into functional container images and running applications. The workflow begins with creating a Dockerfile that defines the container specifications, followed by building the image using the docker build command, and finally running the container using the docker run command.

Key Docker commands enable efficient container management throughout the development lifecycle. The build command creates images from Dockerfiles, the images command provides visibility into available images, and the run command instantiates containers from images. Registry operations using push and pull commands facilitate image sharing and distribution.

Understanding this container development process and mastering essential Docker commands enables developers to create, manage, and deploy containerized applications effectively. The structured approach ensures consistent results across different environments and simplifies application deployment and scaling.


FAQ

The container development process consists of three primary steps:

  1. Create a Dockerfile - Define the container specifications and instructions
  2. Build a container image - Use the Dockerfile to create an executable image
  3. Run a container - Create and start a container instance from the image

A Dockerfile serves as the blueprint for creating container images. It contains a series of instructions that define how to build the container, including the base image, dependencies, and runtime configuration.

  1. CMD
  2. FROM
  3. RUN
  4. COPY
(2) FROM. The FROM command defines the base image that serves as the foundation for the container, establishing the operating system and base environment.

The CMD command specifies the default command to execute when the container starts. It defines the primary application or service that will run when the container is launched.

The correct syntax for building a Docker image with a tag is:

1docker build -t my-app:v1 .

This command uses the build flag, -t for tagging, the repository name and version, and the current directory as build context.

ComponentPurpose
A. build1. Image version tag
B. -t2. Build context directory
C. v13. Tag flag for naming and versioning
D. .4. Primary command for image creation
A-4, B-3, C-1, D-2. The build is the primary command, -t is the tag flag, v1 is the version tag, and . represents the current directory as build context.

The build process generates specific output messages that confirm successful image creation:

  • “Sending build context to Docker Daemon” - Confirms context transfer
  • “Successfully built ” - Confirms image creation with unique ID
  • “Successfully tagged my-app” - Confirms proper image tagging

Use the docker images command to display all available container images on the local system:

1docker images

This command shows repository, tag, image ID, creation date, and image size for each image.

The docker run command only creates a container but does not start it automatically.

False. The docker run command creates and starts a container from a specified image, executing the default command defined in the Dockerfile. It both creates and runs the container in a single operation.

The docker ps command displays detailed information about active containers including:

  • Container ID (unique identifier for the container instance)
  • Image (source image used to create the container)
  • Command (currently executing command)
  • Created (container creation timestamp)
  • Status (current container state)
  • Ports (network port mappings)
  • Names (container name identifier)

  1. docker pull
  2. docker push
  3. docker upload
  4. docker send
(2) docker push. The push command uploads local container images to a configured registry, making them available for deployment on other systems.

The docker push command uploads local container images to a configured registry, making them available for others to use. The docker pull command downloads container images from registries to the local system, enabling access to pre-built images and shared application components.

When the container runs successfully, it executes the command specified in the Dockerfile’s CMD instruction. For this example, the output would be:

1hello world

This confirms that the container is running correctly and executing the intended command.

  1. docker run
  2. docker build
  3. docker images
  4. docker ps
(2) docker build. In the container development workflow, you must first use the docker build command to create a container image from a Dockerfile before you can run containers from that image.

The build context directory (represented by “.” in the docker build command) specifies the directory that Docker will use as the context for building the image. All files in this directory are sent to the Docker daemon and can be referenced in the Dockerfile during the build process.

Container images and running containers are the same thing in Docker.

False. Container images are static templates used to create containers, while running containers are active instances created from images. Images are like blueprints, and containers are the actual running applications based on those blueprints.

Container registries serve as centralized repositories for storing and distributing container images. They enable image distribution and collaboration through centralized storage systems, allowing teams to share images across different environments and systems.

Essential Docker commands for the complete container lifecycle include:

  • build (create container images from Dockerfiles)
  • images (list available images and display local image inventory)
  • run (create and start containers from images)
  • ps (list running containers and monitor container status)
  • push (upload images to registry for sharing)
  • pull (download images from registry to obtain pre-built images)