Working with Containers: Creation, Running, and Stopping

Wrangling Containers: Creation, Running, and Stopping with Code Examples

In the vibrant world of containerization, Docker containers reign supreme. They act as lightweight, portable units encapsulating an application and its dependencies. But how do you bring these containers to life and manage their lifecycles? This guide equips you with the knowledge to create, run, and stop Docker containers, providing code examples to illuminate the process.

1. Creating Containers:

  • Running vs. Creating: The docker run command serves a dual purpose. By default, it creates and runs a new container from a specified image.

Code Snippet (Running a Container):

Bash
docker run ubuntu:latest

This command creates and runs a container based on the official Ubuntu image (ubuntu:latest). The container will launch and remain running until stopped or terminated.

  • Detached Mode: Append the -d flag to the docker run command to create a container in detached mode. This allows the container to run in the background without attaching to your terminal.

Code Snippet (Detached Mode):

Bash
docker run -d --name my-webserver nginx:latest

This command creates a detached container named my-webserver based on the nginx:latest image. The container runs in the background, and you can use docker ps to view its status.

2. Running Existing Containers:

  • Starting a Stopped Container: Use the docker start command to start a container that has been stopped but not removed.

Code Snippet (Starting a Container):

Bash
docker start my-stopped-container

This command starts the container named my-stopped-container, assuming it was previously stopped and not deleted.

3. Stopping Containers:

  • Graceful Termination: The docker stop command sends a SIGTERM signal to the container, allowing it to gracefully stop running processes before termination.

Code Snippet (Stopping a Container):

Bash
docker stop my-running-container

This command stops the container named my-running-container, giving it a chance to perform any necessary cleanup tasks before shutting down.

4. Additional Considerations:

  • Interactive vs. Detached: Choose between running containers in interactive mode (default for docker run) or detached mode (using -d) based on your needs. Interactive mode allows you to interact with the container’s terminal, while detached mode is suitable for long-running background processes.
  • Command Overriding: The docker run command can optionally specify a command to override the default entrypoint defined in the image. This allows you to execute a specific command within the container.

Code Snippet (Command Overriding):

Bash
docker run ubuntu:latest bash

This command runs the bash shell within a container based on the ubuntu:latest image, even if the image has a different default entrypoint.

  • Specifying Ports: Use the -p flag with docker run to map container ports to host machine ports, making services within the container accessible from the outside world.

Code Snippet (Port Mapping):

Bash
docker run -d -p 80:80 nginx:latest

This command maps container port 80 (typically used by web servers) to host machine port 80, allowing you to access the web server running within the container by visiting http://localhost in your web browser.

By grasping these fundamental container creation, running, and stopping techniques, you’ll be well on your way to harnessing the power of Docker containers in your development and deployment workflows. The provided code examples offer a practical starting point for your containerized adventures!