Docker commands cheat sheet
Installation and information
-
docker versionShow the version of the command-line tool (Client) and the version of the Docker service (Server). When the service is not running, an error message appears instead of the service details.
-
docker infoShow general information about the installation, including the number of containers and images, the storage driver, and the Docker data directory.
-
docker run --helpShow all the options of the docker run command. You can add --help to any Docker command.
-
sudo systemctl status dockerShow whether the Docker service is running on a Linux server, and its latest log lines.
-
sudo systemctl enable --now dockerSet the Docker service to start automatically when the server boots, and start it right away.
-
sudo usermod -aG docker $USERLet the current user run Docker commands without sudo. The change takes effect the next time you log in. Caution: membership in the docker group grants privileges equivalent to root on the server.
-
docker run hello-worldRun a small test container that prints a success message, a quick check that the installation works.
Running containers
-
docker run -d --name web nginxStart a container named web in the background from the nginx image. When the image is not on the computer, Docker downloads it automatically.
-
docker run -d -p 8080:80 --name web nginxMap port 8080 on the host to port 80 inside the container. The first number is always the host port, and the second number is the container port.
-
docker run -d -p 127.0.0.1:8080:80 --name web nginxExpose the port only on the host's local address, for example when a local proxy server forwards requests to it. Docker Engine opens mapped ports in firewall rules that it manages itself, so a port mapped without an address may be reachable from the network even when the host firewall seems to block it.
-
docker run -d -v /srv/web:/usr/share/nginx/html:ro --name web nginxMount a host directory into the container (bind mount). The :ro suffix mounts the directory as read-only.
-
docker run -d -v app-data:/data --name app my-imageMount a volume named app-data at the /data path in the container. The data in the volume is kept even after the container is deleted.
-
docker run -d --mount type=bind,source=/srv/app,target=/app,readonly my-imageMount a directory with explicit, more readable syntax. Unlike the -v option, the --mount option returns an error when the source path does not exist, instead of creating an empty directory.
-
docker run -d -e APP_ENV=production --name app my-imageSet an environment variable inside the container. Repeat -e for each additional variable.
-
docker run -d --env-file .env --name app my-imageLoad all environment variables from a file, one variable per line in the KEY=value format.
-
docker run -d --restart unless-stopped --name web nginxRestart the container automatically after a crash or a server restart, unless the container was stopped manually. The other values are always, on-failure, and no, which is the default.
-
docker run -it --rm ubuntu bashOpen an interactive shell in a temporary container that is deleted automatically when you exit it.
-
docker run -d --memory 512m --cpus 1 --name app my-imageLimit the container to 512 MB of memory and one CPU core, so a single process cannot use up all the server's resources.
-
docker run -d --network app-net --name api my-imageConnect the container to an existing network, where the other containers can reach it by the name api.
-
docker run -d --log-opt max-size=10m --log-opt max-file=3 --name app my-imageLimit the container log to at most three files of 10 MB each. Without a limit, the log of a running container can grow and fill the disk.
-
docker run -d --name web -p 8080:80 -v /srv/web:/usr/share/nginx/html:ro --restart unless-stopped nginxA full example that combines a name, a port mapping, a read-only directory, and a restart policy in one command.
Managing containers
-
docker psList the running containers, including the ID, image, status, and mapped ports.
-
docker ps -aList all containers, including containers that stopped or crashed.
-
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"Show a compact table with only the name, status, and ports, easy to read when many containers are running.
-
docker stop webAsk the container to shut down gracefully, and if it does not shut down within 10 seconds by default, stop it by force.
-
docker start webStart an existing stopped container again, with the settings it was created with.
-
docker restart webStop the container and start it again.
-
docker kill webCaution: stop the container immediately, without a graceful shutdown, so use it only for a container that does not respond.
-
docker rm webDelete a stopped container. Caution: data saved inside the container, and not in a volume or a mounted directory, is deleted with it.
-
docker rm -f webCaution: stop a running container immediately, without a graceful shutdown, and delete it in one step.
-
docker exec -it web shOpen a shell inside a running container. In images that include bash, you can replace sh with bash.
-
docker exec -u root -it web shOpen a shell inside the container as the root user, even when the container is set to run as another user.
-
docker exec web cat /etc/hostsRun a single command inside the container and show its output, without opening a shell.
-
docker cp web:/etc/nginx/nginx.conf ./nginx.confCopy a file from inside the container to the current directory on the host.
-
docker cp ./index.html web:/usr/share/nginx/html/Copy a file from the host into the container. The change is lost when the container is deleted and created again.
-
docker update --restart unless-stopped webChange the restart policy of an existing container, without creating it again.
-
docker commit web my-image:snapshotCreate a new image from the current state of the container. The image does not include data in volumes. For regular use, build the image from a Dockerfile so you can reproduce it.
Logs and monitoring
-
docker logs webShow all the output the container wrote to standard output and standard error.
-
docker logs -f --tail 100 webShow the last 100 log lines and keep following new lines in real time. Press Ctrl+C to stop following.
-
docker logs --since 30m webShow the log entries from the last 30 minutes. You can also specify hours, such as 2h, or a full date.
-
docker statsShow the CPU, memory, network, and disk I/O of every running container in real time.
-
docker stats --no-streamShow a single snapshot of resource use and exit, suitable for scripts.
-
docker top webShow the processes running inside the container, without entering it.
-
docker inspect webShow all the settings and the state of the container in JSON format, including environment variables, mounted directories, networks, and IP addresses.
-
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' webShow only the container's internal IP address.
-
docker port webShow the container's port mappings, meaning which host port points to each container port.
Images
-
docker pull nginxDownload the image from an image registry, Docker Hub by default. Without a tag, the latest tag is downloaded.
-
docker pull nginx:alpineDownload a specific version of the image by tag. Images based on Alpine Linux are much smaller than the regular version.
-
docker imagesList all images on the computer, with the name, tag, and size of each image.
-
docker rmi my-imageDelete an image from the computer. You cannot delete an image that an existing container uses, even when the container is stopped.
-
docker build -t my-image .Build an image named my-image from the Dockerfile in the current directory. The dot at the end of the command specifies the directory whose files are sent to the build.
-
docker build -f Dockerfile.prod -t my-image:prod .Build an image from a Dockerfile with a different name and tag it prod.
-
docker build --no-cache -t my-image .Rebuild the image from scratch, without using steps cached from a previous build.
-
docker build --build-arg BUILD_ENV=production -t my-image .Pass a value to a variable defined in the Dockerfile with the ARG instruction. The value is available only during the build, not in the running container.
-
docker tag my-image registry.example.com/team/my-image:latestAdd another name and tag to an existing image, usually before pushing it to a private registry. The command does not create another copy on disk.
-
docker login registry.example.comLog in to a private image registry. Without an address, you log in to Docker Hub.
-
docker push registry.example.com/team/my-image:latestPush the image to the registry. The image name must include the registry address, and on Docker Hub, your username, for example user/my-image.
-
docker history my-imageShow the layers the image is built from and the size of each layer, to find a step that makes the image larger than necessary.
-
docker image inspect my-imageShow the image settings in JSON format, including the exposed ports, the environment variables, and the command that runs at startup.
-
docker save -o my-image.tar my-imageSave the image as a single tar file, to move it to another server without an image registry.
-
docker load -i my-image.tarLoad an image from a file created with the docker save command.
-
docker images -f dangling=trueList dangling images without a name or tag, usually created when a new build replaces an existing tag.
Volumes
-
docker volume lsList all volumes on the computer.
-
docker volume create app-dataCreate a new volume named app-data. The docker run command creates a volume automatically when the name does not exist.
-
docker volume inspect app-dataShow the volume details, including the path on the host disk where the data is actually stored.
-
docker volume rm app-dataCaution: permanently delete the volume and all the data in it. You cannot delete a volume that an existing container uses.
-
docker volume ls -f dangling=trueList volumes that are not attached to any container. Check their contents before you delete them, because they may hold important data.
-
docker run --rm -v app-data:/data -v "$(pwd)":/backup alpine tar -czf /backup/app-data.tar.gz -C /data .Back up the contents of the volume to a compressed file in the current directory, using a temporary container. For a consistent database backup, stop the container that uses the volume first.
-
docker run --rm -v app-data:/data -v "$(pwd)":/backup alpine tar -xzf /backup/app-data.tar.gz -C /dataRestore a backup into the volume. Caution: existing files with the same name are overwritten.
Networks
-
docker network lsList all Docker networks, including the default networks bridge, host, and none.
-
docker network create app-netCreate a new network. Containers on the same network reach each other by container name, without IP addresses.
-
docker network inspect app-netShow the network settings and the containers connected to it, with the IP address of each container.
-
docker network connect app-net webConnect a running container to an additional network, without restarting it.
-
docker network disconnect app-net webDisconnect the container from the network.
-
docker network rm app-netDelete a network. Disconnect all connected containers from it first.
-
docker run -d --network host --name app my-imageRun the container directly on the host network, without network isolation and without port mapping. In this mode, the -p option has no effect.
Docker Compose
-
docker compose versionShow the Docker Compose version and confirm that the plugin is installed.
-
docker compose up -dCreate and start in the background all the services defined in the compose.yaml file in the current directory. Running it again recreates only the services whose settings or image have changed.
-
docker compose up -d --buildRebuild the images of the services defined with build, and then start all the services.
-
docker compose up -d webStart or update only the web service, together with the services it depends on.
-
docker compose up -d --remove-orphansStart the services and remove the containers of services that were deleted from the file.
-
docker compose downStop and delete the project's containers and networks. Volumes and images are kept.
-
docker compose down -vCaution: in addition to the containers and networks, permanently delete the project's volumes, including the databases and files stored in them.
-
docker compose psList the project's containers, with the status of each service and the mapped ports.
-
docker compose logs -fShow the logs of all services together and keep following them in real time, with each line labeled with the service name.
-
docker compose logs -f --tail 100 webFollow only the log of the web service, starting from the last 100 lines.
-
docker compose pullDownload the latest versions of all images defined in the file, without restarting the services.
-
docker compose pull && docker compose up -dUpdate the services to the latest images, recreating only the containers whose image has changed.
-
docker compose buildBuild the images of the services defined with build, without starting them.
-
docker compose restart webRestart the web service. The command does not load changes to the compose.yaml file. To apply them, run docker compose up -d.
-
docker compose stopStop all the project's services without deleting the containers. The docker compose start command starts them again.
-
docker compose exec web shOpen a shell inside the running container of the web service.
-
docker compose run --rm web shCreate a new temporary container from the service settings and run a one-off command in it, for example a database schema update. The container is deleted when the command finishes.
-
docker compose configCheck that the file is valid and show the final configuration, after environment variables are substituted and files are merged.
-
docker compose -f compose.yaml -f compose.prod.yaml up -dStart the project from a combination of several files, where each file overrides settings from the file before it.
Cleanup and disk space
-
docker system dfShow the space used by images, containers, volumes, and the build cache, and how much of it can be freed.
-
docker system df -vShow a breakdown of the space used by each image, container, and volume.
-
docker container pruneCaution: delete all stopped containers, after asking for confirmation.
-
docker image pruneDelete dangling images without a name or tag. This is relatively safe, because no container uses these images.
-
docker image prune -aCaution: delete all images that no container uses. To use them again, you need to download or build them again.
-
docker builder pruneDelete the build cache, which can take up a lot of space on a computer where images are built often. The next build will be slower.
-
docker volume pruneCaution: delete volumes that are not attached to any container, including the data in them. In recent versions, only unnamed volumes are deleted by default, and the -a option extends the deletion to named volumes as well.
-
docker network pruneDelete all networks that no container is connected to.
-
docker system pruneCaution: delete stopped containers, unused networks, dangling images, and the build cache in one step. Volumes are not deleted.
-
docker system prune -a --volumesCaution, use with extra care: in addition to what docker system prune deletes, delete every unused image and the volumes that are not attached to any container, including the data in them. In recent versions, the --volumes option deletes only unnamed volumes. Run docker system df first and make sure no data you need will be deleted.
Dockerfile
-
FROM nginx:alpineSet the base image the new image is built on. The instruction usually appears on the first line of the file.
-
WORKDIR /appSet the working directory for the following instructions and for the running container, and create the directory if it does not exist.
-
COPY . .Copy files from the build directory to the working directory in the image. Files listed in the .dockerignore file are not copied.
-
ADD archive.tar.gz /app/Copy files like COPY, and also extract a local archive automatically. For a regular copy, the COPY instruction is clearer and more predictable.
-
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*Run a command during the build and save the result as a layer in the image. Combining the commands with && and cleaning up the package lists in the same instruction keep the image small.
-
ENV APP_ENV=productionSet an environment variable that is available both during the build and in the running container.
-
ARG BUILD_ENV=productionDefine a variable that is available only during the build, with a default value you can change with the --build-arg option. Do not pass passwords through ARG, because the value is saved in the image history. For secrets during the build, you can use RUN --mount=type=secret.
-
EXPOSE 8080Document the port the application listens on. The instruction does not actually publish the port, which requires a mapping with -p when you run the container.
-
USER appSet the user that runs the following instructions and the container itself. Running as a user other than root limits the damage if the application is compromised.
-
CMD ["npm", "start"]Set the default command that runs when the container starts. A command written after the image name in docker run replaces it.
-
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]Set a fixed program that runs every time the container starts. When both ENTRYPOINT and CMD are set, the CMD values are passed as arguments to ENTRYPOINT.
-
HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost:8080/ || exit 1Define a periodic health check. The healthy or unhealthy status appears in docker ps. The check in the example requires curl to be installed in the image.
-
VOLUME /dataMark a directory for storing data outside the image layers. When no volume is mounted to it at run time, Docker creates an anonymous volume.
-
COPY --from=build /app/dist /usr/share/nginx/htmlCopy files from an earlier build stage defined in the line FROM node:lts AS build. A multi-stage build produces a small final image, without the build tools.
-
.dockerignoreThe .dockerignore file in the build directory lists the files and directories not to send to the build, such as .git, node_modules, and .env. This list shortens the build and prevents secrets from being copied into the image.
Troubleshooting
-
docker ps -a --filter "status=exited"List only containers that have stopped, to find a service that crashed or did not start.
-
docker inspect --format '{{.State.ExitCode}}' webShow the container's exit code. Code 137 means the process was stopped by force, for example because it exceeded the memory limit, and code 1 usually indicates an error in the application itself.
-
docker inspect --format '{{.State.OOMKilled}}' webShow true when the container was stopped because it exceeded the memory limit or the host ran out of memory.
-
docker inspect --format '{{json .State.Health}}' webShow the health check status and the output of the latest checks, to find the reason for an unhealthy status.
-
docker run --rm -it --entrypoint sh my-imageOpen a shell in the image instead of its regular command, to check the files and settings of a container that crashes right after it starts.
-
docker exec web envShow the environment variables the container actually receives, to check that the values from the .env file were loaded as expected.
-
docker diff webShow the files added, changed, or deleted in the container compared to the image it was created from.
-
sudo ss -tulpn | grep 8080Show which process is already listening on the port, when starting the container fails with the error port is already allocated.
-
docker events --filter container=webShow the container's events in real time, such as start, stop, crash, and health status changes.
-
sudo journalctl -u docker -fFollow the log of the Docker service itself in real time, to find problems that do not appear in the container logs.
No matching commands.
Docker packages an application, together with everything it needs to run, into an image and runs it in a container, in an isolated environment. This way the application runs the same on a development computer and on a server. On Linux servers, Docker Engine is installed directly on the system, and on Windows and Mac computers, you can use Docker Desktop.
When an application includes several services, such as a website, a database, and a cache, it is easier to describe all of them in one compose.yaml file and manage them with docker compose commands. To convert an existing docker run command to a Compose file, you can use the docker run to Compose converter, and to check the structure of a YAML file or convert it to JSON, you can use the YAML and JSON converter.
Two habits prevent many problems. Before you run any prune or down -v command, check what is about to be deleted, and for regular use, give the image an explicit tag instead of latest, so a new version is not installed without your knowledge.
On a Docker VPS, Docker and Docker Compose are installed and configured when the server is set up.
Docker commands FAQ
What is the difference between an image and a container?
The difference between an image and a container is like the difference between an installation file and a running program. An image is a fixed, read-only template that includes the application and all its dependencies, and a container is a running instance of the image. You can run several containers from one image at the same time, and a change inside a container does not change the image.
What is the difference between docker compose and docker-compose?
The only difference between docker compose and docker-compose is how they are installed. The docker compose command, with a space, is a Docker plugin that comes with new installations, and the docker-compose command, with a hyphen, is the separate tool that was used in the past. Both commands read the same file and accept almost the same options.
How do I save data so it is not deleted with the container?
Data is kept over time when it is written to a volume or to a directory mounted from the host, not inside the container itself. With docker run, add -v app-data:/data, and in a Compose file, define the volume under the service and also in the top-level volumes list of the file. This way, you can delete the container, update the image, and create a new container, and the data stays in place.
How do I update a container to a new version of the image?
You update a container by creating a new container from the updated image, not by updating the existing container. In a Compose project, run docker compose pull and then docker compose up -d. For a container started with docker run, download the image with docker pull, stop and delete the old container, and run the same docker run command again.
Why does the container stop right after it starts?
A container runs only as long as its main process runs, so it stops when the process ends or crashes. The reason usually appears in the log, which the docker logs web command shows, and the docker inspect command shows the exit code. Common causes are a missing environment variable, an error in a configuration file, and a startup command that ends immediately instead of continuing to run.
How do I free up disk space used by Docker?
The safe way is to first check with the docker system df command what takes up the space, and then delete only what you do not need. The docker system prune command deletes stopped containers, dangling images, and the build cache, and it does not delete volumes. Container logs without a size limit are another common cause of a full disk, so set a maximum size for them.