Using Docker on Ubuntu 20.04

Basic commands and techniques for working with Docker on Ubuntu 20.04.

Docker is software for automating the deployment and management of applications using container-level virtualization. It lets you package an application with all its dependencies into an isolated container that can be moved to any Linux system with cgroups support.

Docker

For installation instructions, see Installing Docker on Ubuntu 20.04.

Basic commands

The docker command syntax:

docker [option] [command] [arguments]

List all available subcommands:

docker

Get help for a specific subcommand:

docker docker-subcommand --help

Display general Docker information:

docker info

Working with images

By default, Docker pulls images from Docker Hub. To verify access to the registry, run the test container:

docker run hello-world

Expected output:

Hello from Docker!
This message shows that your installation appears to be working correctly.

Search for an image:

docker search debian

The OK value in the OFFICIAL column means the image is maintained by the project's developer.

Pull an image:

docker pull debian

Run an image:

docker run <name>

List downloaded images:

docker images

Example output:

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

Running a container

To start an interactive container with shell access, use the -it flags:

docker run -it ubuntu

The prompt will change to the container's shell:

root@e83d97af5a32:/#

From here you can run any commands inside the container. For example, install Node.js:

apt update
apt install nodejs

Verify the installation:

node -v
v10.19.0

All changes apply only to the current container. Type exit to leave.

Managing containers

List active containers:

docker ps

List all containers including stopped ones:

docker ps -a

Show the most recently created container:

docker ps -l

Start a stopped container:

docker start 2d3ef7e60d67

Stop a running container:

docker stop crazy_davinci

Remove a container:

docker rm crazy_davinci

Use docker ps -a to find the container ID or name.

Saving changes to a new image

After modifying a container, you can save the changes as a new image using docker commit:

docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
  • -m — commit message describing the changes.
  • -a — author name.
  • container_id — the container's ID.
  • repository — your Docker Hub username.

For example:

docker commit -m "added Node.js" -a "test" 2d3ef7e60d67 test/ubuntu-nodejs

The new image will appear in your local image list:

docker images

Example output:

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

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us