DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Full Stack Kubernetes with Kong Ingress Controller
  • Running SpringBoot Application On OpenShift
  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular

Trending

  • Internal Developer Portals: Modern DevOps's Missing Piece
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • GDPR Compliance With .NET: Securing Data the Right Way
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Docker Basics, Commands, and Usage

Docker Basics, Commands, and Usage

In this article, learn more about Docker basics, commands, and usage.

By 
Krishna Prasad Kalakodimi user avatar
Krishna Prasad Kalakodimi
·
May. 21, 20 · Review
Likes (11)
Comment
Save
Tweet
Share
10.5K Views

Join the DZone community and get the full member experience.

Join For Free

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

  1. Docker Version: Show the Docker version information

$ docker version 

Plain Text
 




x
28


 
1
Client: Docker Engine - Community
2
 Version:           19.03.8
3
 API version:       1.40
4
 Go version:        go1.12.17
5
 Git commit:        afacb8b7f0
6
 Built:             Wed Mar 11 01:25:46 2020
7
 OS/Arch:           linux/amd64
8
 Experimental:      false
9
 
           
10
Server: Docker Engine - Community
11
 Engine:
12
  Version:          19.03.8
13
  API version:      1.40 (minimum version 1.12)
14
  Go version:       go1.12.17
15
  Git commit:       afacb8b7f0
16
  Built:            Wed Mar 11 01:24:19 2020
17
  OS/Arch:          linux/amd64
18
  Experimental:     false
19
 containerd:
20
  Version:          1.2.13
21
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
22
 runc:
23
  Version:          1.0.0-rc10
24
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
25
 docker-init:
26
  Version:          0.18.0
27
  GitCommit:        fec3683
28
 
           


2. Docker info: Display system-wide information

$ docker info 

Plain Text
 




xxxxxxxxxx
1
50


 
1
Client:
2
 Debug Mode: false
3
 
          
4
Server:
5
 Containers: 0
6
  Running: 0
7
  Paused: 0
8
  Stopped: 0
9
 Images: 1
10
 Server Version: 19.03.8
11
 Storage Driver: overlay2
12
  Backing Filesystem: <unknown>
13
  Supports d_type: true
14
  Native Overlay Diff: true
15
 Logging Driver: json-file
16
 Cgroup Driver: cgroupfs
17
 Plugins:
18
  Volume: local
19
  Network: bridge host ipvlan macvlan null overlay
20
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
21
 Swarm: inactive
22
 Runtimes: runc
23
 Default Runtime: runc
24
 Init Binary: docker-init
25
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
26
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
27
 init version: fec3683
28
 Security Options:
29
  apparmor
30
  seccomp
31
   Profile: default
32
 Kernel Version: 4.15.0-1065-aws
33
 Operating System: Ubuntu 18.04.4 LTS
34
 OSType: linux
35
 Architecture: x86_64
36
 CPUs: 1
37
 Total Memory: 983.7MiB
38
 Name: ip-172-31-43-97
39
 ID: LJ5P:V6HG:MADE:5755:EWP4:VELP:HWXH:WY5N:C4ZG:MUUW:JP7F:CWTN
40
 Docker Root Dir: /var/lib/docker
41
 Debug Mode: false
42
 Registry: https://index.docker.io/v1/
43
 Labels:
44
 Experimental: false
45
 Insecure Registries:
46
  127.0.0.0/8
47
 Live Restore Enabled: false
48
 
          
49
WARNING: No swap limit support
50
 
          


3. Docker login: Login to a Docker registry.

Syntax: $ docker login [options] [server] 

  • Log in to Docker hub registry by typing

$ docker login 

Plain Text
 




xxxxxxxxxx
1


 
1
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. 
2
Username: krishnaprasadkv 
3
Password: 
4
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json. Configure a credential helper to remove this warning. 
5
See https://docs.docker.com/engine/reference/commandline/login/#credentials-store
6
 
          
7
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 

