DEV Community

Cover image for A Quick Guide to Command Your Docker Containers
Raza
Raza

Posted on

A Quick Guide to Command Your Docker Containers

Basic Container Operations

Run a Container

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode

Starts a new interactive container using the Ubuntu image.

Build an Image

docker build -t my-custom-image:latest .
Enter fullscreen mode Exit fullscreen mode

Creates a custom Docker image named 'my-custom-image' from the current directory.

Pull an Image

docker pull nginx:latest
Enter fullscreen mode Exit fullscreen mode

Downloads the latest version of the Nginx image from the Docker Hub.

Push an Image

docker push my-custom-image:latest
Enter fullscreen mode Exit fullscreen mode

Uploads the custom image 'my-custom-image' to a Docker registry.

List Images

docker images
Enter fullscreen mode Exit fullscreen mode

Displays a list of locally available Docker images.

Container Lifecycle Management

Stop a Container

docker stop my_container
Enter fullscreen mode Exit fullscreen mode

Gracefully stops the running container 'my_container'.

Start a Container

docker start my_container
Enter fullscreen mode Exit fullscreen mode

Resumes execution of the stopped container 'my_container'.

Restart a Container

docker restart my_container
Enter fullscreen mode Exit fullscreen mode

Stops and then starts the container 'my_container'.

Kill a Container

docker kill -s SIGTERM my_container
Enter fullscreen mode Exit fullscreen mode

Sends a termination signal to forcefully stop the running container 'my_container'.

Remove a Container

docker rm my_container
Enter fullscreen mode Exit fullscreen mode

Deletes the container 'my_container'.

Image Management

Remove an Image

docker rmi my-custom-image:latest
Enter fullscreen mode Exit fullscreen mode

Deletes the image 'my-custom-image' with the latest tag.

Create Image from Changes

docker commit my_container my-custom-image:latest
Enter fullscreen mode Exit fullscreen mode

Creates a new image 'my-custom-image' from changes made in a running container 'my_container'.

Save Images to Archive

docker save -o my-images.tar.gz my-custom-image:latest
Enter fullscreen mode Exit fullscreen mode

Saves the image 'my-custom-image' with the latest tag to a tar archive.

Load Image from Archive

docker load -i my-images.tar.gz
Enter fullscreen mode Exit fullscreen mode

Loads an image from the tar archive 'my-images.tar.gz'.

Container Interaction

Execute Command in Container

docker exec -it my_container bash
Enter fullscreen mode Exit fullscreen mode

Runs an interactive bash shell in the running container 'my_container'.

Fetch Container Logs

docker logs my_container
Enter fullscreen mode Exit fullscreen mode

Retrieves and displays the logs of the container 'my_container'.

Inspect Container or Image

docker inspect my_container
Enter fullscreen mode Exit fullscreen mode

Retrieves detailed information about the container 'my_container'.

Copy Files Between Container and Host

docker cp my_container:/path/in/container /path/on/host
Enter fullscreen mode Exit fullscreen mode

Copies files or folders between the container 'my_container' and the local filesystem.

System Cleanup and Maintenance

Remove Unused Resources

docker system prune
Enter fullscreen mode Exit fullscreen mode

Cleans up stopped containers, dangling images, and unused networks and volumes.

Miscellaneous Commands

List Port Mappings

docker port my_container
Enter fullscreen mode Exit fullscreen mode

Shows the port mappings of the container 'my_container'.

Display Running Processes

docker top my_container
Enter fullscreen mode Exit fullscreen mode

Lists the running processes inside the container 'my_container'.

Manage Volumes

docker volume ls
Enter fullscreen mode Exit fullscreen mode

Lists the Docker volumes on the host.

Show Docker Version

docker version
Enter fullscreen mode Exit fullscreen mode

Displays information about the installed Docker version.

Show Changes in Container Filesystem

docker diff my_container
Enter fullscreen mode Exit fullscreen mode

Shows the filesystem changes made in the container 'my_container'.

Export Container Filesystem

docker export my_container > my_container_exported.tar
Enter fullscreen mode Exit fullscreen mode

Exports the filesystem of the container 'my_container' to a tar archive.

Import Filesystem to Create Image

docker import my_container_exported.tar my-imported-image:latest
Enter fullscreen mode Exit fullscreen mode

Imports the filesystem from the tar archive 'my_container_exported.tar' to create a new image 'my-imported-image' with the latest tag.

Docker Network Commands

List Docker Networks

docker network ls
Enter fullscreen mode Exit fullscreen mode

Displays a list of Docker networks.

Create a Docker Network

docker network create my_network
Enter fullscreen mode Exit fullscreen mode

Creates a new Docker network named 'my_network'.

Inspect Docker Network

docker network inspect my_network
Enter fullscreen mode Exit fullscreen mode

Retrieves detailed information about the Docker network 'my_network'.

Docker Registry Commands

Docker Login

docker login
Enter fullscreen mode Exit fullscreen mode

Logs in to a Docker registry.

Docker Logout

docker logout
Enter fullscreen mode Exit fullscreen mode

Logs out from a Docker registry.

Docker Volume Commands

Create a Docker Volume

docker volume create my_volume
Enter fullscreen mode Exit fullscreen mode

Creates a new Docker volume named 'my_volume'.

Inspect Docker Volume

docker volume inspect my_volume
Enter fullscreen mode Exit fullscreen mode

Retrieves detailed information about the Docker volume 'my_volume'.

Docker Compose Commands

Docker Compose Up

docker-compose up
Enter fullscreen mode Exit fullscreen mode

Starts services defined in a docker-compose.yml file.

Docker Compose Down

docker-compose down
Enter fullscreen mode Exit fullscreen mode

Stops and removes containers, networks, and volumes defined in a docker-compose.yml file.

Top comments (0)