Tuesday, May 7, 2019

Docker useful commands

1)Delete all containers
docker rm $(docker ps -a -q)

2)Delete all images
docker rmi $(docker images -q)

3)To login as root
docker  exec -it --user root bc bash

4)To run the container in background in interactive mode.
docker run -dit ubuntu


5) START, STOP, PAUSE, RESTART
docker stop <container_id>
docker pause <container_id>
docker start <container_id>
docker restart <container_id>
docker exec -it <container_id> bash

6) To view and remove images
sudo docker images
sudo docker rmi

7) To retrieve logs
docker logs <container_name>

8) Docker Filter by name example
docker ps -a -q --filter "name=jenkins"

9) To view running and stopped containder
docker ps -a

10) To remove docker container
docker rm name/container_id

11) To remove docker image
docker rmi image_name/container_id

12) To pull the image
docker pull ubuntu

13) To run the docker container
docker run ubuntu

This command will exit the container because there is nothing to do for the container

13) To run a command by starting the container and exit it after execution
docker run ubuntu sleep 5

14) To execute a command on a already running container
docker exec Docker_name/Container_id cat /etc/hosts

15) To run the docker in the background without attaching to it
docker -d run ubuntu

16) To attach to the container that is already running and make it in foreground
docker attach container_id/container_name

17) To login to a already running container
docker exec -it 2302174c379b bash

18) To link internal port 5000 of docker container from host port 80
docker run -p 80:5000 ubuntu

PORT MAPPING:

19) Multiple host ports can be mapped to different container ports
docker run -p 80:5000 ubuntu #docker1
docker run -p 8000:5000 ubuntu #docker2

20) Same host port cannot be mapped to different container ports
docker run -p 80:5000 ubuntu #docker1
docker run -p 80:5000 ubuntu #docker1
This will throw error that port is already mapped

VOLUME MAPPING:

21) To map host dir /opt/datadir to container dir /var/lib/mysql
docker run -v /opt/datadir:/var/lib/mysql mysql

Networking
22) To find out the ip address of the docker container
docker inspect Container_id
"IPAddress": "172.17.0.3"
Look at the Networks sections and find the IPAddress

or use
docker inspect -f '{{ .NetworkSettings.IPAddress }}' Container_id

23)

No comments:

Post a Comment