Basic Docker Commands
Basic Docker Commands
• Containers: 3
• Running: 1
• Paused: 0
• Stopped: 2
• Images: 3
• Server Version: 18.09.6
• Storage Driver: overlay2
• Backing Filesystem: extfs
• Supports d_type: true Native Overlay Diff: true Logging Driver:
3. Checking History
.
• Shows the history of a docker image with the image name
mentioned in the command
$ docker history httpd
• The ‘docker run’ command is used to create and start a new container
from a Docker image. The basic syntax of the ‘docker run’ command
is as follows:
• docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
• where,
• OPTIONS: optional flags that configure the behaviour of the
container, such as setting environment variables, mounting volumes,
etc.
• IMAGE: the name of the Docker image to be run.
• COMMAND: the command to be executed when the container is
started. If not provided, the default command specified in the Docker
image will be used.
• ARG: optional arguments passed to the command.
-d means
Options Available for executing Docker Run
Command
• Some commonly used options for executing the docker run
command inside a container:
• -it: Runs the container in interactive mode, allowing you to
interact with the container through the command line.
• -d: Runs the container in the background.
• — name: Specifies a name for the container.
• — rm: Automatically removes the container when it exits.
• -p: Maps a host port to a container port.
• -e: Sets an environment variable inside the container.
• -v: Mounts a host volume inside the container.
1st Example : Running a Simple Image:
• Command:
• $ docker run –d –it –e PASSWORD=‘pass@123’ busybox sh
Then check the environment variable whether it is set or not
• Command :
• $ docker exec –it <container Id> env
2. To Set the NAMESPACE with value staging in ubuntu image:
Command is :
To check whether the given environment variable is set or not
2nd Method to set variables through env
file
• Step 1: Create one directory by mkdir
• Mkdir <dir name>
• $ docker ps
8. ps -a
• $ docker ps -a
9. exec
• Access the docker container and run commands inside the container
in bash
•
6. You have a Docker container running a web server and you
want to access the web server from your host machine.
How would you do it?
7. How would you set the environment variables for the database
connection details and application mode with help of run
command?
Solutions
• 1 docker run -it ubuntu /bin/bash
• 2. docker images --filter "reference=*:latest"
• 3. docker run --name temp-c1 ubuntu echo "Hello, world!“
• 4. docker images --filter "reference=webapp*"
• 5. docker images --filter "reference=*:dev"
Docker
Commands
for Images
List Docker Images
1. $ docker images
Or
2. $ docker image ls
Filtering Docker Image List
In order to filter the Docker image list results, you need to use the
“docker images” command with the “–filter” followed by the
filter in the form of a key-value tuple.
$ docker images --filter "<key>=<value>“
With the “–filter” option, you can specify the following keys :
• “reference” : that can be used in order to isolate images having a
certain name or tag;
• “before” : to filter images created “before” a specific point in time;
• “since” : to filter images since a specific point in time (usually
another image creation);
• “label” : if you used the LABEL instruction to create metadata for
your image you can filter them later with this key
• “dangling” : in order to isolate images that are not used anymore.
For Example you want to filter your image list given the name
of your image.
.
• Similarly, if you are working with multiple images of the same
repository, you can add an additional tag separated by a colon
character
• In short, you would have the following output when listing your
images
• $ docker images -a
• REPOSITORY TAG IMAGE ID CREATED SIZE debian 8
00cc44e1e1a2 3 days ago 129MB
• <none> <none> 00b72214a37e 5 days ago 110MB
• Those images or layers are not actively used anymore : they have been “re-tagged”.
To filter images that are defined as “dangling”, you can append
the “dangling=true” option.
for example-->
• In order to list only image IDs on Docker, you have to use the
“docker images” command with the “–quiet” option to
suppress all other columns.
$ docker images -q
Docker Exec Command With
Examples
1.
The most popular usage of the “docker exec” command is to launch
a Bash terminal within a container.
• If the Bash is part of your PATH, you can simply type “bash” and
have a Bash terminal in your container.
• In order to get the results from your command, you are also
binding the standard output and the standard error to the ones
from your host machine.
2. Docker Exec as Root
• In some cases, you are interested in running commands in your
container as the root user.
• In order to execute a command as root on a container, use the
“docker exec” command and specify the “-u” with a value of 0
for the root user.
• Note : simple quotes may not work in your host terminal, you will
have to use double quotes to execute multiple commands.
• For example, let’s say that you want to change the current
directory within the container and read a specific log file in your
container.
• .
To achieve that, you are going to execute two
commands : “cd” to change directory and “cat” to
read the file content
Executing a command in a specific directory
• In some cases, the purpose of executing multiple commands is to
navigate to a directory in order to execute a specific command in
this directory.
• You can use the method we have seen before, but Docker
provides a special option for this.
•
Docker Run vs Exec
Now that we have seen multiple ways of using the “docker exec”
command, you may wonder what is the difference with the
“docker run ” command.
• root@74f86665f0fd:/# exit
• On the other hand, if a container is started, you can start a Bash shell in it
and exit it without the container stopping at the same time.
• $ docker ps
• CONTAINER ID IMAGE COMMAND CREATED STATUS
74f86665f0fd ubuntu:18.04 "/bin/bash" 49 seconds ago Up 48
seconds