Docker Run a Container

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will immerse yourself in a scenario set in the Victorian era, where you take on the role of an antiquities dealer. Your goal is to effectively transport and present historic artifacts using modern technology—specifically, leveraging Docker containers to streamline your operations.

Imagine you are a respected antiquities dealer in 1880s London, known for your exceptional collection of artifacts from around the world. However, instead of traditional crates and display cases, you've discovered a revolutionary technology called "Docker containers" that allows you to package, transport, and showcase your precious items with unprecedented efficiency and security.

By the end of this lab, you'll understand the basics of Docker containers and how to use them to run applications, which in our Victorian scenario, represents your ability to manage and showcase your valuable antiquities collection.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 100% completion rate. It has received a 96.15% positive review rate from learners.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") subgraph Lab Skills docker/run -.-> lab-271495{{"Docker Run a Container"}} end

Setting Up Your Docker Environment

In this step, you will begin by ensuring that your Docker environment is properly set up to run containers. Think of this as preparing your antiquities workshop with the necessary tools and equipment.

  1. Install Docker by executing the following commands in your terminal:

    sudo apt update
    sudo apt install docker.io

    This first command updates your package lists to ensure you're getting the latest version of Docker, while the second command installs Docker itself. The sudo prefix grants administrative privileges needed for installation.

  2. Once Docker is installed, start the Docker service:

    sudo systemctl start docker

    This command activates the Docker service, similar to opening your workshop for business. The Docker daemon is now running in the background, ready to process your container requests.

  3. Verify that Docker is running successfully:

    sudo systemctl status docker

    This command checks if Docker is active and running properly. You should see output with "active (running)" in green text, confirming that your Docker environment is ready. If you see "inactive" or any error messages, you may need to retry starting the service.

  4. To ensure you can run Docker commands without using sudo each time, add your user to the Docker group:

    sudo usermod -aG docker $USER

    Note: After running this command, you may need to log out and log back in for the changes to take effect. For this lab, we'll continue using sudo with our Docker commands to ensure compatibility.

Running a Hello-World Container

Now that your environment is set up, you will run your first Docker container—a simple "hello-world" example. Consider this as displaying your first artifact to ensure your new container technology works as expected.

  1. Pull the "hello-world" image from Docker Hub:

    sudo docker pull hello-world

    This command downloads the hello-world image from Docker Hub (a public repository of Docker images). Think of this as acquiring your first artifact from a distant land. Docker Hub serves as a marketplace where you can find pre-packaged containers for various purposes.

  2. Run the "hello-world" container:

    sudo docker run hello-world

    This command creates and starts a container from the hello-world image. The container runs a simple program that outputs a message and then exits. In our Victorian scenario, this is equivalent to briefly displaying your first artifact to verify that your new container system works properly.

  3. Check the output to ensure that the container ran successfully. You should see a message that starts with "Hello from Docker!" followed by some explanatory text. This confirms that:

    • Docker is correctly installed
    • Your system can successfully create containers
    • You can download images from Docker Hub
  4. To see a list of all containers (including those that have exited):

    sudo docker ps -a

    This command shows all containers, including the hello-world container that has now exited. You'll see details like the container ID, what image it was created from, when it was created, and its current status.

Deploying an Nginx Web Server

In this step, you will deploy an Nginx web server using a Docker container. In our Victorian scenario, this represents setting up an elegant display cabinet (Nginx) for showcasing your most prized antiquities (web content) to visitors.

  1. Pull the Nginx image from Docker Hub:

    sudo docker pull nginx

    This command downloads the official Nginx image from Docker Hub. Nginx is a popular web server that can serve HTML content. This is equivalent to acquiring a fine display cabinet for your antiquities collection.

  2. Run an Nginx container in detached mode, mapping port 80 on the host to port 80 on the container:

    sudo docker run -d -p 80:80 nginx

    Let's break down this command:

    • run: Creates and starts a new container
    • -d: Runs the container in detached mode (in the background)
    • -p 80:80: Maps port 80 on your host machine to port 80 in the container, allowing web traffic to reach the Nginx server
    • nginx: Specifies the image to use

    This is like placing your display cabinet in your showroom and ensuring visitors can see your antiquities.

  3. Verify that your Nginx container is running:

    sudo docker ps

    This command shows all running containers. You should see your Nginx container in the list, indicating it's actively running.

  4. Add a web service mapping from the top menu lab and set the port mapping to 80. If the Nginx web server is running successfully, the lab environment will automate navigating to a random URL in the server lab, and you should see the default Nginx welcome page.

  5. Optional: If you want to access the Nginx server from your browser manually, you can navigate to http://localhost or the IP address of your machine.

  6. If you encounter any issues, you can check the container logs:

    sudo docker logs <container_id>

    Replace <container_id> with the ID of your Nginx container, which you can find from the docker ps command output.

Summary

In this lab, you have been introduced to the fundamental concepts of running Docker containers. By simulating a historical context and engaging as an antiquities dealer in the Victorian era, you have gained practical experience in leveraging Docker's run capabilities.

You've learned how to:

  • Set up a Docker environment
  • Pull images from Docker Hub
  • Run containers in both foreground and detached modes
  • Expose container ports to the host machine
  • Verify container status

These skills form the foundation of container technology and can be applied to a wide range of applications beyond our antiquities dealer scenario. In modern environments, these same techniques are used to deploy applications ranging from simple web servers to complex microservices architectures.

The ability to package applications with their dependencies into portable containers revolutionizes how we deploy and manage software, much like how standardized shipping containers transformed global trade. As our Victorian antiquities dealer might say, "These marvelous Docker containers shall revolutionize how we present our treasures to the world!"