Back to Home

Docker Command Documentation

Docker is a platform for developing, shipping, and running applications in containers. Containers package an application’s code, libraries, and dependencies together in an isolated environment. The Docker CLI (command-line interface) provides a rich set of commands for managing images, containers, networks, and volumes. The sections below detail the basic Docker commands along with their syntax, description, and usage examples.

1. Getting Started

1.1. Checking Docker Installation

These commands are often the first step to verify that Docker is properly installed and running on your system.


2. Working with Docker Images

Docker images are the blueprints used to create containers.

2.1. Pulling an Image

2.2. Building an Image

2.3. Listing and Removing Images


3. Managing Docker Containers

Containers are running instances of Docker images.

3.1. Running a Container

3.2. Listing Containers

3.3. Stopping, Restarting, and Removing Containers

3.4. Executing Commands Inside Containers

3.5. Viewing Container Logs

3.6. Inspecting Containers


4. Managing Docker Volumes and Networks

Persistent data and networking are essential for containerized applications.

4.1. Docker Volumes

Volumes allow you to persist data independently from the container lifecycle.

4.2. Docker Networks

Using custom networks helps containers communicate securely.


5. System Cleanup and Advanced Commands

5.1. Cleaning Up Unused Resources

Over time, unused images, containers, networks, and volumes can accumulate.

5.2. Other Useful Commands

These commands round out the basic functionality needed to work effectively with Docker.


6. Conclusion

This documentation covers the core Docker commands required to manage images, containers, networks, and volumes. Mastery of these commands is essential for developers and DevOps professionals to build, run, and maintain containerized applications efficiently.

For further details, always refer to the official Docker documentation and CLI reference:

By understanding and practicing these commands, you will have a strong foundation in Docker’s capabilities and be well on your way to containerizing your applications successfully.

Below is an updated section of the Docker command documentation that not only covers the basic commands but also provides detailed, step‐by‐step instructions on how to start a containerized application using Docker. This guide explains the process, the flags used, and best practices, so that you can easily run your containerized application and understand what is happening behind the scenes.


7. Starting a Containerized Application

When your application is containerized—that is, packaged into one or more Docker images—you use Docker to start (or “run”) containers based on those images. This section explains in detail how to launch a containerized application, along with explanations of the commonly used options.

7.1. The Basic docker run Command

The fundamental command to create and start a container is:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
 

7.2. Example: Running a Web Application

Imagine you have containerized a web application using an image named myapp:latest. To start this container so that the web service is accessible, you might use:

docker run -d -p 8080:80 --name my-webapp myapp:latest
 

Let’s break down what each flag means:

7.3. Starting a Container Interactively

Sometimes you may want to interact directly with the container’s shell for debugging or manual configuration. To do so, you use interactive mode along with a pseudo-TTY:

docker run -it --name debug-container myapp:latest bash
 

7.4. Using Environment Variables

Many containerized applications require configuration via environment variables (for example, setting a database connection string or an API key). You can pass these variables using the --env (or -e) flag:

docker run -d -p 8080:80 --name my-webapp -e APP_ENV=production -e API_KEY=abcdef123456 myapp:latest
 

7.5. Persisting Data with Volumes

If your application writes data that you don’t want to lose when the container stops or is removed, you can use volumes. For example, if your web application stores uploaded files, you might run:

docker run -d -p 8080:80 --name my-webapp -v /host/path/uploads:/app/uploads myapp:latest
 

7.6. Networking Considerations

If your application is part of a larger system (e.g., a multi-container setup with a separate database), you might need to attach the container to a custom Docker network:

docker network create my-app-network
docker run -d --name my-webapp --network my-app-network -p 8080:80 myapp:latest
 

7.7. Running Multi-Container Applications with Docker Compose

For applications that require multiple services (e.g., a web server, a database, a cache), Docker Compose is a powerful tool that allows you to define and run multi-container Docker applications. A typical docker-compose.yml file might look like this:

version: "3.8"
services:
  web:
    image: myapp:latest
    container_name: my-webapp
    ports:
      - "8080:80"
    environment:
      - APP_ENV=production
    volumes:
      - ./uploads:/app/uploads
    networks:
      - app-network

  db:
    image: postgres:13
    container_name: my-database
    environment:
      - POSTGRES_PASSWORD=secret
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  db-data:
 

To start the multi-container application defined in the file, simply run:

docker-compose up -d
 

Docker Compose simplifies the orchestration of multiple containers, ensuring that they are all configured correctly and can communicate with each other.


8. Recap and Best Practices

Save This Page