Docker Commands Cheat Sheet

Updated: October 12th, 2023, 06:13:48 IST
Published: October 11th, 2023
Docker Commands Cheat Sheet
Title: Docker Commands Cheat Sheet

Docker is a tool that helps you build and run applications in containers. Containers are like isolated boxes that hold everything your application needs to run, including code, runtime, system tools, system libraries and settings. This makes it easy to package and deploy your applications to different environments, without having to worry about any compatibility issues.

Here is a simple explanation to help you understand Docker

Imagine you are a chef and you want to open a restaurant. You need to buy all the ingredients, cook the food, and serve it to your customers. But you don't want to have to worry about all the logistics of running a restaurant, like cleaning the dishes and doing the laundry.

Docker is like a kitchen for your application. It provides everything you need to cook and serve your food, without having to worry about the logistics. You can focus on your application, and Docker will take care of the rest.

Docker is a popular tool for developing and deploying applications, and it is used by companies of all sizes. It is a great way to make your applications more portable, scalable, and reliable.

Docker General Commands

  1. docker version – Displays detailed information about your Docker CLI and daemon versions.
  2. docker system info – Lists data about your Docker environment, including active plugins and the number of containers and images on your system.
  3. docker help – View the help index, a reference of all the supported commands.
  4. docker <command> --help – View the help information about a particular command, including detailed information on the supported option flags.

Manage Containers

1. List Running Containers:

docker ps - See what containers are currently running.

2. List All Containers:

docker ps -a - View all containers, including the stopped ones.

3. Attach to a Container:

docker attach <container> - Jump into a container's "world" and see what's happening inside.

4. Create a Snapshot:

docker commit <container> new-image:latest - Take a picture of a container's current state and save it as a new image.

5. Inspect a Container:

docker inspect <container> - Get detailed information about a container in a format computers understand.

6. Forcefully Stop a Container:

docker kill <container> - Put a stop to a misbehaving container by force.

7. Rename a Container:

docker rename <container> my-container - Give your container a new name.

8. Pause and Unpause:

docker pause <container> and docker unpause <container> - Temporarily freeze and unfreeze processes in a container.

9. Stop a Container:

docker stop <container> - Gracefully stop a running container.

10. Start a Container:

docker start <container> - Resurrect a previously stopped container.

11. Delete a Container:

docker rm <container> - Remove a container by name or ID. Use the -f flag to forcefully delete a running container.

These commands help you manage your containers, making it easier to work with your Docker "lunchboxes" for different programs.

Docker exec command

1. Run a Command Inside a Container:

docker exec <container> <command> - Imagine you want to send a message to someone inside a container. This command lets you do just that. You specify which container you want to talk to and what you want to say (the command), and it gets the job done.

With docker exec, you can execute commands like you would if you were inside the container, without actually having to enter the container itself. It's like sending instructions to someone in the container through a magic window!

2. Entering Inside a Container:

To enter inside a Docker container and interact with it directly, you can use the docker exec command with a shell command, like bash or sh. Here's how you can do it:

Find the name or ID of the container you want to enter. You can do this by running:

docker ps -a

This command lists all the running containers along with their names or IDs.

Use the docker exec command to start an interactive shell session inside the container. Replace with the name or ID of the container you want to enter, and specify the shell you want to use (usually bash or `sh). For example:

docker exec -it <container> bash

Replace <container> with the actual name or ID of the container.

You will now be inside the container and can run commands as if you were on a regular command line. When you're done, you can exit the container by typing exit.

Keep in mind that you need to have a shell like bash or sh installed in the container for this to work. If the container is running a different shell or doesn't have one, you might need to use the appropriate shell command.

Using Containers

A container is a runtime instance of a docker image. A container will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.

Create and run a container from an image, with a custom name:

docker run --name <container_name> <image_name>

 

Run a container with and publish a container’s port(s) to the host.

docker run -p <host_port>:<container_port> <image_name>

 

Run a container in the background

docker run -d <image_name>

 

Start or stop an existing container:

docker start|stop <container_name> (or <container-id>)

 

Remove a stopped container:

docker rm <container_name>

 

Open a shell inside a running container:

docker exec -it <container_name> sh

 

Fetch and follow the logs of a container:

docker logs -f <container_name>

 

To inspect a running container:

docker inspect <container_name> (or <container_id>)

 

To list currently running containers:

docker ps

 

List all docker containers (running and stopped):

docker ps --all

 

View resource usage stats

docker container stats

 

Using Images

Docker images are a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

 

Build an Image from a Dockerfile

docker build -t <image_name>

 

Build an Image from a Dockerfile without the cache

docker build -t <image_name> . –no-cache

 

List local images

docker images

 

Delete an Image

docker rmi <image_name>

 

Remove all unused images

docker image prune

 

Using Volumes

Read More: Docker Documentation

docker volume ls

docker volume rm VOLUME_NAME

docker volume inspect VOLUME_NAME