Docker commands - see 8 most commonly used basic commands
If you work with Linux systems, you must have undoubtedly heard about the frequently used Docker commands. Simply put, Docker is a software that allows the creation of containers on the Linux system.
Thus, there are numerous Docker commands, which can be adopted. However, many professionals or beginners in the field are still confused about which are the basic commands most used in the field.
That’s why, thinking of helping you, we developed this complete content, with everything you need to know about Docker commands and their applications. Just read on and check it out!
8 basic Docker commands and when to use them
As in any other area, to execute Docker functions there are several paths, possibilities and, consequently, commands.But there are always the most used, with a simpler and more intuitive structure. After all, one of the main objectives of Docker technology is precisely to optimize processes, improve infrastructure, and guarantee system security.
In order for all these points to be achieved, basic commands, which perform indispensable functions, can be applied. We made a “top 8”, with these Docker commands below.
1. Docker run
The Docker Run command basically creates a container with an image, even if that image is not stored on your host.
With it, the image is guaranteed to be inserted, since Docker Run is directed to the central repository and brings the image to its host in order to create the container in an appropriate manner.
The code structure to execute it is:
docker run [image name]
2. Docker pull
Unlike Docker run, Docker pull is responsible for downloading to the host the image found by Docker images (which will be explained later).
Its coded structure is equivalent to:
docker pull (parameter)
Thus, it is possible to insert the images into the host and then verify that the procedure was successful and the image stored.
3. Docker create
As its name suggests, Docker create is responsible for creating a new container layer, which is writable, on top of the selected image, which can then be executed normally, using a command such as Docker run or start.
To better understand, it is enough to consider how the code structure is formed:
docker create [options] image [command]
At the end of the process, simply check whether the container status is evaluated as “created”, or created, translating into Portuguese. If so, your procedure was successful.
4. Docker stop
Totally contrary to the previous command, Docker stop is used to shut down or for a container created. The procedure will be executed and, if no response is received, the container will receive the command “sigkill” after 10 seconds.
The Docker stop structure is very simple and intuitive, just add the name of the container that you want to shut down or stop working:
docker container stop [container name]
It is necessary to correctly identify the container by its exact name, so that other containers with a similar name are not harmed or stopped.
5. Docker ps
This Docker ps command simulates the list command. This means that viewing all the containers created, for example, can be facilitated.
The structure of this command must be combined with the appropriate options according to your viewing needs and desire. The code used is:
docker ps [options]
Some available possibilities, which allow the combination of commands, are the options mentioned on the official website of the company Docker:
- —all, -a, showing all containers running;
- —filter, -f, responsible for filtering the output based on the conditions provided;
- —format, which shows all containers printed, using the Go template model;
- —last, -n-1, capable of showing only the number of last containers created (includes all states, whether running or not);
- —latest, -l, responsible for showing the last container created (which may also include all states);
- —no-trunc, prevents the output from being “truncated”;
- —quiet, -q, which allows the display of only the IDs of all containers shown;
- —size, -s, capable of calculating and presenting the total size of the files.
6. Docker build
This command is responsible for creating an image using a file called Dockerfile and a contextualization, which represents the files located on the specified path or URL.
Its structure boils down to:
docker build [OPTIONS] PATH | URL | -
In this way, it is possible to create the image in a customized way, compressing construction contexts and considering some images constructed from a cache source.
7. Docker exec
To execute a command inside a container that is already running, it is necessary to resort to the help of the Docker exec command. Only it can start any command under these conditions.
Like the other command examples, Docker exec also features its own code structure:
docker exec [options] CONTAINER COMMAND [ARG…]
To complement its use, there is the possibility of adding options, with different functions, according to the table below, translated from the official Docker website:
-
- —detach, -d, which guarantees the separate mode by executing the command in the background;
- —detach-keys, capable of replacing the key sequence to detach a container;
- —interactive, -i, which presents the possibility of keeping the STDIN open, even if it is not connected;
- — privileged, which grants some extended privileges to the command;
- —user, -u, which represents the username or UID (format: [:]);
- —detach, -d, which guarantees the separate mode by executing the command in the background;
-
—tty, -t, responsible for allocating a pseudo-TTY;
-
and many others.
8. Docker images
Docker images is responsible for managing all container images and has a sequence of subcommands, such as:
- docker image build;
- docker image history;
- docker image import;
- docker image inspect;
- docker image load;
- docker image ls;
- docker image prune;
- docker image pull;
- docker image push;
- docker image rm;
- docker image save;
- and docker image tag;
All of these points have unique characteristics, in addition to completely specific functionalities and accesses for containers.
Also check out our content with all the main processes and commands in Linux .
Conclusion
The Docker concept may seem like a “seven-headed animal” to many beginners. Despite actually being a more complicated aspect of the entire Linux system, some professionals need to learn how to perform functions and perform tasks in Docker, such as developers.
So, thinking of helping you, the Linux Certification developed a complete Docker for DevOps course. With it, you can:
- understand why to use containers instead of Virtual Machine;
- learn how to do the Docker Installation;
- build images and much more.
It is worth visiting and enrolling in this quality course, taught by a teacher who truly understands the subject and has over 15 years of experience with the Linux universe.