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 looks like this:
``.shell
docker [option] [command] [arguments]
To see all available subcommands, type:
``.shell
docker
The complete list will look like this:
``.shell
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
To see the options to use a certain command, type
``.shell
docker docker-subcommand --help
To view all the information about Docker, you can use the command:
``.shell
docker info
~~~.shell
root@kvmde67-19464:~# docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.8
Storage Driver: overlay2
Backing Filesystem <unknown>
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:
seccomp
Profile: default
Kernel Version: 4.9.0-11-amd64
Operating System: Debian GNU/Linux 9 (stretch)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 996.5MiB
Name: kvmde67-19464.fornex.org
ID: HSOZ:FUYF:W3OM:FR4R:LDBZ:LPC2:GPF2:VD54:RRSU:PC34:XFRM:URDD
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](https://hub.docker.com/), 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@kvmde67-19464:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
Initially, Docker couldn't 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@kvmde67-19464:~# docker search debian
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys... 10866 [OK]
debian Debian is a Linux distribution that's compos... 3473 [OK]
arm32v7/debian Debian is a Linux distribution that's compos... 64
itcaro/debian-ssh debian:jessie 28 [OK]
samueldebruyn/debian-git a minimal docker container with debian and g... 22 [OK]
arm64v8/debian Debian is a Linux distribution that's compos... 22
multiarch/debian-debootstrap multiarch ports of debian-debootstrap 11
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]
smartentry/debian debian with smartentry 4 [OK]
amd64/debian Debian is a Linux distribution that's compos ... 4
ppc64le/debian Debian is a Linux distribution that's compos... 4
vicamo/debian Debian docker images for all versions/archit... 3
arm32v5/debian Debian is a Linux distribution that's compos... 2
s390x/debian Debian is a Linux distribution that's compos... 2
vpgrp/debian Docker images of Debian. 2
spritsail/debian-builder A Docker image based on debian:slim ideal fo... 1 [OK]
dockershelf/debian Repository for docker images of Debian. Test... 1 [OK]
holgerimbery/debian debian multarch docker base image 1
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 line **OK** shows that the image was built and is supported by the company who developed it. Once the right image has been selected, you can download it to your computer using the **pull** subcommand.
For example, to download the official ubuntu image to your computer:
``.shell
docker pull debian
You will see a similar result:
``.shell
root@kvmde67-19464:~# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
90fe46dd8199: Pull complete
Digest: sha256:2857989334428416b1ef369d6e029e912a7fe3ee7e57adc20b494cc940198258
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 using the **run** subcommand
To view the downloaded images, type:
``.shell
docker images
You will see a result like this:
``.shell
root@kvmde67-19464:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 1d622ef86b13 13 days ago 73.9MB
debian latest 3de0e2c97e5c 2 weeks ago 114MB
hello-world latest bf756fb1ae65 4 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 debian
The command line will show that we are working in the container:
``.shell
root@e4a123443895:/#
Next, we 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@e4a123443895:/# 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@kvmde67-19464:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
To see the active and inactive containers, run docker ps with parameter **-a**:
``.shell
docker ps -a
``.shell
root@kvmde67-19464:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a123443895 debian "bash" 3 minutes ago Exited (0) About a minute ago
ac717eb882db hello-world "/hello" About an hour ago Exited (0) About an hour ago bold_lovelace
To see the last of the created containers, specify parameter **-l**:
``.shell
docker ps -l
``.shell
root@kvmde67-19464:~# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a123443895 debian "bash" 5 minutes ago Exited (0) 2 minutes ago
To start a stopped container, use the command **docker start** and give the container id or its name
``.shell
docker start e4a123443895
You can now use docker ps to see its status:
``.shell
root@kvmde67-19464:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4a123443895 debian "bash" 6 minutes ago Up 12 seconds tender_hugle
To stop a running container, use the command **docker stop** and specify the container id or its name
``.shell
docker stop tender_hugle
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 tender_hugle
**Saving changes to the container in 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 user test and container ID bb5be2ace9f4, the command would look like this:
``.shell
docker commit -m "added Node.js" -a "test" e4a123443895 test/debian-nodejs
After the image has been committed, 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 it was based on:
``.shell
docker images
You will see a similar result:
``.shell
root@kvmde67-19464:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/debian-nodejs latest ef3923c87c69 30 seconds ago 203MB
root/ubuntu-nodejs latest 412fa0b7f003 2 minutes ago 203MB
ubuntu latest 1d622ef86b13 13 days ago 73.9MB
debian latest 3de0e2c97e5c 2 weeks ago 114MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
```
In this example debian-nodejs is a new image based on an existing debian image from the Docker Hub.