From Image to Container: Running Your First Docker Application

Introduction to Docker Containers

Docker has revolutionized the way we develop, ship, and run applications. At its core, Docker allows you to package an application with all of its dependencies – libraries, frameworks, system tools, and runtime – into a standardized unit called an image. But an image is just a template. To actually run your application, you need to create a container from that image. This tutorial will guide you through the process of taking a Docker image and running it as a container.

Understanding Docker Images and Containers

Think of a Docker image as a snapshot of your application and its environment. It’s read-only and acts as the foundation for your container. A container is a runnable instance of that image. It’s a live, isolated environment where your application runs. Multiple containers can be created from a single image, each operating independently.

Listing Available Images

Before you can run a container, you need to know what images are available on your system. Use the following command to list all locally stored Docker images:

docker images

This will display a table with information about each image, including its repository name, tag, image ID, creation date, and size. The REPOSITORY and TAG columns are key for identifying the image you want to run. If no tag is specified when building an image, it defaults to latest.

Running a Container

Once you’ve identified the image you want to use, you can run it as a container using the docker run command. The basic syntax is:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • IMAGE: This specifies the name or ID of the image you want to run. You can use either the repository name and tag (e.g., ubuntu:12.04) or the image ID.
  • [OPTIONS]: These are flags that control how the container runs. We’ll explore some common options below.
  • [COMMAND] and [ARG...]: These specify the command to be executed inside the container and any arguments for that command. If not provided, the image’s default command (specified in its Dockerfile) will be executed.

Example:

To run an Ubuntu 12.04 container and start an interactive bash shell, you would use:

docker run -i -t ubuntu:12.04 /bin/bash
  • -i: Keeps STDIN open even if not attached. This allows you to interact with the container.
  • -t: Allocates a pseudo-TTY, creating an interactive terminal session.

Common docker run Options

Here are some other useful options you might encounter:

  • -d: Runs the container in detached mode (in the background). This is useful for long-running applications that don’t require a direct terminal connection.
  • --name <container_name>: Assigns a name to the container. This makes it easier to manage and refer to the container.
  • -p <host_port>:<container_port>: Maps a port on your host machine to a port inside the container. This allows you to access applications running inside the container from your host machine. For example, -p 8080:80 maps port 80 on the container to port 8080 on your host.
  • -v <host_path>:<container_path>: Creates a volume mount, allowing you to share files and directories between your host machine and the container. This is useful for persistent data storage.
  • --rm: Automatically removes the container when it exits. This is useful for short-lived containers that you don’t need to persist.
  • --restart always: Configures the container to automatically restart if it exits. This ensures your application remains running even after unexpected errors.

Example with Options:

docker run -d --name my_web_app -p 8080:80 --restart always nginx

This command runs an Nginx web server in detached mode, names the container my_web_app, maps port 80 on the container to port 8080 on the host, and ensures the container automatically restarts if it crashes.

Managing Running Containers

Once your container is running, you can use other Docker commands to manage it.

  • docker ps: Lists all running containers.
  • docker exec -it <container_id> /bin/bash: Executes a command inside a running container. In this example, it starts an interactive bash shell inside the container. You can replace <container_id> with the container’s ID or name.
  • docker stop <container_id>: Stops a running container.
  • docker rm <container_id>: Removes a stopped container.

Leave a Reply

Your email address will not be published. Required fields are marked *