10 Essential Docker Commands Every Developer Should Bookmark
This article provides a concise, step‑by‑step guide to the most frequently used Docker commands for development, including container creation, inspection, image management, logging, execution, networking, and file copying, with clear examples and parameter explanations.
Docker is a core component of modern large‑scale architectures. Below is a practical reference of the most commonly used Docker commands for developers.
1. docker run – Start a container (core command)
Command syntax: docker run [options] IMAGE Example – launch a Redis container: docker run -d --name redis -p 6379:6379 redis Parameter table: -d – run in background (detached) --name – assign a container name -p – map host port to container port
After execution the host port 6379 maps to the Redis container.
2. Start a Spring Boot service
docker run -d --name user-service -p 8080:8080 user-service:v1The service becomes reachable at http://localhost:8080.
3. docker ps – List running containers
docker psExample output:
CONTAINER ID IMAGE
a12345 redis
b67890 mysqlUse docker ps -a to include stopped containers.
4. docker images – List local images
docker imagesExample output:
REPOSITORY TAG
user-service v1
mysql 8.0Common operations: docker images to view, docker rmi IMAGE_ID to delete unused images.
5. docker build – Build an image from a Dockerfile
Sample Dockerfile:
FROM openjdk:17
COPY app.jar .
ENTRYPOINT ["java","-jar","app.jar"]Build command: docker build -t user-service:v1 . Parameters: -t – tag the image name . – use the current directory as build context
6. docker logs – View container logs
Basic usage: docker logs CONTAINER_NAME Example – view recent 100 lines of the user-service logs: docker logs --tail 100 user-service Use -f to follow logs in real time.
7. docker exec – Execute a command inside a running container
Enter a Redis container: docker exec -it redis bash Inside you can run ls, ps -ef, or test the service with curl localhost:6379.
8. docker stop and docker start – Stop and restart containers
docker stop redis
docker start redisStopping changes the container state to stopped; starting restores it.
9. docker rm – Remove containers
Remove a container: docker rm redis Force removal (even if running): docker rm -f redis Note: removing a container does not delete its image.
10. docker cp – Copy files between host and container
Copy a configuration file from host to container:
docker cp config.yml user-service:/app/config.ymlCopy a log file from container to host: docker cp user-service:/app/log.txt ./log.txt Typical uses: modify config files, export logs, retrieve build artifacts.
11. docker network – Manage container networks
List networks: docker network ls Create a custom network: docker network create app-net Run a container attached to the network:
docker run --network app-net -d --name redis redisOther services (e.g., user-service, MySQL) can join the same network and communicate via container names, e.g., redis:6379.
These commands cover the essential Docker workflow for development, debugging, and deployment in microservice environments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
