Log In

Basic Docker Commands You Need to Know

Basic Docker Commands You Need to Know
26.03.2024
Reading time: 6 min
Hostman Team
Technical writer

Docker is a popular platform that allows developers to easily manage applications in isolated environments. The platform provides powerful containerization tools, making it indispensable in modern software development. However, to use it effectively, you need to know the basic commands to manage containers and their images. In this article, we'll look at the basic Docker commands that will help you get started with containers.

Basic Docker Commands

To make it more convenient, we made several lists of Docker commands, dividing them into thematic groups. You'll learn what a specific Docker command does, followed by code examples.

Commands for managing containers

  • docker run

Creates and launches a container from an image. Accepts various parameters and arguments that define container settings.

Usage examples:

Starting a container in the background, forwarding a port from the host to the container, and passing an environment variable:

docker run -d -p 8080:80 -e ENV_VAR=value nginx

Launching a container with a resource limit. In this example, the amount of RAM:

docker run --memory=2g myimage
  • docker start

Starts a stopped container.

Usage examples:

Starting one stopped container:

docker start mycontainer

Starting several:

docker start first_container second_container third_container
  • docker stop

Stops a running container. Allows you to terminate the container and disable it.

Usage examples:

Stopping one running container:

docker stop mycontainer

Stopping several:

docker stop first_container second_container third_container

Stop by time (in this example, after 30 seconds):

docker stop -t 30 mycontainer
  • docker restart

Restarts the container. Allows you to stop the container and then start it again.

Usage examples:

Restarting one container:

docker restart mycontainer

Restarting several:

docker restart first_container second_container third_container

Restart after a certain time:

docker restart -t 30 mycontainer
  • docker rm

Removes one or more containers that are already stopped. Allows you to clear the system of unused containers.

Usage examples:

Removing one container:

docker rm mycontainer

Removing multiple:

docker rm first_container second_container third_container

Forced delete (allows you to delete a container that is currently running):

docker rm -f mycontainer
  • docker ps

Displays a list of active containers on the system. Allows you to obtain various information about running containers, including their IDs, names, status, ports, resource usage.

Usage examples:

Displaying a list of only running containers:

docker ps

Displaying a list of all (including stopped ones):

docker ps -a

Displaying the amount of occupied disk space:

docker ps -s

Commands for managing images

  • docker pull

Downloads images from a remote Docker Hub repository. Allows you to access ready-made images.

Usage examples:

Download the latest version of Ubuntu:

docker pull ubuntu

Here, ubuntu is the image name, but the version is not specified, so the latest one will be loaded.

Download a specific version:

docker pull ubuntu:20.04
  • docker build

Builds a custom Docker image based on a Dockerfile. A Dockerfile is a text file with instructions for building an image, including a base image, installing dependencies, and copying files.

Usage examples:

To build an image from the current directory using the Dockerfile in that directory, use the command:

docker build -t myimage:latest .

Build an image from a remote Git repository that contains a Dockerfile:

docker build -t myimage:latest https://github.com/username/repo.git#branch
  • docker push

Uploads a custom image to Docker Hub or other image repositories. Allows you to share the created image with other Docker users.

Usage examples:

Upload to default repository:

docker push username/myimage:1.0

Uploading to another repository (in this example, the ECR repository):

docker push myecr/myimage:latest
  • docker rmi

Removes images from localhost. Allows you to free up space by deleting unused or outdated images.

Usage examples:

Removing one image:

docker rmi myimage:1.0

Removing multiple:

docker rmi myimage:1.0 myotherimage:latest
  • docker images

Lists the images on your localhost. It allows you to view information about available images: identifiers, sizes, tags, etc.

Usage examples:

Output of all images:

docker images

Displaying images for a specific repository:

docker images myrepository/myimage

Commands for managing data stores

  • docker volume create

Creates a new Docker volume. Volumes in Docker are persistent data stores that are used by containers to store and share information between them. Volumes allow data to persist even after containers are deleted or restarted.

Usage examples:

Creating a new volume:

docker volume create myvolume

Creation with driver specification:

docker volume create --driver local myvolume

Creating a volume with labels added:

docker volume create --label mylabel=myvalue myvolume
  • docker run -v

Used to bind the volume to the container at startup. Allows the container to access persistent data storage provided by the volume.

Usage examples:

Bind an existing volume:

docker run -v myvolume:/data myimage

Volume binding indicating additional options (for example, read only):

docker run -v myvolume:/data:ro myimage
  • docker volume rm

Removes a volume from localhost. When deleted, all data associated with this volume will be lost, so be careful.

Usage examples:

Removing a single volume:

docker volume rm myvolume

Removing multiple:

docker volume rm first_volume second_volume third_volume
  • docker volume ls

Lists all available volumes on the local host. Allows you to view existing volumes and obtain information about each volume, including their names, IDs, and additional information, if any.

Example output:

DRIVER VOLUME NAME
local myvolume1
local myvolume2

Docker Compose Commands

Docker Compose runs on top of Docker and allows you to manage multiple containers within a single project. Let's look at the basic commands (the operating principles of most of them are already familiar to you):

  • docker-compose up launches the application with all containers, information about which is in docker-compose.yml. If no file is specified, the default is the file in the current directory;
  • docker-compose down stops and deletes all containers, as well as volumes associated with them;
  • docker-compose start starts stopped containers;
  • docker-compose stop stops running containers without deleting them;
  • docker-compose restart restarts containers;
  • docker-compose build allows you to update images or recreate them if they have been changed;
  • docker-compose logs displays status logs;
  • docker-compose ps displays the current state of containers;
  • docker-compose pull downloads the latest versions of images for the services described in the docker-compose.yml file.

Conclusion

We looked at the basic Docker commands that allow you to manage containers and images. As you delve deeper into the platform, you'll see that Docker provides powerful capabilities for developing, testing, and deploying applications while ensuring efficient resource utilization and application isolation. We wish you success!

Share