Back To Blogs

Comprehensive Guide to Using Docker

June 1, 2024

4 min read

blog image

Comprehensive Guide to Using Docker

Docker is a platform that allows developers to automate the deployment of applications inside lightweight, portable containers. It solves the problem of software dependencies by isolating applications and their dependencies from the underlying system. This makes it easier to develop, ship, and run applications in different environments, without worrying about compatibility issues.

In this blog post, we’ll go through the basics of using Docker, from installation to creating and managing containers, and we’ll also cover common issues and troubleshooting tips.

What is Docker?

Docker is an open-source tool that automates the process of deploying applications inside containers. A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software: code, runtime, system tools, libraries, and settings. Docker uses images to create containers and provides a command-line interface (CLI) to manage them.

Benefits of Docker

  • Portability: Containers can run consistently on any environment, whether on your local machine, a development server, or in the cloud.
  • Isolation: Containers isolate applications and their dependencies from the host system, ensuring that they don’t conflict with other applications.
  • Efficiency: Docker containers are lightweight and use fewer system resources compared to traditional virtual machines (VMs).
  • Scalability: Docker makes it easy to scale applications, deploy microservices, and automate CI/CD pipelines.

Getting Started with Docker

Step 1: Install Docker

On Windows and macOS

  1. Download Docker Desktop from the official website.
  2. Follow the installation instructions for your platform.
  3. Once installed, Docker Desktop will automatically start and be accessible via the system tray (on Windows) or the menu bar (on macOS).

On Linux

  1. Update the system package index:

    sudo apt-get update
  2. Install required dependencies:

    sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  3. Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. Set up the stable repository:

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  5. Install Docker:

    sudo apt-get update
    sudo apt-get install docker-ce
  6. Start and enable Docker:

    sudo systemctl start docker
    sudo systemctl enable docker
  7. Verify installation:

    docker --version

This should print the Docker version to confirm the installation was successful.

Step 2: Docker Commands Overview

Once Docker is installed, you can start working with Docker images and containers. Below are some basic Docker commands:

  • Check Docker version:

    docker --version
  • List Docker images:

    docker images
  • List running containers:

    docker ps
  • List all containers (including stopped):

    docker ps -a
  • Pull a Docker image (e.g., nginx):

    docker pull nginx
  • Run a Docker container:

    docker run -d --name mynginx -p 8080:80 nginx
  • Stop a running container:

    docker stop mynginx
  • Remove a stopped container:

    docker rm mynginx
  • Remove a Docker image:

    docker rmi nginx

Step 3: Creating a Dockerfile

A Dockerfile is a script that contains instructions on how to build a Docker image. It defines the environment and sets up the application inside a container.

Let’s create a simple Python web application inside a Docker container.

  1. First, create a directory for your project:

    mkdir my-python-app
    cd my-python-app
  2. Create a Dockerfile inside the project directory:

    # Use Python 3.9 as the base image
    FROM python:3.9
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # Copy the local code to the container
    COPY . /app
    
    # Install dependencies
    RUN pip install -r requirements.txt
    
    # Expose the port the app will run on
    EXPOSE 5000
    
    # Run the application
    CMD ["python", "app.py"]
  3. Create a requirements.txt file to define the dependencies:

    flask
  4. Create a simple app.py:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello, Docker World!'
    
    if __name__ == "__main__":
        app.run(host='0.0.0.0', port=5000)
  5. Build the Docker image:

    docker build -t my-python-app .
  6. Run the container:

    docker run -d -p 5000:5000 --name my-python-container my-python-app
  7. Access the app by navigating to http://localhost:5000 in your web browser.

Troubleshooting Common Docker Issues

1. Docker Daemon Not Running

  • Cause: Docker requires its daemon (background service) to be running in order to function.

  • Solution:

    • On Linux, you can start the Docker daemon with the following command:

      sudo systemctl start docker
    • On Windows or macOS, ensure that Docker Desktop is running. You can check the system tray or menu bar for the Docker icon.

2. Docker Container is Not Starting

  • Cause: A container may fail to start if there’s an issue with the image or if the necessary resources (like ports) are already in use.

  • Solution:

    • Check the logs of the container to identify any errors:

      docker logs <container_id>
    • Make sure no other container or application is using the same port. You can change the exposed port in the docker run command by using the -p flag:

      docker run -d -p 5001:5000 --name my-python-container my-python-app

3. Permission Denied for Docker Commands

  • Cause: On Linux, Docker commands need to be run with root privileges unless you are part of the docker group.

  • Solution:

    • Add your user to the Docker group:

      sudo usermod -aG docker $USER
    • Log out and log back in to apply the changes.

4. Out of Disk Space

  • Cause: Docker images and containers can take up significant disk space over time, especially if you’re running multiple images or containers.

  • Solution:

    • Remove unused images and containers:

      docker system prune
    • You can also remove individual containers or images:

      docker rm <container_id>
      docker rmi <image_id>

5. Docker Container Exits Immediately

  • Cause: This typically happens when the container’s main process (the command specified in the CMD or ENTRYPOINT in the Dockerfile) finishes execution or encounters an error.

  • Solution:

    • Inspect the container logs to see why it exited:

      docker logs <container_id>
    • Use the -it flag to run the container interactively and keep it open:

      docker run -it my-python-app /bin/bash

6. Network Issues (Ports Not Exposed)

  • Cause: Sometimes, applications inside containers may not be accessible if the correct ports are not exposed or mapped properly.

  • Solution:

    • Ensure that the -p flag is used correctly to map container ports to host machine ports:

      docker run -d -p 5000:5000 my-python-app

Conclusion

Docker simplifies the process of deploying, scaling, and running applications by encapsulating them in lightweight containers. With Docker, you can easily manage dependencies, isolate applications, and ensure consistent behavior across different environments.

By following this guide, you should now have a basic understanding of how to use Docker, from installation to building and running containers. Docker can be a powerful tool in your development and deployment workflow, so make sure to explore its full capabilities.

If you encounter any issues, the troubleshooting tips provided in this post should help you resolve common problems. Happy containerizing!