Basic Docker Terminal Commands
Open Command Prompt to check that Docker is installed in your workstation. Entering the command docker
on your terminal should show the following:
This is the list of available subcommands for Docker. To understand what each subcommand does, enter docker-subcommand –help
on the terminal:
Run docker info
and note the following:
- Containers
- Images
- Server Version
This command displays system-wide information. The server version number is important at times, especially when new releases introduce something that is not backward-compatible. Docker has stable and edge releases for their Community Edition.
We will now look at a few commonly used commands.
This command searches
Docker Hub for images:
Docker Hub is the default Docker registry. A Docker registry holds named Docker images. Docker Hub is basically the "GitHub for Docker images". Earlier, we looked at running an Ubuntu container without building one; this is where the Ubuntu image is stored and versioned:
"There are private Docker registries, and it is important that you are aware of this now."? Docker Hub is at hub.docker.com. Some images are hosted at store.docker.com but Docker Store contains official images. However, it mainly focuses on the commercial aspect of an app store of sorts for Docker images and provides workflows for use.
The register page is as shown here:
The log in page is as shown here:
From the results, you can tell how users have rated the image by the number of stars. You can also tell whether the image is official. This means that the image is promoted by the registry, in this case, Docker Hub. New Docker users are advised to use official images since they have great documentation, are secure, promote best practices, and are designed for most use cases. As soon as you have settled on one image, you'll need to have it locally.
Note
Ensure you are able to search for at least one image from Docker Hub. Image variety ranges from operating systems to libraries, such as Ubuntu, Node.js, and Apache.
This command allows you to search from Docker Hub:
For example, docker search ubuntu
.
This command pulls an image from the registry to your local machine:
For example, docker pull ubuntu
.
As soon as this command is running, you'll notice that it is using the default tag: latest
. In Docker Hub, you can see the list of tags. For Ubuntu, they are listed here: https://hub.docker.com/r/library/ubuntu/ plus their respective Dockerfiles:
Download the Ubuntu image profile on Docker Hub from: https://hub.docker.com/r/library/ubuntu/.
Activity 1 — Utilizing the docker pull Command
To get you conversant with the docke
r pull
command.
The goal of this activity is to gain a firm understanding of the docker-pull
CLI, not only by running the listed commands, but also by seeking help on other commands while exploring, through manipulating the built containers.
- Is Docker up and running? Type
docker
on the terminal or command-line application. - This command is used to pull the image from the Docker Hub
.
Image variety ranges from operating systems to libraries, such as Ubuntu, Node.js, and Apache. This command allows you to pull images from Docker Hub:
For example, docker pull ubuntu
.
This command lists the Docker images we have locally:
When we run the command, if we have pulled images from Docker Hub, we will be able to see a list of images:
They are listed according to the repository, tag, image ID, date created, and size. The repository is simply the image name unless it is sourced from a different registry. In this case, you'll have a URL without the http://
and the top level domain (TLD) such as >registry.heroku.com/<image-name>
from the Heroku registry.
This command will check whether the image by the name hello-world
exists locally:
For example, docker run hello-world
:
If the image is not local, it will be pulled from the default registry, Docker Hub, and run as a container, by default.
This command lists the running containers:
If there aren't any running containers, you should have a blank screen with the headers:
Activity 2 — Analyzing the Docker CLI
Ensure you have the Docker CLI running by typing docker
on your terminal.
You have been asked to demonstrate the commands covered so far.
To get you conversant with the Docker CLI. The goal of this activity is to gain a firm understanding of the docker-compose
CLI, not only by running the listed commands, but also by seeking help on other commands while exploring, through manipulating the built containers. The goal is to be flexible enough with the CLI to be able to use it in a real-world scenario such as running an automated script.
- Is Docker up and running? Type
docker
on the terminal or command-line application. - Search for the official Apache image using the CLI, using
docker search apache:
- Attempt to pull the image using
docker pull apache.
- Confirm the availability of the image locally using
docker images.
- Bonus: Run the image as a container using
docker run apache.
- Bonus: Stop the container using
docker stop <container ID>.
- Bonus: Delete the container and the image using
docker rm <contai
ner ID>.