Basic Docker Commands

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 65

Basic Docker commands

Course Code: INT332


• $ docker --version

• Docker version 18.09.6, build 481bc77


2. Docker info

• Get detailed information about docker installed on the system


including the kernel version, number of containers and images,
etc.
• $ docker info

• 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

IMAGE CREATED CREATED BY SIZE COMMENT


4 . Downloading Image

When we need to pull the docker image from dockerhub


(docker repository) then ,the following example of pulling the
the image and here we are taking Apache HTTP server image.
$ docker pull httpd
5. Images
To list all the docker images pulled on the system with image
details such as TAG/IMAGE ID/SIZE etc.
$ docker images
6.RUN

• 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:

docker run httpd echo “Hello, World!”


• This command runs a new container from the ‘httpd ’ image
and executes the ‘echo’ command with the argument “Hello,
World!”.
2nd example

Running a Container with a Specific Name:

• $ docker run --name my-container httpd echo “Hello, World!”

• --name : this option is to give the name of container


• my-container: it is the name of container
• Echo: it is the command
• “hello,world!” – it is argument used
• This command runs a new container with the specified name
“my-container” from the httpd image and executes the echo
command with the argument “Hello, World!”.
3rd Example : Setting Environment:
• docker run -e MY_VAR=value httpd env

• This command sets an environment variable ‘MY_VAR’ to the


value “value” and runs a new container from the ‘httpd’ image.
The ‘env’ command is executed to display the list of environment
variables in the container.
3rd Example in Detail
1. set the password with value pass@123 in busybox 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>

• Step 2: Create one file in that directory


cat > f1.env
NAMESPACE=‘mount’
PASSWORD=‘pass987’
Press ctrl c
Step 3:
4th Example

$ docker run -it -d httpd


Here :
 -it flag enables interactive mode, so you can interact with the
container’s command line
 The -d flag runs the container in the background and the httpd
argument specifies the image name to use for the container.
This command will pull the httpd image from Docker Hub if it
is not already present on your local machine, create a container
based on that image, and start it in the background.
7 What’s running?

• To lists all the docker containers are running with container


details.
.

• $ docker ps
8. ps -a

To list all the docker containers running/exited/stopped with


container details

• $ docker ps -a
9. exec
• Access the docker container and run commands inside the container
in bash

$ docker exec -it 09ca6feb6efc bash


09ca6feb6efc --- it is Id of the container
10. Removing container

• Remove the docker container with container id mentioned in


the command.
$ docker rm 9b6343d3b5a0

Run the below command to check if the container got removed or


not.
$ docker ps -a
11. Removing image
• Remove the docker image with the docker image id mentioned in
the command
$ docker rmi fce289e99eb9
..contd..

• To remove the image forcefully

$ docker rmi –f <image id>

docker rmi -f 92fa43a2ff60503fdf250


Untagged: httpd:latest
Untagged:
httpd@sha256:7765977cf2063fec486b63ddea574faf8fbed285f
2b17020fa7ef70a4926cdec
Deleted:
sha256:92fa43a2ff60503fdf250ef0a16d4170aa2a47c4b5a60d4
27724283543a8792a
Practice Q#1

• Run a Docker container named “DB-app" based on the


“mongodb" image, and expose port 80 on the host to port 8082
on the container?
• docker run -d -p 80:8082 --name DB-app mongo
Practice Questions
1. Explain how you would run an interactive container based on the
"ubuntu" image, where you can execute commands within the
container.
2. Filters the images by reference, where the tag is "latest."
3. To run a container named "temp-c1" based on the “ubuntu" image to
execute a one-time command and then exit. How can you achieve this?
4. Filters the images by reference, where the reference starts with
"webapp.“
5. You are managing a Docker registry with images for different
environments, including development, staging, and production. You
need to find all the images that are specifically tagged with "dev" for
development. Write a Docker command to filter and list only those
images.


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.

$ docker images --filter "reference=deb*“

.
• Similarly, if you are working with multiple images of the same
repository, you can add an additional tag separated by a colon
character

$ docker images --filter "reference=debian:8"


