Docker has a lot to talk about. But I am going to list a few commands here that come handy while working with it on a daily basis. We will list them in a sequence that looks like a tutorial.
1. List all docker containers
docker container ls
2. List all images
docker image ls
docker images
3. Spawn & run from an image, a new container.
docker run image_name COMMAND
Example: docker run -ti ubuntu bash
Note (1) : The tag "latest" is automatically used if no tag is specified.
Note (2) : If the image is not locally available, then it will be downloaded.
Note (3) : To exit from container, press CTRL+D or type 'exit'.
4. Useful arguments with the docker run command
- -t : Run in a terminal
- -I : Interactive mode
- --rm : Run and then, delete this container when it exits.
- -d : Detached . To run container and leave it running in the background.
5. See all running containers
docker ps
6. See the last container that was run/exited.
docker ps -l
Note: Containers are not deleted automatically .
7. See all stopped containers list.
docker ps -a
8. Commit the changes you made to the container.
docker commit container_id (or NAMES . Get that using docker ps -l command)
Note: This does not change the container specifies her , BUT it creates a new image out of it.
The id of this new IMAGE , is returned to us after the commit.
Run "docker images" to see this new image in the list.
9. Add a tag to an image.
docker tag image_id some_name
10. Commit changes and tag container together.
docker commit container_id image_name
11. Working on a detached container.
docker attach container_id or name
Note: To exit (but not kill . Just leave it again running in the background) , press the key sequence:
Ctrl + p Ctrl + q
12. Run some extra command/process on the running container
docker exec
Example: docker exec -ti container_id/name COMMAND
Note: Do the above on a new terminal. You probably are already in the attached state to the same container on a different terminal.
13. Stop a container.
docker container stop container_id
14. Delete/remove a container.
docker container rm container_id
Note: You can also use the Name.
15. Delete an Image
docker image rm <Image Name>
16. Remove all unused containers