How to Use a Docker Container?

How to Use a Docker Container: A Comprehensive Guide by OpsNexa

Docker containers have revolutionized the way developers build, package, and deploy applications. Containers provide an isolated environment for running software, ensuring that applications run consistently across different systems and platforms. This makes Docker an invaluable tool for modern development workflows.

In this guide, OpsNexa will walk you through the basic steps of using Docker containers. You’ll learn how to create, run, manage, and interact with Docker containers in a seamless way. Whether you’re just getting started with Docker or looking to refine your skills, this guide will provide the information you need to leverage Docker containers effectively.

What is a Docker Container?

Before diving into how to use a Docker container, let’s briefly discuss what a Docker container is.

A Docker container is a lightweight, stand-alone, and executable software package that contains everything needed to run a piece of software: the code, runtime, libraries, environment variables, and configuration files. Containers encapsulate an application and its dependencies, ensuring that it runs the same way regardless of the underlying infrastructure.

Unlike virtual machines, containers do not require a full operating system to run. Instead, they share the host system’s kernel, making them more efficient and faster than traditional virtual machines. Containers are an essential part of modern DevOps practices, as they allow developers to automate deployment, scaling, and management of applications.

Step 1: Install Docker

Before you can use Docker containers, you need to have Docker installed on your system. Here’s how to install Docker:

Installing Docker on Linux:

  1. Update your system’s package index:

    bash
    sudo apt update
  2. Install Docker using the following command:

    bash
    sudo apt install docker.io
  3. After installation, start Docker:

    bash
    sudo systemctl start docker
  4. Enable Docker to start on boot:

    bash
    sudo systemctl enable docker
  5. Verify the installation:

    bash
    docker --version

Installing Docker on macOS:

  1. Download Docker Desktop from the official Docker website: Docker Desktop for Mac.

  2. Open the downloaded .dmg file and drag Docker to the Applications folder.

  3. Launch Docker from your Applications folder, and you’ll see Docker’s whale icon in your system tray.

  4. Verify Docker is working by running:

    bash
    docker --version

Installing Docker on Windows:

  1. Download Docker Desktop for Windows: Docker Desktop for Windows.

  2. Follow the installation instructions and ensure that Docker Desktop is set to run with Windows Subsystem for Linux (WSL).

  3. Once installed, launch Docker Desktop and confirm that Docker is running by checking the Docker icon in your system tray.

  4. Verify the installation:

    bash
    docker --version

Once Docker is installed, you’re ready to start working with Docker containers.

Step 2: Pull an Image from Docker Hub

Docker containers are created from Docker images. Images are stored in repositories, and the most popular repository is Docker Hub. You can find official images for almost every popular application on Docker Hub.

To use a Docker container, you need to pull an image from Docker Hub or create your own custom image.

For example, let’s pull the official Nginx image:

bash
docker pull nginx

This command fetches the latest Nginx image from Docker Hub and downloads it to your system.

Explanation:

  • docker pull: This command downloads a Docker image from a registry.

  • nginx: This is the name of the image you want to pull.

Once the image is downloaded, you can use it to create a Docker container.

Step 3: Run a Docker Container

Now that you have a Docker image, you can create and run a container based on that image.

For example, to run a container from the Nginx image, use the following command:

bash
docker run --name mynginx -d -p 8080:80 nginx

Explanation:

  • docker run: This command creates and starts a container from an image.

  • --name mynginx: This assigns the name “mynginx” to your container. You can use any name you like.

  • -d: This flag tells Docker to run the container in detached mode, meaning it runs in the background.

  • -p 8080:80: This maps port 8080 on your host machine to port 80 on the container. This way, you can access the Nginx service running inside the container via your browser at http://localhost:8080.

  • nginx: This is the name of the image to use for the container.

After running this command, your Nginx server will be accessible at http://localhost:8080 on your local machine.

Step 4: Interact with a Running Docker Container

Once your container is running, you might want to interact with it. There are a few ways to interact with Docker containers, such as viewing logs or opening a shell inside the container.

Viewing Container Logs

To see the logs of a running container, use the following command:

bash
docker logs mynginx

This will show the logs of the mynginx container, which can be helpful for debugging or monitoring the application.

Executing a Command Inside the Container

You can also execute commands inside the running container. For example, to start an interactive shell inside the container, use the docker exec command:

bash
docker exec -it mynginx bash

Explanation:

  • docker exec: This command is used to execute a command in a running container.

  • -it: This flag starts an interactive terminal session inside the container.

  • bash: This opens a Bash shell inside the container.

Now, you’re inside the container’s shell, where you can interact with the container’s filesystem, install software, and perform any necessary maintenance tasks.

Step 5: Stop a Running Docker Container

When you’re done with your Docker container, you’ll want to stop it to free up resources.

To stop a running container, use the following command:

bash
docker stop mynginx

This stops the container named mynginx. After stopping it, you can either restart the container or remove it as needed.

Step 6: Remove a Docker Container

If you no longer need the container, you can remove it. To remove the mynginx container, use the following command:

bash
docker rm mynginx

Note that you can only remove a container that is not running. If the container is still running, you need to stop it first using the docker stop command.

Step 7: Clean Up Unused Docker Containers and Images

Over time, you may accumulate unused containers and images on your system. Docker provides several commands to clean up unnecessary resources:

Remove all stopped containers:

bash
docker container prune

Remove all unused images:

bash
docker image prune -a

This will help you free up disk space by removing containers and images that are not in use.

Conclusion

Using Docker containers can drastically improve your development, testing, and deployment workflows. By encapsulating your applications and dependencies in containers, you ensure consistency across different environments and simplify the management of complex applications.

In this guide, OpsNexa has covered the essential steps on how to use a Docker container, from installation to pulling images, running containers, interacting with them, and cleaning up after use. With these skills, you can now effectively manage Docker containers and streamline your development process.

Happy containerizing!