Bytes

Docker Commands

Last Updated: 15th January, 2024

Docker is a popular open-source platform that developers and system administrators use for creating, deploying, and running applications in containers. Containerization with Docker allows for the packaging of an application along with its required dependencies into a standardized unit for software development.

This tutorial provides an overview of various Docker commands, including their explanations and examples. These commands are essential for managing the lifecycle of Docker containers, images, and systems. Whether you are a beginner just starting out with Docker or a seasoned user, this guide will serve as a useful reference for understanding and using Docker commands.

Docker Commands

Docker provides a comprehensive set of commands to manage and manipulate Docker objects such as containers, images, volumes, networks, and more.

Here are some of the basic Docker commands:

1. Docker build command: This command is used to build Docker images from a Dockerfile and a "context". A build's context is the set of files located at a specified path or URL.

For example, docker build -t myapp:1.0 . will build an image named "myapp" with tag "1.0" using the Dockerfile in the current directory.

2. Docker run command: This command is used to create and start a container from an image.

For example, docker run -d -p 8080:80 myapp:1.0 will start a container in detached mode (-d), mapping port 8080 of the host to port 80 of the container, using the "myapp:1.0" image.

3. Docker exec command: This command is used to run a command in a running container.

For example, docker exec mycontainer ls /app will execute the "ls /app" command in the "mycontainer" container, listing the files in the /app directory inside the container.

4. Docker commands list: To get a list of available Docker commands, you can simply run docker or docker --help.

5. Docker compose command: Docker Compose is a tool for defining and managing multi-container Docker applications.

For example, docker-compose up will start all services defined in the "docker-compose.yml" file in the current directory, while docker-compose down will stop all these services.

6. Docker tag command: This command is used to create a tag TARGET_IMAGE that refers to SOURCE_IMAGE.

For example, docker tag myapp:1.0 myrepo/myapp:1.0 will create a new tag "myrepo/myapp:1.0" that refers to the "myapp:1.0" image.

7. Docker logs command: This command fetches the logs of a container.

For example, docker logs mycontainer will display the logs of the "mycontainer" container.

8. Docker login command: This command is used to log in to a Docker registry.

For example, docker login myregistry.com will prompt for a username and password to log in to the myregistry.com registry.

9. Docker push command: This command is used to push an image or a repository to a registry.

For example, docker push myrepo/myapp:1.0 will push the "myapp:1.0" image to the "myrepo" repository on the Docker registry.

10. Docker ps command: This command lists running containers.

For example, docker ps will display a list of all running containers, while docker ps -a will list all containers, whether they are running or not.

11. Docker file command: Dockerfile is a text file that contains all the commands to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

12. Docker cp command: This command copies files/folders between a container and the local filesystem.

For example, docker cp mycontainer:/app/data ./data will copy the /app/data directory from "mycontainer" to the current directory on the host.

13. Docker prune command: This command is used to remove all stopped containers, all build cache, and all unused networks or images.

For example, docker system prune will remove all of these resources, while docker system prune -a will additionally remove any unused images.

14. Docker commit command: This command creates a new image from a container's changes.

For example, docker commit mycontainer mynewimage:1.0 will create a new image "mynewimage" with tag "1.0" from the current state of "mycontainer".

15. Docker copy command: This is an alias for the docker cp command.

16. Docker pull command: This command is used to pull an image or a repository from a registry.

For example, docker pull ubuntu:latest will pull the latest version of the Ubuntu image from the Docker Hub.

17. Docker start command: This command starts one or more stopped containers.

For example, docker start mycontainer will start the "mycontainer" container.

18. Docker image command: This command manages images.

For example, docker image ls will list all images, while docker image rm myimage:1.0 will remove the "myimage:1.0" image.

19. Docker save command: This command saves one or more images to a tar archive.

For example, docker save -o myimage.tar myimage:1.0 will save the "myimage:1.0" image to a tar file named "myimage.tar".

20. Docker inspect command: This command returns low-level information on Docker objects.

For example, docker inspect mycontainer will display detailed information about the "mycontainer" container.

21. Docker restart command: This command restarts one or more containers.

For example, docker restart mycontainer will stop and then start the "mycontainer" container.

Conclusion

Docker is a powerful tool that simplifies the deployment and scaling of applications. It achieves this by packaging applications and their dependencies into containers, which are standalone executable packages. Docker provides a range of commands that allow developers to interact with these containers and images.

From creating and managing containers (docker rundocker startdocker stop, etc.) to handling images (docker builddocker pulldocker push, etc.), Docker offers a comprehensive suite of commands. These commands enable developers to control every aspect of their Docker environment, from building images from Dockerfiles to running containers from those images.

Furthermore, Docker provides commands for managing volumes (docker cp), networking (docker network), and even logging (docker logs). There are also commands for cleaning up resources (docker prune) and inspecting the state of Docker objects (docker inspect).

Understanding and effectively using these Docker commands is crucial for anyone working with Docker, regardless of their level of experience. Whether you're just getting started with Docker or are an experienced user, having a solid grasp of these commands will greatly enhance your productivity and efficiency.

Key Takeaways

1. Docker Build Command: This command is used to construct Docker images from a Dockerfile and a "context". It's crucial for creating custom images based on your application requirements.

2. Docker Run Command: This command is used to create and start a container from an image. It's essential for deploying applications in isolated environments.

3. Docker Exec Command: This command allows you to run commands in a running container. It's useful for executing tasks inside a container after it has been deployed.

4. Docker Login Command: This command is used to authenticate with a Docker registry, allowing you to pull and push images.

5. Docker Pull Command: This command is used to download an image or a repository from a registry.

Module 2: Working with DockerDocker Commands

Top Tutorials

Related Articles

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter