Docker Command Line Basics
When diving into the world of Docker, getting comfortable with the command line interface (CLI) is essential for managing your containers efficiently. The Docker CLI offers a variety of commands that help you create, manage, and interact with containers and images. In this article, we'll walk through the basic commands you need to know to start your journey with Docker.
Getting Started
Before you start running commands, ensure you have Docker installed on your machine. You can verify your installation by running:
docker --version
This command will return the version of Docker you have installed. If it doesn’t, it’s time to download and install Docker from the official website.
Common Docker Commands
1. docker --help
The best way to start is by understanding the command syntax. Typing docker --help in the terminal provides a helpful overview of the Docker command line interface. You'll find a list of commands, options, and subcommands available to you.
2. docker info
To get information about your Docker installation, use:
docker info
This command returns a wealth of information, including the number of containers, images, and the storage driver in use. It's a great way to understand what resources you have at your disposal.
3. docker version
To check the version of Docker and the APIs in use, run:
docker version
This command provides separate version information for the client and the server, helping you identify any version mismatches that might occur.
Working with Docker Images
Docker images are the building blocks of Docker containers. Here’s how to manage them using the CLI.
4. docker pull
To download an image from Docker Hub (the central repository for Docker images), use the pull command:
docker pull <image-name>
For example:
docker pull ubuntu
This command downloads the latest Ubuntu image to your local machine.
5. docker images
Once you've downloaded images, list them with:
docker images
This command displays a table of available images, including their repository name, tags, and sizes.
6. docker rmi
If you need to remove an image from your local machine, use:
docker rmi <image-name>
For example, to remove the Ubuntu image, run:
docker rmi ubuntu
Be cautious with this command, as attempting to remove an image that's in use will result in an error.
Managing Docker Containers
Now that you know how to work with images, let’s explore how to manage and interact with containers.
7. docker run
The run command is how you create and start a container from an image:
docker run <image-name>
For example:
docker run ubuntu
This command will start an Ubuntu container. If you want to run a command inside the container (for instance, launching the shell), you would do:
docker run -it ubuntu /bin/bash
Here, the -it flags allow you to interact with the container via your terminal.
8. docker ps
To list all currently running containers, use:
docker ps
For a list of all containers (running and stopped), add the -a flag:
docker ps -a
9. docker stop
To stop a running container:
docker stop <container-id>
The <container-id> can be found using the docker ps command.
10. docker start
To restart a stopped container, use:
docker start <container-id>
11. docker rm
If you wish to remove a stopped container, use:
docker rm <container-id>
To remove multiple containers, you can list their IDs or use a wildcard. For example:
docker rm $(docker ps -aq)
This command removes all stopped containers.
Inspecting Containers and Images
Understanding the properties of your containers and images can be crucial for debugging and optimization.
12. docker inspect
To get detailed information about a container or image, use:
docker inspect <container-id>
This command will output JSON data about the specified container, including settings and configuration details.
13. docker logs
To check the logs of a running or stopped container, use:
docker logs <container-id>
Logs can be instrumental in tracing issues or understanding the behavior of your applications running within containers.
Networking and Volume Management
Docker containers can communicate with each other and can store data persistently using volumes.
14. docker network ls
To view existing networks, use:
docker network ls
15. docker volume ls
To list all volumes on your machine:
docker volume ls
Creating a Volume
To create a new volume, use:
docker volume create <volume-name>
Running a Container with a Volume
You can mount a volume when running a container:
docker run -v <volume-name>:<path-in-container> <image-name>
For example:
docker run -v my_volume:/data ubuntu
This command mounts my_volume to /data in the container.
Conclusion
The Docker command line interface is a powerful tool that allows you to perform a myriad of operations on containers and images.
By mastering these basic commands, you're laying a solid foundation to work more complex Docker setups. As you progress, explore other commands, such as docker exec to run commands in a running container, or docker build to create images from your Dockerfile. Happy Dockering!
With practice, you'll find that interacting with Docker through the command line becomes second nature. Enjoy your journey in the exciting realm of containerization!