Docker has become a cornerstone in the modern software development landscape. It empowers developers to package and run applications in a consistent and portable manner, irrespective of the underlying infrastructure. This guide serves as a gentle introduction to Docker, delving into the essential commands that will kickstart your journey into the world of containers.
The first step is to verify that Docker is installed on your system. Run the following command:
sudo docker --version
This command displays the installed Docker version.
To experience the magic of Docker firsthand, let’s run the “hello-world” application:
sudo docker run hello-world
This command fetches the “hello-world” image from Docker Hub and executes it, displaying a simple message on your console.
Docker allows you to manage multiple containers simultaneously. To view a list of all running containers, use the following command:
sudo docker ps
This command displays a table with information about each running container, including its ID, image name, status, and ports.
One of Docker’s key features is the ability to run applications in an isolated environment. To experience this, let’s launch an interactive container based on the Ubuntu image:
sudo docker run -it ubuntu bash
This command not only starts the container but also provides you with a terminal inside the container, allowing you to execute commands and interact with the system as if it were a regular machine.
Docker Hub, a public registry, hosts a vast collection of images. You can search for images by name, description, or other criteria using the following command:
sudo docker search ubuntu
This command searches for images containing the keyword “ubuntu” and displays a list of relevant results.
Often, you may want to run specific commands within a container. To achieve this, you can combine the run command with the -it flag and specify the desired command:
sudo docker run -it ubuntu bash
sudo apt-get update
This example updates the package list inside the Ubuntu container.
For a comprehensive overview of your Docker environment, use the info command:
sudo docker info
This command provides detailed information about the Docker daemon, including its configuration, network settings, and storage options.
By default, docker ps only lists running containers. To view all containers, including those that have stopped, use the -a flag:
sudo docker ps
sudo docker ps -a
The first command displays only running containers, while the second command shows both running and stopped containers.
Docker provides various options for customizing the output of the docker ps command. The -l flag allows you to specify the desired format, such as JSON or table:
sudo docker container ls
sudo docker container ls -a
The container ls command is an alias for docker ps. The first command lists all running containers in a table format, while the second command, with the -a flag, lists all containers (running and stopped) in a table format.
Now that you’ve gained familiarity with basic commands let’s delve into container management, covering operations such as starting, stopping, removing, and restarting containers.
To start a container, use the run command with the -it flag for an interactive session:
sudo docker run -it centos bash
This command starts a container based on the CentOS image and provides you with a terminal inside the container.
To stop a running container, use the stop command:
sudo docker stop <container_id>
Replace <container_id> with the ID of the container you want to stop.
Once you’re done with a container, you can remove it using the rm command:
sudo docker rm <container_id>
This command removes the specified container from your system.
To restart a stopped container, use the restart command:
sudo docker restart <container_id>
This command restarts.
sudo docker container run -it centos:7 bash
sudo: Grants root privileges for container operations. Docker container run: Creates a new Docker container. -it: Allocates a pseudo-terminal (interactive shell) for interacting with the container. centos:7: Specifies the Docker image to use (CentOS version 7). bash: Launches a Bash shell within the container.
sudo yum -y update
sudo: Again, for root privileges (package management often requires them). Yum: I am the package manager for RPM-based systems like CentOS. -y: Automatically confirms any prompts during the update process (use with caution in production environments). Update: Updates system packages to their latest versions.
ls -l
ls: Lists files and directories. -l: Provides detailed information in a long format (including permissions, owner, group, size, and timestamps).
echo "we are here" > example.org
echo: Prints text to the terminal or a file. “we are here”: The text to be written.
: Redirects the output to a file (creates example.org in this case)
ls -l
Lists the directory contents again, showing the newly created example.org.
cat example.org
cat: Displays the contents of a file. example.org: The file to display.
exit
Terminates the interactive shell session within the container.
sudo docker ps
sudo: For root privileges. docker ps: Lists currently running Docker containers.
sudo docker ps -a
-a: Shows all containers, regardless of their running state.
sudo docker container run -it centos:7 bash
Re-runs the container creation command from step 1.
exit
Terminates the shell session within the second container
sudo docker ps -a
Verifies both containers are listed.
sudo docker container start [containerid]
start: Attempts to start a stopped container. [containerid]: Replace this with the actual ID of the container you want to start (obtainable from docker ps -a).
sudo docker ps
Checks if the previously stopped container is now listed as running.
sudo docker container exec [containerid] ps -ef
exec: Executes a command within a running container. [containerid]: The ID of the container to access. ps -ef: Lists all processes running inside the container (similar to ps -aux on Linux).
sudo docker container exec -it [containerid] bash
-it: Allocates a pseudo-terminal for interactive interaction. Provides access to the Bash shell within the specified container, allowing you to execute further commands.
ls -l
Lists directory contents, showing example.org
cat example.org
Displays the contents of example.org created earlier.
Replace [containerid] with the actual container ID in steps 13, 15, and 16. Use docker ps -a to view all containers, including stopped ones.
Also published here.