Plain Text
 




xxxxxxxxxx
1


 
1
Using default tag: latest
2
latest: Pulling from library/ubuntu
3
d51af753c3d3: Pull complete
4
fc878cd0a91c: Pull complete
5
6154df8ff988: Pull complete
6
fee5db0ff82f: Pull complete
7
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
8
Status: Downloaded newer image for ubuntu:latest
9
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 

Plain Text
 




xxxxxxxxxx
1


 
1
The push refers to repository [docker.io/krishnaprasadkv/ubuntu]
2
8891751e0a17: Mounted from library/ubuntu
3
2a19bd70fcd4: Mounted from library/ubuntu
4
9e53fd489559: Mounted from library/ubuntu
5
7789f1a3d4e9: Mounted from library/ubuntu
6
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 

Plain Text
 




xxxxxxxxxx
1


 
1
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
2
krishnaprasadkv/ubuntu   latest              1d622ef86b13        10 days ago         73.9MB
3
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.

Dockerfile
 




xxxxxxxxxx
1


 
1
FROM ubuntu:latest
2
RUN mkdir test
3
WORKDIR test
4
RUN apt-get  update
5
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 .

Plain Text
 




xxxxxxxxxx
1
36


 
1
Sending build context to Docker daemon  19.97kB
2
Step 1/5 : FROM ubuntu:latest
3
 ---> 1d622ef86b13
4
Step 2/5 : RUN mkdir test
5
 ---> Using cache
6
 ---> fab356589341
7
Step 3/5 : WORKDIR test
8
 ---> Using cache
9
 ---> f24914f2dbaa
10
Step 4/5 : RUN apt-get  update
11
 ---> Running in bba46be46347
12
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
13
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
14
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [107 kB]
15
Get:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
16
Get:5 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [3717 B]
17
Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
18
Get:7 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [3190 B]
19
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [35.3 kB]
20
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
21
Get:10 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
22
Get:11 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
23
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [43.6 kB]
24
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [14.1 kB]
25
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [3190 B]
26
Get:15 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [2903 B]
27
Fetched 13.5 MB in 3s (4906 kB/s)
28
Reading package lists...
29
Removing intermediate container bba46be46347
30
 ---> 99fc47453d38
31
Step 5/5 : CMD echo hello world
32
 ---> Running in 834b91357a41
33
Removing intermediate container 834b91357a41
34
 ---> a4e02999c87e
35
Successfully built a4e02999c87e
36
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 

Plain Text
 




xxxxxxxxxx
1


 
1
Untagged: krishnaprasadkv/ubuntu:latest
2
Deleted: sha256:a4e02999c87ed555da02364f95e7098b652e60d7b5146c1b9fb33475ce169d1d
3
Deleted: sha256:99fc47453d383cb94909d018d0358ce9cb4228b87414a43598c7e586c2494425
4
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 the ubuntu: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 

Plain Text
 




xxxxxxxxxx
1


 
1
CONTAINER ID    IMAGE         COMMAND         CREATED         STATUS         PORTS         NAMES
2
 
          
3
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 
Plain Text
 




xxxxxxxxxx
1


 
1
REPOSITORY                  TAG                    IMAGE ID            CREATED             SIZE
2
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 

Plain Text
 




xxxxxxxxxx
1


 
1
root@5c7b73f91043:/# ls
2
 
          
3
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 createcommand 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 

Plain Text
 




xxxxxxxxxx
1


 
1
PID                 USER                TIME                COMMAND
2
2421                root                0:00                /bin/bash
3
2535                root                0:00                bash
4
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 

Plain Text
x
 
1
A /test
2
A /test123
3
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.

Docker (software) Command (computing) Plain text Syntax (programming languages)

Opinions expressed by DZone contributors are their own.

Related

  • Full Stack Kubernetes with Kong Ingress Controller
  • Running SpringBoot Application On OpenShift
  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!