Common Docker Commands

  • Post author:
  • Post category:Commands

Here are some common Docker commands.

1. Search the Docker repository (like Docker Hub) for Docker images.

docker search IMAGE

2. Pull an image from the Docker repository.

docker pull IMAGE

3. Print the image change history.

docker history IMAGE

4. List all images on the host system.

docker images

5. Create and run a container. A container is an instance of an image.

docker run --name CONTAINER IMAGE

6. Create and run an interactive container.

docker run -it --name CONTAINER IMAGE

7. Create and run a container, and remove container on exit.

docker run --rm --name CONTAINER IMAGE

8. Create and run a container with a mounted volume from the host.

docker run -d --name CONTAINER -v HOST_DIR:CONTAINER_DIR IMAGE

9. Create and run a container as a daemon.

docker run -d --name CONTAINER IMAGE

10. Attach to a running container.

docker attach CONTAINER

11. Tail the container console.

docker logs -f CONTAINER

12. Stop, start or restart a container.


docker stop CONTAINER

docker start CONTAINER
docker restart CONTAINER

13. List running containers.

docker ps

14. List all containers.

docker ps -a

15. List container ports.

docker port CONTAINER

16. Print low-level information on a container or image.

docker inspect CONTAINER|IMAGE

17. Delete a container, stop it first if running.

docker rm -f CONTAINER

18. Delete an image.

docker rmi IMAGE

19. Create a new image based on a Dockerfile.

docker build -t IMAGE

20. Commit container changes made using an interactive shell into a new image.

docker commit CONTAINER IMAGE:TAG

21. Push an image to the Docker repository.

docker push IMAGE:TAG

22. Display Docker system information.

docker info

23. Display running processes inside the container.

docker top CONTAINER

24. Display the container’s resource usage.

docker stats CONTAINER

25. Link two containers, a web application connecting to a database, using a secure tunnel.


docker run -d --name DB_CONTAINER DB_IMAGE
docker run -d -P --name WEB_CONTAINER --link DB_CONTAINER:DB_ALIAS WEB_IMAGE

26. Create a new Bash session inside the container.

docker exec -it CONTAINER bash

27. Update to the latest Docker image. This will remove the old Docker container.

 
docker pull IMAGE
docker stop CONTAINER
docker rm CONTAINER
docker run -d --name CONTAINER IMAGE

Bonus: Install Docker on Red Hat or CentOS.

yum install docker-io; service docker start; chkconfig docker on

This Post Has One Comment

  1. Murtuza

    A quick and helpful docker commands reference.

Comments are closed.