How To Install and Use Docker: Getting Started

Introduction

Docker is a powerful platform that allows you to develop, ship, and run applications in containers. Containers are lightweight, portable, and self-sufficient environments that ensure consistency across different development and deployment stages. In this guide, we’ll walk you through the steps to install Docker and get started with its basic usage.

Step 1: Install Docker

For Linux

To install Docker on a Linux-based system, follow these steps:

    1. Update your package manager:
sudo apt-get update
    1. Install Docker:
sudo apt-get install docker.io
    1. Start the Docker service:
sudo systemctl start docker
    1. Enable Docker to start on boot:
sudo systemctl enable docker

For macOS and Windows

For macOS and Windows, Docker provides a desktop application called Docker Desktop. Follow these steps:

  1. Download Docker Desktop from the official Docker website.
  2. Install the application by following the on-screen instructions.
  3. Launch Docker Desktop and ensure it is running.

Step 2: Verify Docker Installation

To verify that Docker is installed correctly, run the following command:

docker --version

This will display the installed Docker version. You can also check the Docker service status with:

docker info

Step 3: Run Your First Docker Container

Docker provides a simple “Hello World” container to test your setup. Run the following command:

docker run hello-world

This command downloads a test image, runs it in a container, and displays a “Hello from Docker!” message.

Step 4: Pull and Run a Docker Image

Docker images are the building blocks of containers. You can pull an image from Docker Hub (a public registry) and run it locally. For example, to run an Nginx web server, use:

docker run -d -p 8080:80 nginx

This command pulls the Nginx image, runs it in detached mode (-d), and maps port 8080 on your host to port 80 in the container. You can access the Nginx server by navigating to http://localhost:8080 in your browser.

Step 5: Manage Docker Containers

To list all running containers, use:

docker ps

To list all containers (including stopped ones), use:

docker ps -a

To stop a running container, use:

docker stop CONTAINER_ID

To remove a container, use:

docker rm CONTAINER_ID

Step 6: Build Your Own Docker Image

You can create custom Docker images using a Dockerfile. Here’s an example:

# Use an official base image
FROM ubuntu:latest

# Install necessary packages
RUN apt-get update && apt-get install -y python3

# Copy your application code
COPY . /app

# Set the working directory
WORKDIR /app

# Run your application
CMD ["python3", "app.py"]

To build the image, navigate to the directory containing the Dockerfile and run:

docker build -t my-custom-image .

Then, run the container:

docker run my-custom-image

Conclusion

Docker is an essential tool for modern application development and deployment. By following this guide, you’ve learned how to install Docker, run containers, and create custom images. With Docker, you can ensure consistency and efficiency across your development workflow.

If you’re looking for a reliable hosting solution to deploy your Dockerized applications, consider NetCloud24, which offers free webhosting options to help you get started quickly.