Docker Basics, Commands, and Usage
In this article, learn more about Docker basics, commands, and usage.
Join the DZone community and get the full member experience.
Join For FreeWhat Is Docker?
Docker is the world’s leading software containerization platform. Docker is a tool designed to make it easier to deploy and run applications by using containers. Containers allow a developer to package upon applications with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Basic Concepts:
Containers: A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application like code, runtime, system tools, system libraries, and settings.
Docker Image: are the basis of containers. An image is an ordered collection of root filesystem changes and the corresponding execution parameters for use within a container runtime.
- An image typically consists of a union of layered filesystem stacked on top of each other.
- An image doesn't have state and te never changes.
Docker Hub: is a centralized resource for working with Docker and its components it provides
- Docker image hosting
- User authentication
- Automated images build and workflow tools, such as build triggers and webhooks.
- Integration with git hub and bit bucket.
Docker Registry: Registry is a hosted service containing repositories of images which responds to the registry API.
- Default registry can be accessed using browser at Docker hub or using the Docker search command
Repository: A repository is a set of Docker images the repository can be shared by pushing it to a registry server.
- The different images in the repository can be labeled using tags.
Tags: A tag is labeled applied to a Docker image in a repository. Tags are how various images in a repository are distinguished from each other.
Docker Machine: A machine is a Docker tool that makes it really easy to create a Docker host on your computer, on the cloud provides and inside your data center.
- It creates servers installs Docker on them.
- Then configure the Docker client to talk to them
Get Docker: You can download and install Docker on multiple platforms
Basic Commands:
- Docker Version: Show the Docker version information
$ docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.40
Go version: go1.12.17
Git commit: afacb8b7f0
Built: Wed Mar 11 01:25:46 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.8
API version: 1.40 (minimum version 1.12)
Go version: go1.12.17
Git commit: afacb8b7f0
Built: Wed Mar 11 01:24:19 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
2. Docker info: Display system-wide information
$ docker info
xxxxxxxxxx
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 1
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:
apparmor
seccomp
Profile: default
Kernel Version: 4.15.0-1065-aws
Operating System: Ubuntu 18.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 983.7MiB
Name: ip-172-31-43-97
ID: LJ5P:V6HG:MADE:5755:EWP4:VELP:HWXH:WY5N:C4ZG:MUUW:JP7F:CWTN
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
WARNING: No swap limit support
3. Docker login: Login to a Docker registry.
Syntax: $ docker login [options] [server]
- Log in to Docker hub registry by typing
$ docker login
xxxxxxxxxx
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: krishnaprasadkv
Password:
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json. Configure a credential helper to remove this warning.
See https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
Learn more about Options and Examples.
4. Docker logout: Log out from a Docker registry
$ docker logout
Removing login credentials for https://index.docker.io/v1/
5: Docker Search: Search the Docker Hub for images.
Syntax: $ docker search [options] Term
- Search image by ID: Below example displays images with a name containing ‘ubuntu’.
$
docker search ubuntu
6: Docker Pull: Pull an image or a repository from a registry
Syntax: $ docker pull [options] Name[:Tag]
- Pull an image from Docker Hub: To download a particular image, or set of images (i.e., a repository), use
docker pull
. If no tag is provided, Docker Engine uses the:latest
tag as a default. This command pulls the ubuntu:latest image:
$ docker pull ubuntu
xxxxxxxxxx
Using default tag: latest
latest: Pulling from library/ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
6. Docker Push: Push an image or a repository to a registry
Syntax: $
docker push [OPTIONS] NAME[:TAG]
- Push an image to Docker Hub: To share your images to the Docker Hub registry or to a self-hosted one.
$ docker push krishnaprasadkv/ubuntu
xxxxxxxxxx
The push refers to repository [docker.io/krishnaprasadkv/ubuntu]
8891751e0a17: Mounted from library/ubuntu
2a19bd70fcd4: Mounted from library/ubuntu
9e53fd489559: Mounted from library/ubuntu
7789f1a3d4e9: Mounted from library/ubuntu
latest: digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8 size: 1152
7. Docker images: List images
Syntax: $
docker images [OPTIONS] [REPOSITORY[:TAG]]
- List the local server Docker images.
$ docker images
xxxxxxxxxx
REPOSITORY TAG IMAGE ID CREATED SIZE
krishnaprasadkv/ubuntu latest 1d622ef86b13 10 days ago 73.9MB
ubuntu latest 1d622ef86b13 10 days ago 73.9MB
Learn More About: Options and Examples
8. Docker tag: Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE.
Syntax: $ docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
- Tag an image referenced by Name and Tag: To tag a local image with the name “ubuntu” and tag “latest” into the “krishnaprasadkv” repository with “latest”:
$ docker tag ubuntu:latest krishnaprasadkv/ubuntu:latest
Learn More About: Examples
9. Dockerfile: A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
xxxxxxxxxx
FROM ubuntu:latest
RUN mkdir test
WORKDIR test
RUN apt-get update
CMD echo hello world
Learn More About: Best practices for writing Dockerfile
10. Docker build: Build an image from a Dockerfile.
Syntax: $ docker build [OPTIONS] PATH | URL | -
- Build and tag an image: This example specifies build a Docker image from the Dockerfile and tag the resulting image. The repository name will be krishnaprasadkv/ubuntu and the tag will be the latest.
$ docker build -t krishnaprasadkv/ubuntu .
xxxxxxxxxx
Sending build context to Docker daemon 19.97kB
Step 1/5 : FROM ubuntu:latest
---> 1d622ef86b13
Step 2/5 : RUN mkdir test
---> Using cache
---> fab356589341
Step 3/5 : WORKDIR test
---> Using cache
---> f24914f2dbaa
Step 4/5 : RUN apt-get update
---> Running in bba46be46347
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [107 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [3717 B]
Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [3190 B]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [35.3 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [43.6 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [14.1 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [3190 B]
Get:15 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [2903 B]
Fetched 13.5 MB in 3s (4906 kB/s)
Reading package lists...
Removing intermediate container bba46be46347
---> 99fc47453d38
Step 5/5 : CMD echo hello world
---> Running in 834b91357a41
Removing intermediate container 834b91357a41
---> a4e02999c87e
Successfully built a4e02999c87e
Successfully tagged krishnaprasadkv/ubuntu:latest
Learn More About: Options and Examples
11. Docker rmi: Remove one or more images
Syntax: $ docker rmi [OPTIONS] IMAGE [IMAGE...]
- Remove a Docker image by id: Below example shows to remove a Docker image by id.
$ docker rmi a4e02999c87e
xxxxxxxxxx
Untagged: krishnaprasadkv/ubuntu:latest
Deleted: sha256:a4e02999c87ed555da02364f95e7098b652e60d7b5146c1b9fb33475ce169d1d
Deleted: sha256:99fc47453d383cb94909d018d0358ce9cb4228b87414a43598c7e586c2494425
Deleted: sha256:53beaacd1c580c0c692520393932c4f3c95da1d0389d509649d4095607bbd957
Learn More About: Options and Examples
12. Docker run: The docker run
the command first creates a writeable container layer over the specified image and then starts it using the specified command
Syntax: $ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- Assign a name and allocate pseudo-TTY(--name, -it): This example runs a container named
ubuntu-container
using theubuntu:latest
image. The -it means interactive terminal and --name specifies the name of the container. instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container.
$ docker run --name ubuntu-container -it ubuntu
Learn More About: Options and Examples
13. Docker ps: This command displays the list of containers.
Syntax: $docker ps [OPTIONS]
- Show both running and stopped containers: The
docker ps
command only shows running containers by default. To see all containers, use the -a
$ docker ps -a
xxxxxxxxxx
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c7b73f91043 ubuntu "/bin/bash" 9 seconds ago Up 6 seconds ubuntu-container
Learn More About: Options and Examples
14. Docker attach: Use docker attach
to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
Syntax: $ docker attach [options] container
- Attach to a running container: This example attaches to a running container. The running container name is
ubuntu-container
. To list the running container using$ docker ps
$ docker attach ubuntu-conatiner
Learn More About: Options and Examples
15. Docker exec: The docker exec
command runs a new command in a running container. The command started using docker exec
only runs while the container’s primary process (PID 1) is running, and it is not restarted if the container is restarted.
Syntax: $ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
- Run docker exec on running container: Below example will execute an interactive bash shell on the container i.e
ubuntu-container
.
$ docker exec -it ubuntu_container bash
Learn More About: Options and Examples
16. Docker stop: Stop one or more running containers. The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.
Syntax: $ docker stop [OPTIONS] CONTAINER [CONTAINER...]
- Stop a running container by typing:
$ docker stop ubuntu-container
17. Docker start: Start one or more stopped containers
Syntax: $ docker start [OPTIONS] CONTAINER [CONTAINER...]
- Start a stopped container by typing:
$ docker start ubuntu-container
Learn More About: Options
18. Docker restart: Restart one or more containers
Syntax: $ docker restart [OPTIONS] CONTAINER [CONTAINER...]
- Restart a Docker container by typing:
$ docker restart ubuntu-container
19. Docker rename: The docker rename
command renames a container
Syntax: $ docker rename CONTAINER NEW_NAME
- Rename a Docker container by typing:
$ docker rename ubuntu-container ubuntu
20. Docker commit: Create a new image from a container’s changes.
Syntax: $ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- Commit a container:
$ docker commit 5c7b73f91043 krishnaprasadkv/ubuntu:latest
sha256:4afe2a9d8a9abad8e7c3c9b9498d01e6af803d4200113d7699c0139be2d97f01
- To see the Docker images
$ docker images
xxxxxxxxxx
REPOSITORY TAG IMAGE ID CREATED SIZE
krishnaprasadkv/ubunt latest 4afe2a9d8a9a 43 seconds ago 64.2MB
Learn More About: Options and Examples
21. Docker cp: The docker cp
utility copies the contents of SRC_PATH to the DEST_PATH. You can copy from the container’s file system to the local machine or the reverse, from the local filesystem to the container.
Syntax: $ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
$ docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
- Copy the local file test123 to the container
$ docker cp ./test123 ubuntu-container:/
$docker exec -it ubuntu-container bash
xxxxxxxxxx
root@5c7b73f91043:/# ls
bash bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test test123 tmp usr var
Learn More About: Options
22. Docker inspect: Docker inspect provides detailed information on constructs controlled by Docker.
Syntax: $ docker inspect [OPTIONS] NAME|ID [NAME|ID...]
- Get a container name: The
docker inspect
command will render results in a JSON array.
$ docker inspect --format='{{.Name}}'ubuntu-conatiner
Learn More About: Options and Examples
23. Docker create: The docker create
command creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT. This is similardocker run -d
except the container is never started. You can then use the docker start <container_id>
command to start the container at any point.
Syntax: $ docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
create and start a container: Below example will create a container using docker create
command and to start the created container docker start
command.
$ docker create -it --name ubuntu-create ubuntu bash
$ docker start -ai ubuntu-create
Learn More About: Options and Examples
24. Docker rm: Remove one or more stopped containers.
Syntax: $ docker rm [OPTIONS] CONTAINER [CONTAINER...]
- Stop and remove a container: Below example will show how to stop (
docker stop
)and remove (docker rm
) a container.
$ docker stop ubuntu-create
$ docker rm ubuntu-create
Learn More About: Options and Examples
25. Docker kill: The docker kill
subcommand kills one or more containers. The main process inside the container is sent SIGKILL signal (default), or the signal that is specified with the --signal
option. You can kill a container using the container’s ID, ID-prefix, or name.
Syntax: $ docker kill [OPTIONS] CONTAINER [CONTAINER...]
- Send a kill signal to a container: The following example sends the default KILL signal to the container named ubuntu-container:
$ docker kill ubuntu-container
Learn More About: Options and Examples
26. Docker logs: Fetch the logs from a container
Syntax: $ docker logs [OPTIONS] CONTAINER
- Retrieve logs from a container: The following example retrieves logs present at the time of execution
$ docker logs ubuntu-conatiner
Learn More About: Options and Examples
27. Docker top: Display the running processes of a container
$ docker top CONTAINER [ps OPTIONS]
- List the running process of a container
$ docker top ubuntu-container
xxxxxxxxxx
PID USER TIME COMMAND
2421 root 0:00 /bin/bash
2535 root 0:00 bash
2661 root 0:00 bash
28. Docker stats: The docker stats
the command returns a live data stream for running containers. To limit data to one or more specific containers, specify a list of container names or ids separated by a space.
Syntax: $ docker stats [OPTIONS] [CONTAINER...]
- Get the Container stats:
$
docker stats ubuntu-container
Learn More About: Options and Examples
29. Docker save: Save one or more images to a tar archive (streamed to STDOUT by default)
Syntax: $ docker save [OPTIONS] IMAGE [IMAGE...]
$ docker save busybox > busybox.tar
$ ls -sh busybox.tar
2.7M busybox.tar
Learn More About: Examples
30. Docker diff: List the changed files and directories in a container᾿s filesystem since the container was created. Three different types of change are tracked:
Symbol
|
Description
|
---|---|
A
|
A file or directory was added
|
D
|
A file or directory was deleted
|
C
|
A file or directory was changed
|
Syntax: $ docker diff CONTAINER
- Inspect the changes to an ubuntu-container.
$ docker diff ubuntu-container
A /test
A /test123
A /bash
Conclusion
In this article, you learned in detail Docker basics, basic Docker commands, and usage.
If you want to learn more about Docker see Docker Base commands, Dockerfile, compose file v3 reference.
Opinions expressed by DZone contributors are their own.
Comments