Top Docker Commands Every User Should Master
Posted on September 19, 2024 • 6 min read • 1,277 wordsDiscover the top Docker commands every developer must master, including Docker images, containers, and Docker Compose. Streamline container management and boost productivity with essential tips on docker run, docker pull, docker build, and more
Docker is a powerful tool that simplifies the process of creating, deploying, and managing applications. Mastering its core commands will not only boost your productivity but also enhance the way you work with containers. In this guide, we’ll focus on essential Docker commands, organized into three key areas: Docker Images, Docker Containers, and Docker Compose.
Docker images are the foundation of containers. They are immutable files that contain the code, runtime, libraries, and dependencies needed to run your application. Understanding how to work with Docker images is crucial for efficient containerization.
The docker pull
command fetches an image from a remote registry (such as Docker Hub) to your local machine.
docker pull [IMAGE_NAME]:[TAG]
docker pull node:14
In this example, Docker pulls version 14
of the Node.js image from Docker Hub.
Why It’s Important: Ensures you’re using the most up-to-date or specific image version.
This command lists all the Docker images available on your local system.
docker images
Why It’s Important: Helps you manage and track the images you’ve downloaded or built.
When you’re developing your own applications, you’ll need to build images. The docker build
command builds an image from a Dockerfile, allowing you to define the environment for your app.
docker build -t [IMAGE_NAME] [PATH_TO_DOCKERFILE]
docker build -t myapp .
In this case, Docker will search for a Dockerfile
in the current directory (.
) and create an image named myapp
.
Why It’s Important: Allows you to customize and build images tailored to your application needs.
The docker rmi
command is used to remove images that are no longer needed, freeing up disk space.
docker rmi [IMAGE_NAME]
docker rmi node:14
Why It’s Important: Regularly removing unused images helps optimize disk space and system performance.
Docker containers are the running instances of Docker images. They encapsulate an application and its dependencies in a consistent environment, ensuring portability across different systems.
The docker run
command creates and starts a container from a Docker image. This command is one of the most commonly used in Docker workflows.
docker run [OPTIONS] [IMAGE_NAME]
docker run -d -p 8080:80 nginx
In this example, an NGINX server container runs in detached mode (-d
), with port 80 of the container mapped to port 8080 on the host.
Key Options:
-d
: Runs the container in detached mode (background).-p
: Maps container ports to the host.Why It’s Important: Powers up containers with custom configurations and enables you to map ports and volumes as needed.
To view the currently running containers, use docker ps
. This command provides a list of active containers and details about their status.
docker ps
Why It’s Important: Essential for monitoring active containers and diagnosing issues.
The docker exec
command lets you run commands inside a running container. This is especially useful for debugging and making changes on the fly.
docker exec [OPTIONS] [CONTAINER_NAME] [COMMAND]
docker exec -it mycontainer bash
This command opens an interactive bash shell (-it
) inside the running container mycontainer
.
Why It’s Important: Allows real-time interaction with containers, aiding in debugging or runtime modifications.
The docker stop
command gracefully shuts down a running container by sending a SIGTERM
signal, allowing it to close processes cleanly.
docker stop [CONTAINER_NAME]
docker stop mycontainer
Why It’s Important: Ensures a safe shutdown of running containers without abrupt termination.
After stopping a container, you can remove it from your system using the docker rm
command.
docker rm [CONTAINER_NAME]
docker rm mycontainer
Why It’s Important: Removes unused containers to free up resources and maintain a clean environment.
View the logs generated by a running or stopped container using the docker logs
command. This is extremely helpful for diagnosing issues with containerized applications.
docker logs [CONTAINER_NAME]
docker logs mycontainer
Why It’s Important: Crucial for monitoring container output, performance, and troubleshooting errors.
For applications requiring multiple containers (e.g., a web app with a database), managing them individually can be tedious. Docker Compose simplifies this process by using a single YAML file to define, start, and manage multiple containers.
The docker-compose up
command brings up all the services (containers) defined in a docker-compose.yml
file.
docker-compose up [OPTIONS]
docker-compose up -d
Here, Docker Compose starts all the services in detached mode (-d
).
Why It’s Important: Efficiently spins up complex applications with multiple containers using a single command.
When you’re done working, use docker-compose down
to stop and remove all containers, networks, and volumes defined in the Docker Compose file.
docker-compose down
Why It’s Important: Cleanly shuts down all services and removes associated resources, helping maintain a clean environment.
Similar to docker logs
, but for multiple containers, docker-compose logs
displays the logs for all services managed by Docker Compose.
docker-compose logs [SERVICE_NAME]
docker-compose logs web
Why It’s Important: Allows you to monitor logs for all services in a multi-container application from a single command.
Command | Usage | Example |
---|---|---|
docker build | docker build [OPTIONS] PATH | URL | - | docker build -t myimage:latest . |
docker pull | docker pull [OPTIONS] NAME[:TAG | @DIGEST] | docker pull ubuntu:latest |
docker images | docker images [OPTIONS] [REPOSITORY[:TAG]] | docker images |
docker rmi | docker rmi [OPTIONS] IMAGE [IMAGE…] | docker rmi myimage:latest |
docker tag | docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] | docker tag myimage:latest myrepo/myimage:latest |
docker save | docker save [OPTIONS] IMAGE [IMAGE…] | docker save -o image_backup.tar myimage:latest |
docker load | docker load [OPTIONS] | docker load -i image_backup.tar |
docker history | docker history [OPTIONS] IMAGE | docker history myimage:latest |
docker inspect | docker inspect [OPTIONS] NAME | ID [NAME | ID…] | docker inspect myimage |
docker prune | docker image prune [OPTIONS] | docker image prune |
Command | Usage | Example |
---|---|---|
docker run | docker run [OPTIONS] IMAGE [COMMAND] [ARG…] | docker run -d –name my_container nginx |
docker ps | docker ps [OPTIONS] | docker ps |
docker stop | docker stop [OPTIONS] CONTAINER [CONTAINER…] | docker stop container_name |
docker start | docker start [OPTIONS] CONTAINER [CONTAINER…] | docker start container_name |
docker restart | docker restart [OPTIONS] CONTAINER [CONTAINER…] | docker restart container_name |
docker exec | docker exec [OPTIONS] CONTAINER COMMAND [ARG…] | docker exec -it container_name bash |
docker rm | docker rm [OPTIONS] CONTAINER [CONTAINER…] | docker rm container_name |
docker logs | docker logs [OPTIONS] CONTAINER | docker logs container_name |
docker inspect | docker inspect [OPTIONS] NAME | ID [NAME | ID…] | docker inspect container_name |
docker top | docker top CONTAINER [ps OPTIONS] | docker top container_name |
Command | Usage | Example |
---|---|---|
docker-compose up | docker-compose up [options] [SERVICE…] | docker-compose up -d |
docker-compose down | docker-compose down [options] | docker-compose down -v |
docker-compose build | docker-compose build [options] [SERVICE…] | docker-compose build service_name |
docker-compose start | docker-compose start [SERVICE…] | docker-compose start service_name |
docker-compose stop | docker-compose stop [SERVICE…] | docker-compose stop service_name |
docker-compose restart | docker-compose restart [SERVICE…] | docker-compose restart service_name |
docker-compose logs | docker-compose logs [options] [SERVICE…] | docker-compose logs -f service_name |
docker-compose exec | docker-compose exec [options] SERVICE COMMAND [ARGS…] | docker-compose exec service_name bash |
docker-compose ps | docker-compose ps [options] [SERVICE…] | docker-compose ps |
docker-compose config | docker-compose config [options] | docker-compose config |
Mastering Docker’s commands for working with images, containers, and Docker Compose is essential for any developer or system administrator. These commands not only streamline your workflow but also help you build, run, and manage applications more effectively. Whether you’re pulling images, orchestrating complex multi-container applications with Docker Compose, or troubleshooting with logs, these commands form the core of Docker’s functionality.