MODULE 7 · TOPIC 8 · VISUAL DEVOPS LAB

Managing Docker Containers

A container has a lifecycle: created, running, paused, exited and removed. Docker can inspect process logs, execute additional commands in a running container and configure restart policies. Persistent data should be stored outside the disposable writable layer. The exercises use small local containers and scoped names to avoid touching other applications.

Browser-based DevOps simulations. Commands, interfaces, logs and infrastructure states are illustrative; no Jenkins, Docker, Git hosting or Kubernetes operation is executed.

1. Create, start, stop and remove

docker create prepares a container without launching its process; docker start launches it. docker stop requests termination and may force termination after a timeout. docker rm normally removes stopped containers; it does not remove the source image.

Why this example? Compare leaving a stopped container with removing it.

Before you run: Choose a scenario and use Next step or Play. Read the diagram, command/configuration panel and simulated interface together. Code fragments require the indicated tools or existing project.
Why this step happens
See all steps in this scenario
    Commands and configuration
    Simulated terminal outputConsole
    Data operationsCompact blocks
    CurrentResultError / rejected

    Change inputs and step through the example. Simulated output. Exact logs, versions and UI appearance vary by installation.

    What this animation explains

    docker create prepares a container without launching its process; docker start launches it. docker stop requests termination and may force termination after a timeout. docker rm normally removes stopped containers; it does not remove the source image.

    Try and observe

    Watch the lifecycle and the effect on docker ps -a.

    2. Foreground, detached and logs

    A foreground container attaches the CLI to its streams, while -d starts detached and returns the container ID. docker logs reads retained stdout/stderr. A process that exits immediately does not stay running simply because it was detached.

    Why this example? Compare a one-shot detached process and a long-lived nginx process.

    Before you run: Choose a scenario and use Next step or Play. Read the diagram, command/configuration panel and simulated interface together. Code fragments require the indicated tools or existing project.
    Why this step happens
    See all steps in this scenario
      Commands and configuration
      Simulated terminal outputConsole
      Data operationsCompact blocks
      CurrentResultError / rejected

      Change inputs and step through the example. Simulated output. Exact logs, versions and UI appearance vary by installation.

      What this animation explains

      A foreground container attaches the CLI to its streams, while -d starts detached and returns the container ID. docker logs reads retained stdout/stderr. A process that exits immediately does not stay running simply because it was detached.

      Try and observe

      Inspect process lifetime and recorded logs.

      3. Exec and inspect

      docker exec launches an additional process inside an already-running container. It does not restart a stopped container. docker inspect works on existing stopped or running containers and shows configuration and runtime state.

      Why this example? Try executing a command before and after stopping a container.

      Before you run: Choose a scenario and use Next step or Play. Read the diagram, command/configuration panel and simulated interface together. Code fragments require the indicated tools or existing project.
      Why this step happens
      See all steps in this scenario
        Commands and configuration
        Simulated terminal outputConsole
        Data operationsCompact blocks
        CurrentResultError / rejected

        Change inputs and step through the example. Simulated output. Exact logs, versions and UI appearance vary by installation.

        What this animation explains

        docker exec launches an additional process inside an already-running container. It does not restart a stopped container. docker inspect works on existing stopped or running containers and shows configuration and runtime state.

        Try and observe

        See that inspect can still work when exec is rejected.

        4. Restart policies

        Restart policies govern automatic restart after exit. on-failure restarts when the process exits with nonzero status, within configured retry limits. A normal exit is not restarted by on-failure. Manual stopping and daemon restarts have separate rules.

        Why this example? Compare exit code zero with nonzero using on-failure:2.

        Before you run: Choose a scenario and use Next step or Play. Read the diagram, command/configuration panel and simulated interface together. Code fragments require the indicated tools or existing project.
        Why this step happens
        See all steps in this scenario
          Commands and configuration
          Simulated terminal outputConsole
          Data operationsCompact blocks
          CurrentResultError / rejected

          Change inputs and step through the example. Simulated output. Exact logs, versions and UI appearance vary by installation.

          What this animation explains

          Restart policies govern automatic restart after exit. on-failure restarts when the process exits with nonzero status, within configured retry limits. A normal exit is not restarted by on-failure. Manual stopping and daemon restarts have separate rules.

          Try and observe

          Observe bounded retries for the nonzero case.

          5. Bind mount versus named volume

          A bind mount maps an explicit host path into the container, while a named volume is managed by Docker. A read-only mount prevents writes through that mount. Choose dedicated paths and avoid mounting sensitive host directories.

          Why this example? Compare a read-only and read-write named volume.

          Before you run: Choose a scenario and use Next step or Play. Read the diagram, command/configuration panel and simulated interface together. Code fragments require the indicated tools or existing project.
          Why this step happens
          See all steps in this scenario
            Commands and configuration
            Simulated terminal outputConsole
            Data operationsCompact blocks
            CurrentResultError / rejected

            Change inputs and step through the example. Simulated output. Exact logs, versions and UI appearance vary by installation.

            What this animation explains

            A bind mount maps an explicit host path into the container, while a named volume is managed by Docker. A read-only mount prevents writes through that mount. Choose dedicated paths and avoid mounting sensitive host directories.

            Try and observe

            Inspect the write result in the container.

            6. Resource and health inspection

            docker stats shows live CPU and memory measurements. A Docker health check runs a configured command and reports starting, healthy or unhealthy. An unhealthy result alone does not automatically restart a plain Docker container.

            Why this example? Select whether a configured health probe succeeds or fails.

            Before you run: Choose a scenario and use Next step or Play. Read the diagram, command/configuration panel and simulated interface together. Code fragments require the indicated tools or existing project.
            Why this step happens
            See all steps in this scenario
              Commands and configuration
              Simulated terminal outputConsole
              Data operationsCompact blocks
              CurrentResultError / rejected

              Change inputs and step through the example. Simulated output. Exact logs, versions and UI appearance vary by installation.

              What this animation explains

              docker stats shows live CPU and memory measurements. A Docker health check runs a configured command and reports starting, healthy or unhealthy. An unhealthy result alone does not automatically restart a plain Docker container.

              Try and observe

              Compare running process state with independent health status.

              Command and configuration reference

              Use these commands and settings with the corresponding installed tools in a disposable practice environment.

              Command / settingWhy and when to use it
              docker create --name web-lab nginx:alpineCreate, start, stop and remove — Compare leaving a stopped container with removing it.
              docker run -d --name msg-lab alpine echo helloForeground, detached and logs — Compare a one-shot detached process and a long-lived nginx process.
              docker run -d --name exec-lab nginx:alpineExec and inspect — Try executing a command before and after stopping a container.
              docker run --name retry-lab --restart=on-failure:2 alpine sh -c "exit 0"Restart policies — Compare exit code zero with nonzero using on-failure:2.
              docker volume create write-labBind mount versus named volume — Compare a read-only and read-write named volume.
              docker inspect --format "{{.State.Health.Status}}" health-labResource and health inspection — Select whether a configured health probe succeeds or fails.

              Worked practical examples

              Follow the procedures in an isolated practice environment. Configuration snippets may require an existing project, build tools, credentials, images or cluster objects as indicated.

              1. Practice the container lifecycle

              Procedure

              1. Ensure web-lab is an unused lab name.
              2. Create, start, inspect and stop it.
              3. Remove only the stopped lab container.
              Code / configuration
              docker create --name web-lab nginx:alpine
              docker start web-lab
              docker ps
              docker stop web-lab
              docker ps -a
              docker rm web-lab
              Expected result: The container progresses through created, running, exited and removed.

              2. Inspect logs and execute a command

              Procedure

              1. Start a dedicated nginx container.
              2. Read logs and run nginx -v inside it.
              3. Stop the container and observe exec failure; inspect still works.
              Code / configuration
              docker run -d --name exec-lab nginx:alpine
              docker logs exec-lab
              docker exec exec-lab nginx -v
              docker stop exec-lab
              docker inspect exec-lab
              Expected result: Inspect works for an existing stopped container; exec requires a running one.

              Predict the outcome before you run

              Try answering these questions, then expand each answer and repeat the appropriate simulation.

              1. Predict the final state for Stop and remove in “Create, start, stop and remove”. Which operation explains it?

              Delete stopped container; image retained Result: web-lab removed.

              2. Predict the final state for Web server in “Foreground, detached and logs”. Which operation explains it?

              Running process remains present Result: web-lab Up.

              3. Predict the final state for Stopped in “Exec and inspect”. Which operation explains it?

              Start container before using exec Result: Inspect remains available.

              Hands-on practice and assessment

              Hands-on challenge

              Practice with uniquely named nginx and Alpine containers, inspect state, stop and start them, and remove only your lab containers.

              Edit this command/configuration scratchpad. Review prerequisites and run only in your own isolated practice environment; the browser never executes commands.

              Quick assessment

              Official documentation

              Use the documentation for your installed tool versions and platform.

              https://docs.docker.com/engine/containers/run/

              https://docs.docker.com/reference/cli/docker/container/

              https://docs.docker.com/engine/storage/volumes/

              Key points and real tool notes

              Use a local disposable practice environment and verify the active context, account, repository and names before running commands. Examples use Java 21, Maven 3.9, Jenkins Pipeline, Docker Engine and standard Kubernetes APIs. Logs and interfaces are educational reconstructions; versions, permissions, resources and real results can differ. No real deployments, credentials or network operations run in this page.