← Back to Tutorials

Getting Started with Docker

Getting Started with Docker

Introduction

Docker is a powerful platform that allows developers to automate the deployment of applications inside lightweight containers. These containers can run on any machine that has Docker installed, ensuring that your application runs consistently across different environments. In this tutorial, we will guide you step-by-step on how to get started with Docker, from installation to creating your first container.

Steps (detailed)

Step 1: Install Docker

  1. Download Docker Desktop:

    • Go to the Docker website.
    • Click on the “Download for your OS” button. Docker Desktop is available for Windows, macOS, and Linux.
  2. Install Docker Desktop:

    • Follow the installation instructions for your operating system.
    • For Windows and macOS, double-click the downloaded file and follow the prompts.
    • For Linux, you can use your package manager. For example, on Ubuntu, run:
      sudo apt-get update
      sudo apt-get install docker.io
  3. Start Docker:

    • On Windows and macOS, Docker Desktop starts automatically after installation.
    • For Linux, you might need to start the Docker service with:
      sudo systemctl start docker

Docker Installation (Example image URL, replace with actual)

Step 2: Verify the Installation

To check if Docker is installed correctly, open your terminal or command prompt and run:

docker --version

You should see the version of Docker installed on your system.

Step 3: Run Your First Container

  1. Pull a Docker Image:

    • Docker images are the blueprints for containers. Let’s pull a simple image like hello-world:
    docker pull hello-world
  2. Run a Container:

    • Now that you have the image, you can run it:
    docker run hello-world
    • This command will create a container from the hello-world image and run it. You should see a message confirming that Docker is working.

Hello World Container (Example image URL, replace with actual)

Step 4: Explore Docker Commands

Here are a few basic commands to manage your containers:

Step 5: Create Your Own Docker Image

  1. Create a Directory for Your Project:

    mkdir my-docker-app
    cd my-docker-app
  2. Create a Dockerfile:

    • Open a text editor and create a file named Dockerfile:
    # Use an official Python runtime as a parent image
    FROM python:3.8-slim
    
    # Set the working directory in the container
    WORKDIR /usr/src/app
    
    # Copy the current directory contents into the container at /usr/src/app
    COPY . .
    
    # Install any needed packages specified in requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt
    
    # Make port 80 available to the world outside this container
    EXPOSE 80
    
    # Define environment variable
    ENV NAME World
    
    # Run app.py when the container launches
    CMD ["python", "app.py"]
  3. Build Your Docker Image:

    • In the terminal, run:
    docker build -t my-docker-app .
  4. Run Your Docker Container:

    docker run -p 4000:80 my-docker-app

Now your application should be running in a Docker container accessible at http://localhost:4000.

Docker Image Creation (Example image URL, replace with actual)

Troubleshooting

Conclusion

Congratulations! You’ve successfully installed Docker, run your first container, and even created your own Docker image. As you continue to explore Docker, you’ll find it a valuable tool for developing, shipping, and running applications. To delve deeper, consider exploring Docker Compose for managing multi-container applications or Docker Hub for sharing your images.


SEO Metadata: