Skip to main content

Docker Commands Tutorial

Docker is a powerful platform that enables developers to automate the deployment of applications in lightweight containers. This tutorial covers essential Docker commands, along with explanations and examples to help you get started.


Prerequisites​

  1. Install Docker: Ensure that Docker is installed on your system. You can verify installation by running:
    docker --version

1. Basic Docker Commands​

Viewing Docker Version and System Info​

  • View Docker Version:

    docker --version
  • View System-Wide Information:

    docker info

2. Working with Docker Images​

Docker images are templates used to create containers.

Pulling an Image​

To download a Docker image from Docker Hub:

docker pull <image-name>

Example:

docker pull nginx

Listing Images​

To see all images on your local system:

docker images

Removing an Image​

To delete an image:

docker rmi <image-id>

Example:

docker rmi nginx

3. Working with Docker Containers​

Containers are instances of Docker images that run isolated applications.

Running a Container​

To start a new container:

docker run <image-name>

Options:

  • -d: Runs the container in detached mode (in the background).
  • -p [host-port]:[container-port]: Maps a host port to a container port.

Example:

docker run -d -p 8080:80 nginx

Listing Running Containers​

To see all active containers:

docker ps

To see all containers (including stopped ones):

docker ps -a

Stopping a Container​

To stop a running container:

docker stop <container-id>

Removing a Container​

To delete a stopped container:

docker rm <container-id>

4. Managing Docker Volumes​

Docker volumes are used to persist data.

Creating a Volume​

To create a new volume:

docker volume create <volume-name>

Listing Volumes​

To view all volumes:

docker volume ls

Removing a Volume​

To delete a volume:

docker volume rm <volume-name>

5. Docker Networks​

Docker networks allow containers to communicate with each other.

Listing Networks​

To see all Docker networks:

docker network ls

Creating a Network​

To create a new network:

docker network create <network-name>

Connecting a Container to a Network​

To attach a container to an existing network:

docker network connect <network-name> <container-id>

Disconnecting a Container from a Network​

To detach a container from a network:

docker network disconnect <network-name> <container-id>

6. Building Docker Images​

To create a custom image using a Dockerfile:

docker build -t <image-name> <path-to-dockerfile>

Example:

docker build -t my-app .

Options​

  • -t: Tags the image with a name.

7. Docker Compose​

Docker Compose is used to define and manage multi-container applications.

Running Docker Compose​

To start containers defined in docker-compose.yml:

docker-compose up

Stopping Docker Compose​

To stop containers started by Docker Compose:

docker-compose down

Summary of Docker Commands​

CommandDescription
docker --versionShows Docker version
docker pull <image>Downloads an image
docker imagesLists all images
docker rmi <image-id>Removes an image
docker run <image>Runs a new container from an image
docker psLists running containers
docker stop <container>Stops a running container
docker rm <container>Removes a stopped container
docker volume createCreates a new volume
docker network createCreates a new network
docker build -t <name>Builds an image from a Dockerfile
docker-compose upStarts services defined in docker-compose.yml
docker-compose downStops all services

By using these commands, you can easily manage Docker images, containers, volumes, networks, and multi-container applications with Docker Compose.

Content Review​

The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.

About the Reviewer​

For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.