fbpx Docker Commands: docker run, pull, build, exec and More | Certificação Linux

Docker Commands: 8 Essential Commands You Should Know

📅 11/19/2021 ⏱ 6 min ✍️ Uira Ribeiro
commands
Docker Commands: 8 Essential Commands You Should Know
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

If you work with Linux systems, you have undoubtedly already heard about Docker commands, which are frequently used. In simple terms, Docker is a software that allows the creation of containers on the Linux system.

Thus, there are countless Docker commands that can be adopted. However, many professionals or beginners in the field are still confused about which are the basic commands, most used in the industry.

That's why, thinking about helping you, we developed this complete content, with everything you need to know about Docker commands and their applications. Just continue reading and check it out!

8 basic Docker commands and when to use them

docker commands importance
Photo: Pexels | Understanding the main Docker commands can make your daily routine easier!

Just like in any other field, to execute Docker functions there are various paths, possibilities and, consequently, commands.

But, there are always the most used ones, 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 ensure system security.

So that all these points are achieved, basic commands, that execute 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 directs itself to the central repository and brings the image to your host, in order to create the container appropriately.

The code structure to execute it is:

<code> docker run [image name] </code>

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:

<code> docker pull (parameter) </code>

Thus, it is possible to insert images on the host and then verify if the procedure was successful and the image stored.

3. Docker create

As the name suggests, Docker create is responsible for creating a new writable container layer over the selected image, which can then be executed normally, through a command such as Docker run or start.

To better understand, just consider how the code structure is formed:

<code> docker create [options] image [command] </code>

At the end of the process, just verify if the container status is evaluated as "created". If yes, your procedure was successful.

4. Docker stop

Completely contrary to the previous command, Docker stop serves to shut down or stop a created container. The procedure will be executed and, if no response is obtained, the container will receive the "sigkill" command, after 10 seconds.

The Docker stop structure is very simple and intuitive, just adding the name of the container you want to shut down or stop:

<code> docker container stop [container name] </code>

It is necessary to correctly identify the container through the exact name, so that other containers with similar names are not harmed or stopped.

5. Docker ps

This Docker ps command simulates the list command. This means that viewing all created containers, for example, can be facilitated.

The structure of this command needs to be combined with the appropriate options, according to your need and desire for viewing. The code used is:

<code> docker ps [options] </code>

Some available possibilities, that allow the combination of commands, are the options mentioned on the official Docker company website:

  • --all, -a, which show all running containers;
  • --filter, -f, responsible for filtering the output based on the provided conditions;
  • --format, which displays all printed containers, 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 displaying only the IDs of all presented containers;
  • --size , -s, capable of calculating and displaying the total file size.

6. Docker build

This command is responsible for creating an image through a file called Dockerfile and a context, which represents the files located in the specified path or URL.

Its structure can be summarized as:

<code> docker build [OPTIONS] PATH | URL | - </code>

This way, it is possible to create the image in a customized way, compressing build contexts and considering some images built from a cache source.

7. Docker exec

To execute a command inside a container that is already running, it is necessary to use the Docker exec command. Only it can start any command under these conditions.

Like the other command examples, Docker exec also has its own code structure:

<cod> docker exec [options] CONTAINER COMMAND [ARG...]</cod>

To complement its use, there is the possibility of adding options, with differentiated functions, according to the table below, translated from the official Docker website:

  • --detach , -d, which ensures detached mode, running the command in the background;
  • --detach-keys, capable of overriding the key sequence to detach a container;
  • --interactive , -i, which allows keeping STDIN open even if not attached;
  • --privileged, which gives extended privileges to the command;
  • --user , -u, which represents the username or UID (format: <name | uid> [: <group | gid>]);
  • --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 these points have unique characteristics, in addition to functionalities and accesses that are completely specific to containers.

Also check out our content with all the main Linux processes and commands.

Conclusion

The Docker concept may seem like a "tough nut to crack" for many beginners. Although it is, indeed, a more complicated aspect of the entire Linux system, some professionals need to learn how to perform functions and execute tasks in Docker, such as developers.

Thus, thinking about helping you, Certificação Linux has developed a complete Docker course for DevOps. With it, you can:

  • understand why to use containers instead of Virtual Machine;
  • learn how to install Docker;
  • build images and much more.

It's worth checking out and enrolling in this quality course, taught by a professor who really knows the subject and has over 15 years of experience with the Linux universe.

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp