Docker is a software for automating the deployment and management of applications in an operating system-level virtualization environment. It allows you to package an application with all its environment and dependencies into a container that can be ported to any Linux system with kernel cgroups support and provides a container management environment
You can learn how to install Docker in this manual
Using the Docker command
The docker command allows you to use various options, commands with arguments. The syntax is as follows:
``.shell
docker [option] [command] [arguments]
To see all available subcommands, type:
``.shell
docker
The complete list will look like this:
root@kvmde68-19464:~# docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, -context string Name of the context to use to connect to the daemon
(overrides DOCKER_HOST env var and default context set with
"docker context use")
-D, --debug Enable debug mode
-H, -host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
configage Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach local standard input, output, and error streams to a running container
build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull Pull an image or a repository from a registry
push Push Push an image or a repository to a registry
rename Rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run Run a command in a new container
save Save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To see the options for using a particular command, type:
``.shell
docker docker-subcommand --help
To view all the information about Docker, you can use the command:
``.shell
docker info
root@kvmde68-19464:~# docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.11
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.4.0-37-generic
Operating System: Ubuntu 20.04 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 981.3MiB
Name: kvmde68-19464.fornex.org
ID: ZN2U:RFE6:H63F:QXH2:EZPM:L236:H5AX:KGLJ:ZFT3:SAAG:LSSS:TF4H
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Working with Docker images
By default, Docker gets images from the Docker Hub, which is a registry of images maintained by Docker
To check if you can access and download images from Docker Hub, type the following command:
``.shell
docker run hello-world
The correct result of this command, which means that Docker is working correctly, is shown below:
``.shell
root@kvmde68-19464:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Initially, Docker could not find the hello-world image locally, so it downloaded the image from the Docker Hub, which is the default repository. After downloading the image, Docker created a container from the image and ran the application in the container, displaying a message.
The images available in Docker Hub can be searched using the docker command and the search subcommand.
``.shell
docker search debian
The script looks through the Docker Hub and returns a list of all images whose names match the given search
~~~.shell
root@kvmde68-19464:~# docker search debian
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys... 11031 [OK]
debian Debian is a Linux distribution that's compos... 3521 [OK]
arm32v7/debian Debian is a Linux distribution that's compos... 66
itscaro/debian-ssh debian:jessie 28 [OK]
arm64v8/debian Debian is a Linux distribution that's compos... 23
samueldebruyn/debian-git a minimal docker container with debian and g... 22 [OK]
multiarch/debian-debootstrap multiarch ports of debian-debootstrap 12
i386/debian Debian is a Linux distribution that's compos... 10
eboraas/debian Debian base images, for all currently-availa... 8 [OK]
vergissberlin/debian-development Docker debian image to use for development, ... 6 [OK]
ppc64le/debian Debian is a Linux distribution that's compos... 4
amd64/debian Debian is a Linux distribution that's compos... 4
smartentry/debian debian with smartentry 4 [OK]
vicamo/debian Debian docker images for all versions/archit... 3
s390x/debian Debian is a Linux distribution that's compos... 2
vpgrp/debian Docker images of Debian. 2
arm32v5/debian Debian is a Linux distribution that's compos... 2
spritsail/debian-builder A Docker image based on debian:slim ideal fo... 1 [OK]
holgerimbery/debian debian multarch docker base image 1
dockershelf/debian Repository for docker images of Debian. Test... 1 [OK]
fleshgrinder/debian Debian base images for production and multis... 0 [OK]
casept/debian-amd64 A debian image built from scratch. Mostly fo... 0
jdub/debian-sources-resource Concourse CI resource to check for updated D... 0 [OK]
1and1internet/debian-9-nginx-php-7.2-wordpress-4 debian-9-nginx-php-7.2-wordpress-4 0 [OK]
konstruktoid/debian Debian base image 0 [OK]
~~~
In the **OFFICIAL** column, the **OK** line shows that the image was built and is supported by the company who built it. When the desired image is selected, you can download it to your computer using the **pull** subcommand.
For example, downloading the official ubuntu image to your computer:
``.shell
docker pull debian
You will see a similar result:
root@kvmde68-19464:~# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
e9afc4f90ab0: Pull complete
Digest: sha256:46d659005ca1151087efa997f1039ae45a7bf7a2cbbe2d17d3dcbda632a3ee9a
Status: Downloaded newer image for debian:latest
docker.io/library/debian:latest
After downloading the image you can start the container with the downloaded image with the run subcommand
To view the downloaded images, type:
``.shell
docker images
You will see a result like this:
``.shell
root@kvmde68-19464:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 74435f89ab78 6 days ago 73.9MB
debian latest 1b686a95ddbf 2 weeks ago 114MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
Starting the Docker container
The hello-world container is an example of a container that starts and finishes after a test message is output. Containers are similar to virtual machines, but are less demanding on resources.
The combination of the parameters -i and -t gives interactive access to the container's command processor:
``.shell
docker run -it ubuntu
The command line will show that we are working in the container:
``.shell
root@kvmde68-19464:~# docker run -it ubuntu
root@e83d97af5a32:/#
Next you can run commands inside the container
``.shell
apt update
Install Node.js:
``.shell
apt install nodejs
This command installs Node.js in a container from the official Ubuntu repository
Check that Node.js is installed:
``.shell
node -v
``.shell
root@e83d97af5a32:/# node -v
v10.19.0
Changes that are executed inside a container only apply to that container.
To exit the container, type exit.
Manage Docker Containers
Once you start using Docker, you'll have many active and inactive containers on your machine
To view active containers, type the command:
``.shell
docker ps
``.shell
root@kvmde68-19464:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
To see the active and inactive containers, run docker ps with option -a:
``.shell
docker ps -a
``.shell
root@kvmde68-19464:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
``.shell
root@kvmde68-19464:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d3ef7e60d67 debian "bash" 23 seconds ago Exited (0) 14 seconds ago crazy_davinci
e83d97af5a32 ubuntu "/bin/bash" 7 minutes ago Exited (0) 3 minutes ago amazing_wilson
56aac1efc3f6 hello-world "/hello" 20 minutes ago Exited (0) 20 minutes ago vigorous_allen
To see the last of the created containers, specify parameter **-l**:
``.shell
docker ps -l
``.shell
root@kvmde68-19464:~# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d3ef7e60d67 debian "bash" 53 seconds ago Exited (0) 43 seconds ago crazy_davinci
To start a stopped container, use the command **docker start** and give the container id or its name
``.shell
docker start 2d3ef7e60d67
You can now use docker ps to view its status:
``.shell
root@kvmde68-19464:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d3ef7e60d67 debian "bash" 2 minutes ago Up 31 seconds crazy_davinci
To stop a running container, use the command **docker stop** and specify the container id or its name
``.shell
docker stop crazy_davinci
If you don't need the container anymore, delete it with the command docker rm specifying the container ID or name. To find the container ID or name, use the command docker ps -a. The container can then be deleted.
``.shell
docker rm crazy_davinci
**Saving changes in the container to a Docker image**
When you run the container from a Docker image, you can create, modify, and delete files, just like in a virtual machine.
After installing Node.js in an Ubuntu container, you will have a container running from the image, but it will be different from the image used to create it. However, you may need such a Node.js container as the basis for future images.
Then confirm the changes in the new Docker image with the following command
``.shell
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
Parameter -m allows you to specify a confirmation message, parameter -a allows you to specify an author. The container_id identifier is the identifier that was used before. If you have not created additional repositories in Docker Hub, the repository name is usually your user name in Docker Hub.
For example, for the user test and the container ID 2d3ef7e60d67, the command would look like this:
``.shell
docker commit -m "added Node.js" -a "test" 2d3ef7e60d67 test/ubuntu-nodejs
After confirming (commit) the image, the new image is saved locally on your computer
If you browse the list of Docker images, you will find both the new image and the original image on which it was based:
``.shell
docker images
You will see a similar result:
``.shell
root@kvmde68-19464:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu-nodejs latest 79fbfd36b4b3 12 seconds ago 114MB
ubuntu latest 74435f89ab78 6 days ago 73.9MB
debian latest 1b686a95ddbf 2 weeks ago 114MB
hello-world latest bf756fb1ae65 5 months ago 13.3kB
```
In this example, ubuntu-nodejs is a new image created from an existing ubuntu image from the Docker Hub.