DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Basic Docker Commands explanation

Here's a detailed explanation of some basic Docker commands, along with analogies to help make them more relatable:

  1. docker build

Command:

docker build -t :

Explanation: This command creates a Docker image from a Dockerfile. The -t flag allows you to tag your image with a name and version (tag). The path_to_dockerfile is where your Dockerfile is located.

Analogy: Imagine you're a chef, and the Dockerfile is your recipe. Running docker build is like following the recipe to create a dish. The final dish (image) can be saved and reused whenever needed.

  1. docker run

Command:

docker run -d --name

Explanation: This command creates and starts a container from a specified Docker image. The -d flag runs the container in the background (detached mode), and --name assigns a name to the container.

Analogy: Think of this as serving the dish (from the previous analogy) to a customer. Running docker run is like plating the dish and serving it, so the customer can start eating. Each container is like a separate instance of the dish, cooked and served individually.

  1. docker volume

Command:

docker volume create

Explanation: This command creates a volume that can be used to persist data beyond the lifespan of a container. Volumes allow data to be shared between the host system and the container or between multiple containers.

Analogy: Imagine the container is a small, temporary kitchen. If you want to store ingredients (data) so that they remain available even after the kitchen closes, you'd use a storage room (volume). No matter how many times the kitchen (container) opens and closes, the ingredients in the storage room remain safe.

  1. docker-compose

Command:

docker-compose up -d

Explanation: docker-compose is a tool to define and run multi-container Docker applications. Using a docker-compose.yml file, you can specify all the services (containers), networks, and volumes that are needed to run your application. The up command starts and runs everything defined in the docker-compose.yml file, and the -d flag runs them in the background.

Analogy: Think of docker-compose as an event planner who sets up all the dishes and stations for a buffet. Instead of manually serving each dish (running each container individually), the event planner prepares everything at once and starts the event. The docker-compose.yml file is the plan or layout for the entire event.

Examples for Better Understanding:

  1. Building an Image:

docker build -t myapp:1.0 .

This command reads the Dockerfile in the current directory (.) and builds an image named myapp with version 1.0.

  1. Running a Container:

docker run -d --name webapp -p 8080:80 myapp:1.0

This starts a container named webapp from the myapp:1.0 image. It also maps port 8080 on the host machine to port 80 in the container, allowing you to access the application via http://localhost:8080.

  1. Creating and Using a Volume:

docker volume create app_data
docker run -d --name db_container -v app_data:/var/lib/mysql mysql:latest

The first command creates a volume named app_data. The second command runs a container named db_container and mounts app_data to /var/lib/mysql inside the container, so that the database data is preserved even if the container stops.

  1. Using Docker Compose: docker-compose.yml example:

version: '3'
services:
web:
image: myapp:1.0
ports:
- "8080:80"
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:

Running docker-compose up -d will start two containers (web and db) with the specified configurations. This is like setting up a mini environment with a web server and a database.

These commands should give you a solid foundation to start working with Docker, and the analogies can help make the concepts clearer!

Top comments (0)