Basic Docker Commands You Need to Know
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 Copy link
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 Copy link
- 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 nginxLaunching 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 mycontainerStarting 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 mycontainerStopping several:
docker stop first_container second_container third_containerStop 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 mycontainerRestarting several:
docker restart first_container second_container third_containerRestart 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 mycontainerRemoving multiple:
docker rm first_container second_container third_containerForced 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 psDisplaying a list of all (including stopped ones):
docker ps -aDisplaying the amount of occupied disk space:
docker ps -sCommands for managing images Copy link
- 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 ubuntuHere, 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.0Uploading 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.0Removing 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 imagesDisplaying images for a specific repository:
docker images myrepository/myimageCommands for managing data stores Copy link
- 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 myvolumeCreation with driver specification:
docker volume create --driver local myvolumeCreating 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 myimageVolume 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 myvolumeRemoving 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 myvolume2Docker Compose Commands Copy link
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 uplaunches the application with all containers, information about which is indocker-compose.yml. If no file is specified, the default is the file in the current directory;docker-compose downstops and deletes all containers, as well as volumes associated with them;docker-compose startstarts stopped containers;docker-compose stopstops running containers without deleting them;docker-compose restartrestarts containers;docker-compose buildallows you to update images or recreate them if they have been changed;docker-compose logsdisplays status logs;docker-compose psdisplays the current state of containers;docker-compose pulldownloads the latest versions of images for the services described in thedocker-compose.ymlfile.
Conclusion Copy link
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!
By the way, with Hostman, you can run your workloads on an efficient Amsterdam VPS that support low latency for EU-based users. Check this out, we have plenty of budget VPS hosting options for your projects.