Filtering Unused Docker Images
Dangling” images are not images that are not used by any
container anymore. We would define those images as “unused”,
but not as “dangling”.
• “Dangling” images are created whenever you create a new
version of a Docker image with the same “image plus tag”
pair.

• 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.

$ docker images --filter "dangling=true"

REPOSITORY TAG IMAGE ID CREATED SIZE


<none> <none> 00b72214a37e 5 days ago 110MB
Filtering Images on Date

When you want to list Docker images depending on their


creation date or when they were downloaded for the first time.

 If you are looking to list Docker images created before


another image, you would run the following command.
$ docker images --filter "before=<image_name>“
In this case, the “Debian Jessie” image was the most recent one :

to list images created before, we would run the following


command.

$ docker images --filter "before=debian:8"

REPOSITORY TAG IMAGE ID CREATED SIZE


debian latest 971452c94376 3 days ago 114MB
ubuntu latest 72300a873c2c 7 days ago 64.2MB
Also you can choose to list Docker images created after another
image, you would use the “since” keyword instead of the “before”
keyword.

$ docker images --filter "since=<image_name>"

$ docker images --filter "since=ubuntu"

REPOSITORY TAG IMAGE ID CREATED SIZE


debian 8 00b72214a37e 3 days ago 129MB
debian latest 971452c94376 3 days ago 114MB
Listing and Formatting Docker Images
• Docker will by default display the results in a set of five different
columns :
repository, tag, image ID, the creation date and the size of your
image.

• However, in some cases, you may want to have a different output


if you need to process the information in a specific system

for example-->

• In order to list and format Docker images, you have to use


the “docker images” command followed by the “–format”
option and a Go template.
• $ docker images --format <go_template>

Go template is a set of variables that are enclosed in double


curly braces, representing the different columns to be
displayed.

For example, if you want to have the output as the repository


named followed by the image ID, you would run the following
command.
• $ docker images --format "{{.Repository}} has the
following {{.ID}}"
Listing Only Image IDs

• 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 --quiet

$ 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.

• In order to start a Bash shell in a Docker container, execute


the “docker exec” command with the “-it” option and specify
the container ID as well as the path to the bash shell.

• If the Bash is part of your PATH, you can simply type “bash” and
have a Bash terminal in your container.

• $ docker exec -it <container> /bin/bash


Docker Exec Interactive Option (IT)
• If you are familiar with Linux operating systems, you have probably
already heard about the concept of file descriptors.
• Whenever you are executing a command, you are creating three file
descriptors :
• STDIN : also called the standard input that will be used in order to
type and submit your commands (for example a keyboard, a
terminal etc..);
• STDOUT : called the standard output, this is where the process
outputs will be written (the terminal itself, a file, a database etc..);
• STDERR : called the standard error, it is very related to the
standard output and is used in order to display errors.
So how are file descriptors related to the “docker exec“?
• When running “docker exec” with the “-i” option, you are
binding the standard input of your host to the standard input
of the process you are running in the 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.

$ docker exec -u 0 <container> <command>

For example, in order to make sure that we execute the command as


root, let’s have a command that prints the user currently logged in
the container.
Docker Exec Multiple Commands
• In order to execute multiple commands using the “docker
exec” command, execute “docker exec” with the “bash”
process and use the “-c” option to read the command as a
string.
• $ docker exec <container> bash -c "command1 ; command2 ;
• command3“

• 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.

• In order to execute a command within a specific directory in


your container, use “docker exec” with the “-w” and specify
the working directory to execute the command.

• $ docker exec -w /path/to/directory <container> <command>


• $ docker exec -w /var/log 74f86665f0fd cat dmesg

Before this run a container


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.

• The difference between “docker run” and “docker exec” is


that “docker exec” executes a command on a running
container.

• On the other hand, “docker run” creates a temporary


container, executes the command in it and stops the container
when it is done.
• For example, you can execute a Bash shell using the “docker run”
command but your container will be stopped when exiting the
Bash shell.

• $ docker run -it ubuntu:18.04 bash


• root@b8d2670657e3:/# exit
• $ docker ps
• (No containers.)

• 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

• $ docker exec -it 74f86665f0fd bash

You might also like