Docker Basic Commands Cheat Sheet


Docker is an indispensable tool for modern developers, allowing you to create, deploy, and run applications inside containers. These containers package an application along with all its dependencies, ensuring consistency across different environments. To work effectively with Docker, it’s crucial to understand its basic commands. Here’s a cheat sheet covering the fundamental Docker commands that every developer should know.

Setting Up Docker

Before diving into the commands, ensure Docker is installed on your system. You can download Docker from the official website and follow the installation instructions for your operating system. DevOps training can also provide guidance on setting up Docker. Once installed, verify the installation by running:

docker --version                                             

Basic Docker Commands

1. docker run: This command creates and starts a container from an image. If the image isn’t available locally, Docker pulls it from Docker Hub.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]                

 Example:

docker run -d -p 80:80 --name mynginx nginx                  

 This command runs an NGINX container in detached mode (`-d`), maps port 80 of the host to port 80 of the container (`-p`), and names the container "mynginx".

2. docker ps: Lists running containers. Add the `-a` option to include stopped containers.

docker ps                                                    

docker s -a

3. docker images: Lists all images available locally.

docker images                                                                                                             

4. docker pull: Downloads an image from Docker Hub.

docker pull IMAGE                                            

Example:

docker pull ubuntu                                           

5. docker exec: Runs a command inside a running container.

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]             

Example:

docker exec -it mynginx /bin/bash                            

This command opens an interactive terminal (`-it`) inside the "mynginx" container.

6. docker stop: Stops a running container.

docker stop CONTAINER                                        

Example:

docker stop mynginx                                          

7. docker start: Starts a stopped container.

docker start CONTAINER                                       

Example:

docker start mynginx                                         

8. docker restart: Restarts a running or stopped container.

docker restart CONTAINER                                     

Example:

docker restart mynginx                                       

9. docker r: Removes one or more stopped containers.

docker rm CONTAINER                                          

Example:

docker rm mynginx                                                                                                                   

10. docker rmi: Removes one or more images from your local storage.

docker rmi IMAGE                                             

Example:

docker rmi ubuntu                                            


Dockerfile and Building Images

To create custom Docker images, you need to write a Dockerfile and build it.

1. Dockerfile: A text document containing instructions on how to build a Docker image.

Example of a simple Dockerfile:

Dockerfile                                                   

FROM ubuntu:latest                                           

RUN apt-get update && apt-get install -y nginx               

COPY . /usr/share/nginx/html                                 

CMD ["nginx", "-g", "daemon off;"]                           

2.docker build: Builds an image from a Dockerfile.

docker build [OPTIONS] PATH | URL | -                        

Example:

docker build -t mynginximage .                               

This command builds an image named "mynginximage" from the Dockerfile in the current directory.


Docker Networking

Docker allows you to create and manage networks for your containers.

1.docker network ls: Lists all networks.

docker network ls                                            

2.docker network create: Creates a new network.

docker network create NETWORK_NAME                           

Example:

docker network create mynetwork                              

3.docker network connect: Connects a container to a network.

docker network connect NETWORK_NAME CONTAINER                

Example:

docker network connect mynetwork mynginx                     

4.docker network disconnect: Disconnects a container from a network.

docker network disconnect NETWORK_NAME CONTAINER             

Example:

docker network disconnect mynetwork mynginx                  


Docker Volumes

Volumes are used for persistent storage in Docker.

1.docker volume ls: Lists all volumes.

docker volume ls                                             

2.docker volume create: Creates a new volume.

docker volume create VOLUME_NAME                             

Example:

docker volume create myvolume                                

3.docker run with volumes: Mounts a volume to a container.

docker run -v VOLUME_NAME:/path/in/container IMAGE           

Example:

docker run -d -v myvolume:/data --name mynginx nginx         

4.docker volume rm: Removes a volume.

docker volume rm VOLUME_NAME                                 

Example:

docker volume rm myvolume                                    


Inspecting and Logging

1.docker inspect: Returns detailed information on Docker objects (containers, images, volumes, etc.).

docker inspect CONTAINER|IMAGE|VOLUME                        

Example:

docker inspect mynginx                                       

2. docker logs: Fetches the logs of a container.

docker logs [OPTIONS] CONTAINER                              

Example:

docker logs mynginx                                          


Cleaning Up

1. docker system prune: Removes all stopped containers, unused networks, images, and build cache.

docker system prune                                          

2. docker container prune: Removes all stopped containers.

docker container prune                                       

3. docker image prune: Removes unused images.

docker image prune                                           

4. docker volume prune: Removes unused volumes.

docker volume prune                                          

5. docker network prune: Removes unused networks.

docker network prune                                         


Conclusion

Getting familiar with these basic Docker commands is essential for efficiently managing containers and images. Whether you’re developing a new application, running tests, or deploying software, these commands will help you get the job done. As you gain more experience with Docker and possibly through DevOps courses, you'll find that these commands are just the beginning. Docker’s flexibility and power extend far beyond basic usage, enabling you to create complex, scalable, and reliable application environments. Keep this cheat sheet handy as you work with Docker, and you’ll quickly become proficient in using this powerful tool. 

For more information: What Is A Docker Container? How Do You Create, Start And Stop Containers?


Comments

Popular posts from this blog

How to Install JUnit in Eclipse

Docker-Compose Cheat Sheet