Docker Images and Containers Important commands
Check Docker Version:
docker version
Pull a Docker Image:
docker pull [image_name]:[tag]
Build a Docker Image from a Dockerfile:
docker build -t [image_name]:[tag] [directory]
List Docker Images:
docker image ls
Run a Docker Container:
docker run [options] [image_name]
List Running Containers:
docker container ls
Stop a Docker Container:
docker stop [container_id/name]
Remove a Docker Container:
docker rm [container_id/name]
Execute Command in Running Container:
docker exec -it [container_id/name] [command]
Docker Networking
List Docker Networks:
docker network ls
Inspect a Docker Network:
docker network inspect [network_name]
Remove a Docker Network:
docker network rm [network_name]
Docker Volumes (Persistent Data Management)
Create a Docker Volume:
docker volume create [volume_name]
List Docker Volumes:
docker volume ls
Inspect a Docker Volume:
docker volume inspect [volume_name]
Remove Unused Docker Volumes:
docker volume prune
Run Container with Volume:
docker run -d --name [container_name] -e [env_variables] --mount source=[volume_name],target=[path_in_container] [image_name]
Docker Bind Mounts
Run Container with Bind Mount:
docker run -d --name [container_name] --mount type=bind,source=[host_path],target=[path_in_container] [image_name]
Upgrading Containers without Losing Data
Example with MySQL:
Run MySQL Container with Named Volume:
docker run -d -e MYSQL_ROOT_PASSWORD=[password] --name mysqldb_1 --mount source=mysql_db,target=/var/lib/mysql mysql:8.0
Upgrade by Removing Old and Running New Container:
docker rm -f mysqldb_1 docker run -d -e MYSQL_ROOT_PASSWORD=[password] --mount source=mysql_db,target=/var/lib/mysql --name mysqldb_2 mysql:8.2
Certainly, let's continue with additional Docker commands and concepts that complement and extend the summary provided:
Docker Image Management
Remove a Docker Image:
docker rmi [image_name]:[tag]
Tag a Docker Image:
docker tag [source_image]:[tag] [target_image]:[tag]
Push a Docker Image to Docker Hub:
docker login docker push [username]/[image_name]:[tag]
Inspecting Containers and Images
Inspect a Docker Container:
docker inspect [container_id/name]
View the Logs of a Running Container:
docker logs [container_id/name]
View the Port Mappings of a Container:
docker port [container_id/name]
Docker Network and Data Volume CLI Operations
Create a Docker Network:
docker network create [options] [network_name]
Run a Container on a Specific Network:
docker run -d --name [container_name] --network [network_name] [image_name]
Attach a Running Container to a Network:
docker network connect [network_name] [container_id/name]
Detach a Container from a Network:
docker network disconnect [network_name] [container_id/name]
Interactive Containers
Start a Container in Interactive Mode:
docker run -it --name [container_name] [image_name] [command]
For example, to start a Bash session in an Ubuntu container:
docker run -it --name ubuntu_test ubuntu bash
Docker Compose (for Multi-Container Applications)
Start Services Defined in docker-compose.yml:
docker-compose up
Stop and Remove Containers, Networks, and Volumes Created by
up
:docker-compose down
Cleaning Up
Remove All Stopped Containers:
docker container prune
Remove All Unused Images, Containers, Networks, and Volumes:
docker system prune
Add
-a
to remove all unused images not just dangling ones, and--volumes
to remove all unused volumes.To ensure we cover a comprehensive set of Docker commands and concepts, here are some additional points that complement the previous summaries, focusing on advanced usage, docker-compose, and cleanup commands:
Advanced Container Operations
Restart a Docker Container:
docker restart [container_id/name]
Attach to a Running Container: For interactive processes (like a shell), you can use
docker attach
to connect to a running container.docker attach [container_id/name]
Copy Files/Folders between a Container and the Local Filesystem:
docker cp [container_id:name]:[container_path] [local_path] docker cp [local_path] [container_id:name]:[container_path]
Docker Compose for Multi-container Applications
Run a Specific Service Defined in
docker-compose.yml
:docker-compose up [service_name]
Build or Rebuild Services: Useful when you want to build images before starting the containers.
docker-compose build
View Running Containers of a Docker Compose Project:
docker-compose ps
Stop Services Without Removing Them:
docker-compose stop
Remove Stopped Containers Managed by Compose:
docker-compose rm
Cleanup Commands
Remove All Unused or Dangling Images:
docker image prune
To remove all images not used by existing containers, add the
-a
flag.Remove Dangling Volumes: As mentioned,
docker volume prune
will remove all unused volumes. Ensure no data is lost inadvertently by verifying that no containers are using these volumes.
Monitoring and Logs
Monitor Docker Containers: You can monitor the CPU, memory usage, I/O, and network stats for running containers using:
docker stats
Tail Logs of a Container: To follow (tail) the log output of a container, use:
docker logs -f [container_id/name]
Docker Network Insights
Inspecting Network Settings of a Container: To get detailed network settings used by a container:
docker inspect --format='{{json .NetworkSettings.Networks}}' [container_id/name] | jq .
This command requires
jq
to be installed for parsing JSON output. It's useful for debugging or when you need detailed network configuration info.
Conclusion
- we've covered a comprehensive range of Docker commands and concepts, spanning from basic to more advanced operations, including image and container management, networking, persistent data handling with volumes and bind mounts, docker-compose for orchestrating multi-container applications, and various cleanup commands to maintain a tidy environment. This collection should serve as a robust foundation for managing Docker containers and images, setting up and networking containers, ensuring data persistence, and optimizing your Docker environment for development and production use.