Docker Commands : A Simple Guide

Docker is one of the most powerful tools for developers, but it can feel overwhelming when you’re just starting out. Don’t worry—I’ve got you covered! In this guide, I’ll walk you through essential Docker commands in plain, beginner-friendly language with practical examples.


What is Docker?

In simple terms, Docker allows you to package your application and its dependencies into a container. A container is like a box that runs your application in any environment, whether it’s your local machine, a server, or the cloud.

Let’s dive into the commands you need to get started!


Step 1: Checking Docker Installation

Before using Docker, make sure it’s installed on your system.

  • Check Docker version:

      docker --version
    

    Output:

      Docker version 24.0.6, build 12345abc
    
  • View help commands:

      docker help
    

Step 2: Running Your First Docker Container

A container is like a running app in Docker. Let’s run our first container using the NGINX (a web server) image.

  1. Download and run NGINX:

     docker run -d -p 8080:80 nginx
    
    • -d: Runs the container in the background (detached mode).

    • -p 8080:80: Maps port 8080 on your computer to port 80 in the container.

  2. View the running container:

     docker ps
    

    Output:

     CONTAINER ID   IMAGE    COMMAND   STATUS        PORTS
     123abc456def   nginx    ...       Up 5 minutes  0.0.0.0:8080->80/tcp
    
  3. Access NGINX in your browser:
    Go to http://localhost:8080 to see the default NGINX page.

  4. Stop the container:

     docker stop 123abc456def
    

Step 3: Working with Images

Docker images are templates used to create containers.

  • Download an image:

      docker pull nginx
    
  • List all downloaded images:

      docker images
    

    Output:

      REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
      nginx        latest    abc123456def   5 days ago    23MB
    
  • Remove an image:

      docker rmi nginx
    

Step 4: Building Your Own Docker Image

If you have a custom application, you can create your own Docker image.

  1. Create a simple Python app:
    Create a file called app.py with this code:

     print("Hello from Docker!")
    
  2. Write a Dockerfile:
    The Dockerfile is a set of instructions for building your image. Create a Dockerfile:

     FROM python:3.9
     WORKDIR /app
     COPY app.py .
     CMD ["python", "app.py"]
    
  3. Build the Docker image:

     docker build -t my-python-app .
    
    • -t: Tags the image with a name (my-python-app).

    • .: Points to the location of the Dockerfile.

  4. Run your custom image:

     docker run my-python-app
    

    Output:

     Hello from Docker!
    

Step 5: Managing Containers

  • List all running containers:

      docker ps
    
  • List all containers (including stopped ones):

      docker ps -a
    
  • Remove a container:

      docker rm <container-id>
    
  • Restart a stopped container:

      docker start <container-id>
    

Step 6: Sharing Data Between Your Computer and a Container

Sometimes, you want to share files between your computer and the container.

  • Run a container with a volume:

      docker run -v /path/to/local-folder:/data alpine
    

    This command maps /path/to/local-folder on your computer to /data in the container.


Step 7: Clean Up Docker

Over time, Docker can take up space on your system. Use these commands to clean up.

  • Remove all stopped containers:

      docker container prune
    
  • Remove unused images:

      docker image prune
    
  • Remove unused resources (images, containers, networks):

      docker system prune
    

Step 8: Docker Compose (Simplify Multi-Container Apps)

Docker Compose lets you manage multiple containers easily.

  1. Create a docker-compose.yml file:

     version: '3.8'
     services:
       web:
         image: nginx
         ports:
           - "8080:80"
    
  2. Start the service:

     docker-compose up
    
  3. Stop the service:

     docker-compose down
    

Key Benefits of Docker

  1. Consistency Across Environments: Run the same app on any system.

  2. Simplified Application Setup: No more dependency headaches.

  3. Efficient Resource Usage: Containers are lightweight and fast.


Final Thoughts

Docker is an invaluable tool for modern development, and mastering its basic commands will help you build and ship applications efficiently. With this beginner-friendly guide, you now have a solid foundation to start your Docker journey.

Got any questions or favorite Docker commands? Let’s discuss in the comments! And don’t forget to follow Mustufa khan for more beginner-friendly tech insights.