Docker - Commands



Docker commands are instructions used to interact with Docker objects such as images, containers, and networks, with some acting directly on the Docker engine itself. These help developers and administrators in building, managing, and deploying containerized applications.

Typical commands include docker run for creating and starting a container, docker ps to list the running containers, docker build to create an image from a Dockerfile, docker pull to download an image from a registry, and docker push to upload an image into a registry. Coupled with many other useful commands, they make up a full set of utilities for dealing with the whole lifecycle of Dockerized applications.

Lifecycle of Dockerized Applications

Docker Container Commands

You can use the Docker container commands to manage the lifecycle of containers. With these commands, you can create, start, stop, inspect, or remove containers. Here are the top five most important container commands −

docker run − This command allows you to create a new container from a specified Docker image and start it.

$ docker run -d -p 80:80 --name mywebserver nginx 
docker run Command

The above command will start a detached (-d) Nginx web server container, publish port 80 of the container to port 80 on the host machine (-p 80:80) and name the container mywebserver.

docker ps − If you want to list all the running containers, you can use the below command.

$ docker ps
docker ps Command

This command lists all containers currently running on your system.

docker stop − To stop a running container, you can use this command.

$ docker stop mywebserver
docker stop Command

This stops the container named mywebserver.

docker rm − To remove a stopped container, you can use the docker rm command.

$ docker rm mywebserver
docker rm Command

This removes the stopped container named mywebserver.

docker logs − For debugging purposes, if you want to display the logs of a container, you can use the docker logs command.

$ docker logs mywebserver
docker logs Command

This shows the logs for the container named mywebserver, which can be useful for debugging.

Docker Image Commands

If you want to work with Docker images, Docker provides with a set of handy commands to manage images. Here are five of the most important image commands, along with examples −

docker build − You can use this command to build a Docker image using a Dockerfile. The Dockerfile is a text file that has instructions for creating the image and its several layers.

$ docker build -t myimage 
docker build Command

The above command will build an image with the name myimage using the Dockerfile located in the current directory (.).

docker pull − If you want to pull an image from a Docker registry, such as Docker Hub, you can use the Docker pull command.

$ docker pull ubuntu:latest
docker pull Command

When you run this command, it will pull the latest version of the official Ubuntu image from the Docker Hub.

docker images − While working with Docker, you will often feel the need to list all the available Docker images in your local. The Docker images command below will help you do that.

$ docker images
docker images Command

The above command lists all Docker images stored locally in your system.

docker rmi − If you want to remove a particular image from your machine, you can use this command.

$ docker rmi myimage
docker rmi Command

This command will permanently delete the image myimage.

docker tag − This command creates a new tag for an image. Tags in Docker images are used to identify and differentiate between different versions of an image.

$ docker tag myimage myrepo/myimage:v1.0
docker tag Command

This creates a new tag myrepo/myimage:v1.0 for the image myimage.

Docker Volume Commands

For data persistence and sharing between containers, Docker volumes can be used as it allows data to be stored independently of container lifecycles. To work with Docker volumes, here are five useful commands with examples −

docker volume create − Creates a new Docker volume.

$ docker volume create myvolume
docker volume create Command

docker volume ls − Lists all volumes available on your system.

$ docker volume ls
docker volume ls Command

docker volume inspect − Provides detailed information about a specific volume.

$ docker volume inspect myvolume
docker volume inspect Command

docker volume rm − Removes a specified volume.

$ docker volume rm myvolume
docker volume rm Command

docker volume prune − Removes all unused volumes from your system, freeing up space.

$ docker volume prune
docker volume prune Command

All Important Docker Commands

Here's a table summarizing some of the top Docker container commands, their use cases, and examples −

Command Use Case Example
docker run Create and start a new container docker run -it ubuntu bash
docker ps List running containers docker ps
docker ps -a List all containers (including stopped ones) docker ps -a
docker start Start a stopped container docker start my_container
docker stop Stop a running container docker stop my_container
docker restart Restart a container docker restart my_container
docker rm Remove a stopped container docker rm my_container
docker rmi Remove an image docker rmi my_image
docker images List all Docker images docker images
docker pull Download an image from a registry docker pull nginx
docker build Build an image from a Dockerfile docker build -t my_image
docker exec Run a command in a running container docker exec -it my_container bash
docker logs View the logs of a container docker logs my_container
docker cp Copy files/folders between a container and the local filesystem docker cp my_container:/path/to/file /local/path
docker inspect Display detailed information on a container or image docker inspect my_container
docker network ls List all Docker networks docker network ls
docker network create Create a new Docker network docker network create my_network
docker-compose up Create and start containers using a docker-compose.yml file docker-compose up
docker-compose down Stop and remove containers, networks, images, and volumes docker-compose down
docker volume ls List all Docker volumes docker volume ls
docker volume create Create a new Docker volume docker volume create my_volume
docker info Display system-wide information docker info
docker stats Display a live stream of container resource usage statistics docker stats
docker attach Attach local standard input, output, and error streams to a container docker attach my_container

Conclusion

In conclusion, the Docker commands that we discussed in this chapter are the most essential ones for the effective management and deployment of containerized applications. Understanding these commands and their common applications will be useful in the day-to-day activities of developers and administrators easing workflows, assuring persistence, and optimizing resource consumption.

FAQs on Docker Command

1. What is the Difference between docker run and docker start?

The docker run command creates a new container from an image and starts it in one go. This is the combination of two steps: docker create, which creates a new container, and docker start, which starts a stopped container. Contrary to this, docker start is used to start an existing container that has been stopped previously.

2. How do I view the logs of a running Docker container?

You can view the logs of a container using the docker logs command followed by the name or ID of the container. For example, docker logs mycontainer will return the logs of the container called mycontainer. You can use the flag -f just after docker logs to print the logs in real-time when new entries are added.

3. How to run a Docker container in detached mode?

The -d flag in the docker run stands for "detached" mode. When you run a container in detached mode, it runs in the background, and the terminal is not attached to its standard input and output. This means you can continue using the terminal for other tasks while the container runs independently. You can reconnect to the container's input and output using the docker attach command.

Advertisements