0% found this document useful (0 votes)
44 views9 pages

Lesson 3 Docker Container Usage

This document provides a comprehensive guide on using Docker commands for managing containers and images. It covers commands such as 'docker info', 'docker images', 'docker pull', 'docker run', and various options for listing, starting, stopping, and deleting containers. Additionally, it explains how to view process information and metadata related to containers and images.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

Lesson 3 Docker Container Usage

This document provides a comprehensive guide on using Docker commands for managing containers and images. It covers commands such as 'docker info', 'docker images', 'docker pull', 'docker run', and various options for listing, starting, stopping, and deleting containers. Additionally, it explains how to view process information and metadata related to containers and images.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Lesson 3 Docker Container Usage

1. Help Command
“docker info” is used to display information about the Docker system,
including its configuration and current status. It provides a detailed summary of
Docker images, the number of containers, the Docker daemon, storage drivers,
networks, and other relevant information.
“docker --help” is used to obtain help information for Docker commands.
If it is executed, all available Docker commands and their options will be listed
for reference.

2. Image Command
1) “docker images” lists all images on the local master.

Corresponding Parameters:
REPOSITORY: the repository source of the image.
TAG: the tag of the image.
IMAGE ID: the ID of the image.
CREATED: the creation time of the image.
SIZE: the size of the image.
(The same repository source can have multiple tags representing different
versions of the repository source. You can define different images using
REPOSITORY: TAG. If a tag version for an image is not defined, Docker will
use the “lastest” image by default.)
“docker images” also provides some options:
-a: list all local images
1
-q: display image id only
--digests: display the image’s digest information
2) “docker pull” is used to download images from a selected repository.

Usage:
“docker pull” image repository address/image name: version
Demonstration (it cannot be downloaded, for reference only): “docker pull”
hiwonder/ros-foxy:1.0.0
For example, download the “debian” image: Press “Ctrl+Alt+T” to open the
command line terminal. Then enter “docker pull debian” and press “Enter”.

Enter “docker images” to view the images on the master, and you can see
a “debian” image has been added to the list of images.

3) “docker search” is used to search the image corresponding to the

2
“DockerHub” repository.
Take researching the “ROS” image for an example. Press “Ctrl+Alt+T” to
open the command line terminal. Then enter “docker search ros” and press
“Enter”.

“docker search” also provides some options.

-f, --filter filter: Filter output based on conditions provided


--format string: Pretty-print search using a Go template
--limit int: Max number of research results
--no-trunc: Do not truncate output
4) “docker rmi” is used to delete images.
docker rmi -f [image id]: delete a single image
docker rmi -f [image name: tag] [image name: tag]:delete multiple images
3
docker rmi -f $(docker images -qa) :delete all images

3. Container Command
The containers can be created only if there are images. Use “debian” to
test. Press “Ctrl+Alt+T” to open the command line terminal. Then enter “docker
pull debian” and press “Enter”.

3.1 “docker run” Image Running and Container Starting


Command Parameters: docker run [OPTIONS] IMAGE [COMMAND]
[ARG...]
Commonly Used Parameters:
--name="Name": assign a name to the container
-d:to run the container in the background and print its id
-i:to run the container in interactive mode and be used with -t
-t:to redistribute a terminal for the container and be used with -i
-P:to map random ports (uppercase)
-p : to map appointed ports (lowercase), generally, with four writing
methods
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort (commonly used)
Take the running “debian” image as an example. In the command line
terminal, enter “docker run -it debian /bin/bash” and press “Enter”.
“pi@raspberry” is the master system interface,and “root@9aeb4cc 795e6”
is in the interface inside the “docker” container.
4
Parameters:
-i: interactive mode
-t: terminal
debian : “debian” image
“/bin/bash” is the command shown after the image name, indicating the
hope that there is an interactive “Shell”. Therefore, “/bin/bash” is used.
To exit the terminal, enter “exit” in the terminal and press “Enter”.

3.2 “docker ps” Listing All Running Containers


Command Parameters: docker ps [OPTIONS]
Commonly Used Parameters:
-a:list all running and previously run containers
-l:display the latest created container
-n=?:display multiple (n) recently created containers
-q:Default mode, only display container IDs
Enter “docker ps -a” in the command line terminal and press “Enter”. The
running and run containers are displayed. “container id” is the container ID,
“image” is the image name used by the container, “created” is the creation time
of the container, and “status” is the current status of the container.

5
3.3 Exit Container
There are two commands for exiting containers:
1) Enter “exit” directly in the terminal and press “Enter”. This action will
stop the container from running and exit it.

2) Use “crtl+P+Q”. The container exits directly but keeps running. You can
enter the command “docker ps” in the terminal to view the running containers.

3.4 Multiple Terminals Enter Running Container


You can enter a running container from another terminal with two
commands:
“docker attach” is used to enter the terminal of the container start
command without starting a new process. Exiting from this container may
cause the container to stop running.
“docker exec” is used to open a new terminal in the container and start a
new process. Exiting from the container will not affect the container’s running
status. This method is generally preferred for entering a container.
3.4.1 docker attach
1) Press “Ctrl+Alt+T” to open the command line terminal and enter
“docker ps”. It displays the running containers.

2) Enter “docker attach 004c” in the terminal (The container ID can be


shortened as long as it is a unique identifier for the container.) to get into the
6
running container.

3) Enter “exit” in the terminal and press “Enter”. The container stops
running and exits.

4) Enter “docker ps” in the terminal. It displays the running container, and
you can see the earlier exited container is also closed.

3.4.2 docker exec


1) Press “Ctrl+Alt+T” to open the command line terminal and enter
“docker ps”. It displays the running containers.

2) Enter “docker exec -it efe9 /bin/bash” in the terminal (The container ID
can be shortened as long as it is a unique identifier for the container.) to enter
the running container.

3) Enter “exit” in the terminal and press “Enter”. The container will exit.

7
4) Enter “docker ps” in the terminal. It displays the running container, and
you can see the exited container is not closed.

3.5 Start and Stop Container


docker start [ container id or container name ]:to start a container
docker restart [ container id or container name ]:to restart a container
docker stop [ container id or container name ]:to stop a running container
docker kill [ container id or container name ]:to forcefully stop a running
container

3.6 Delete Container


docker rm [container id]:Delete specified container
docker rm -f $(docker ps -a -q):Delete all containers
docker ps -a -q|xargs docker rm:Delete all containers

4. Other Commonly-Used Other Commands

4.1 View Process Information Running in Container


1) Press “Ctrl+Alt+T” to open the command line terminal and enter
“docker ps”. It displays the running containers.

2) Enter “docker top efe9” in the terminal. It displays the process


information running in the container.

8
Corresponding Parameters:
UID: User ID.
PID: Process ID.
PPID: Parent Process ID.
C: CPU utilization of the process.
STIME: Start time of the process.
TTY: Terminal associated with the process.
TIME: Total CPU time used by the process.
CMD: Command line used to start the process.

4.2 View Metadata of Container or Image


1) Press “Ctrl+Alt+T” to open the command line terminal and enter
“docker ps”. It displays the running containers.

2) Enter “docker inspect efe9” in the terminal to output the JSON data
including detailed information about the container or image.

You might also like