Docker Command Cheatsheet

Docker is a popular platform for developing, shipping, and running applications in containers. To effectively manage Docker containers, it's essential to understand and use Docker commands. In this article, we will introduce some common Docker commands and provide code examples to help you get started.

Basic Docker Commands

Here are some basic Docker commands you may find useful:

Command Description
docker run <image> Run a Docker container from an image
docker ps List running containers
docker ps -a List all containers, including stopped ones
docker images List all images on your system
docker pull <image> Pull an image from a registry
docker stop <container> Stop a running container
docker rm <container> Remove a container
docker rmi <image> Remove an image

Code Examples

Pulling and Running an Image

To pull and run a Docker image, you can use the following commands:

docker pull ubuntu
docker run -it ubuntu bash

The first command pulls the ubuntu image from the Docker Hub registry, while the second command runs a container based on this image interactively with a bash shell.

Listing Containers and Images

To list running containers and images on your system, you can run the following commands:

docker ps
docker images

These commands will display the details of the running containers and available images on your Docker host.

Stopping and Removing Containers

If you want to stop and remove a container, you can use the following commands:

docker stop <container_id>
docker rm <container_id>

Replace <container_id> with the actual ID of the container you want to stop or remove.

Flowchart

flowchart TD;
    A[Pull Image] --> B(Run Container);
    C[List Containers] --> D(List Images);
    E(Stop Container) --> F(Remove Container);

Conclusion

In this article, we have provided an overview of some common Docker commands and demonstrated how to use them with code examples. By mastering these commands, you can efficiently manage Docker containers and images in your development workflow. Experiment with these commands and explore more advanced Docker features to enhance your containerized application development experience. Happy coding with Docker!