Useful docker commands
- list all docker images
docker image ls
2. list all docker containers
docker ps -a
Without -a , it will only display the container that is running.
3. build the docker image
docker image build -t [name of the image] .
If we have Dockerfile with an extension such as .dev , then we should run the following command
docker build -f Dockerfile.dev .
Sometimes, if you forget to tag image name, you will get the following result in your command prompt (terminal) with running docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE<none> <none> 01367f96bd59 3 minutes ago 414MB
Then you can solve the problem by tagging the container with the following command
docker tag [image id] [name of the image]
Variable explanation
--tag, -t : [options] name of the image
. : current path for your build context
4. Run the docker container
docker run -d -p [host port]:[container port] --name [name of container] [name of image]
Variable explanation
--detach, -d :[options] Run container in background and print container ID
With detach , the output will return the container id instead of outputting your program/system messages.
--publish , -p :[options] Publish a container’s port(s) to the host
If your flask server or other program has default port, you must match your container port with the default port in your program.
5. Stop running the docker container
docker stop [container id /container name]
6. Restart the docker container
docker restart [container id/container name]
7. Remove the docker container
docker rm [container id/container name]
7.1 Remove all docker container with status “Exited”
docker rm $(docker ps -a -f status=exited -q)
docker rm -v $(docker ps -aq -f status=exited)
`-v` argument will delete any Docker-managed volume that aren’t referenced by other containers.
8. Remove all images
docker rmi -f $(docker images -a -q)
9. Execute an interactive bash shell on the container
docker exec -it [container id/container name] bash
Variable explanation
--interactive, -i: [options] Keep STDIN open even if not attached-tty , -t:[options] Allocate a pseudo-TTY
10. Show the logs on the container
docker logs --details [container id/container name]
11. Show the real time logs on the container
docker logs -f [container id/container name]
12. Show the full text for docker container
docker container ls --no-trunc