Docker container commands :-
# container
home :- /var/lib/docker/containers/<container_ID>
#
docker container --help
showing
total details for docker
#
docker info
- Search image for found the image from Docker
repo.
# docker search <image_name>
-t :- Allocate a pseudo-TTY
-i :-
interactive (Keep STDIN open even if not
attached)
-d :-
detach (Run container in background and
print container ID)
- Run a container interactive mode. If you logout
from container, container will be stop and delete.
#
docker container run --name <container_name> -it -p<HostPort>:<Container_port>
<Image_name>:<tag_name> <login
to container use shell>
#
docker container run --name web_tomcat -it
-p 80:8080 shashikant11/tomcat8:latest
/bin/bash
- Run a container detached mode. This mode use
for run container in the background.
# docker container run --name
web_tomcat -d -p 8080:8080 tomcat8:latest
- Particular IP add with container.
#
docker container run --name web_tomcat -d
-p 192.168.1.24:8080:8080 tomcat8:latest
- Start/ Stop /Restart command for Container.
#
docker container stop <container_ID>
#
docker container start <container_ID>
#
docker container restart <container_ID>
- Check running container and history.
#
docker container ls
#
docker container ps
#
docker container ls -a
- remove/ pause/ unpause/kill/prune
command
#
docker container rm <container_ID>
#
docker container pause <container_ID>
#
docker container unpause <container_ID>
#
docker container kill <container_ID>
#
docker container docker prune {stopped all container}
- show container details like IP, volume, port
#
docker container inspect <container_ID>
#
docker container port <container_ID>
- Login in container
#
docker container exec -it <container ID> /bin/bash
#
docker container attach <container ID>
- Check cpu load and memory for container running.
#
docker container top <container ID>
#
docker container stats
- diff command for show change create, delete ,
add file.
# docker container diff <container
name>
- check container logs.
#
docker container logs <container ID> -f
#
docker container logs <container ID> --tail 6
#
vim /var/lib/docker/containers/<container ID>/0306685bc0d7-json.log
#
tail -f /var/log/messages
- Rename container name
#
docker container rename <container ID> <new name>
- Copy data from localhost to container.
#
docker container cp test/ <container ID>:/tmp/
- update command memory/cpu
docker
container update -m "300M" --memory-swap -1 0306685bc0d7
- create container backup.
#
docker container export <container ID> -o ubnutu_file.tar
#
docker image import ubnutu_file.tar ubnutu_file
- running container ka image create ##
# docker container commit --author "shashikant" -m "this is test commit" <container ID> <new image_name>
18. add hostname in Container .
# docker container run --name web_tomcat --hostname shashi.example.com -d -p 8080:8080 tomcat8:latest
19. Exit interactive mode .
# ctrl + p + q
20. check image history.
# docker image history nginx
21 . start container with nginx
# docker container run --name=nginx_server -itd -p 80:80 new_nginx:latest nginx -g 'daemon off;'
22.Container restart policies are :-
1. no :- Do not automatically restart the container. (the default).
2. always :- If the container stops and docker daemon restarts for any reasons whatsoever, always attempt to restart it.
# sudo docker run -d --name testing_restarts --restart always testing_restarts
3. unless-stopped :- The unless-stopped restart policy behaves the same as always with one exception. When a container is stopped and the server is rebooted or the Docker service is restarted, the container will not be restarted.
One important item with unless-stopped is that if the container was running before the reboot, the container would be restarted once the system restarted. We can see this in action by restarting our container and rebooting the system again.
# sudo docker run -d --name testing_restarts --restart unless-stopped testing_restarts
4. on-failure :- Restart only if the container exits with a failure (non-zero exit status). To avoid restarting it indefinitely (in case of some problem), one can limit the number of restart retries the Docker daemon attempts.
The benefit of on-failures is that when an application exits with a successful exit code, the container will not be restarted.
How to check container restart :-
"RestartCount": 4,
#docker inspect -f "{{ .RestartCount }}" <container_name>
# docker inspect -f "{{ .State.StartedAt }}" <container_name>
# sudo docker run -d --name testing_restarts --restart always testing_restarts
# sudo docker ps
################# on-failure ###########
#!/bin/bash
sleep 30
exit 1
# vi Dockerfile
FROM ubuntu:14.04
ADD crash.sh /
CMD /bin/bash /crash.sh
# sudo docker build -t testing_restarts ./
# sudo docker run -d --name testing_restarts --restart on-failure:5 testing_restarts
# sudo docker ps -a
############## unless-stopped #############
sudo docker run -d --name testing_restarts --restart unless-stopped testing_restarts
With the container running, let’s stop it and reboot the system again.
# sudo docker stop testing_restarts
testing_restarts
# sudo reboot
This time when the system restarts, we should see the container is in a stopped state.
#sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fec5be52b955 testing_restarts "/bin/sh -c '/bin/bas" 2 minutes ago Exited (137) About a minute ago
One important item with unless-stopped is that if the container was running before the reboot, the container would be restarted once the system restarted. We can see this in action by restarting our container and rebooting the system again.
#sudo docker start testing_restarts
testing_restarts
#sudo reboot
After this reboot, the container should be running.
#sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fec5be52b955 testing_restarts "/bin/sh -c '/bin/bas" 5 minutes ago Up 13 seconds testing_restarts
23. CPU and mem set for container.
# docker run -it --cpuset-cpus="1,3" ubuntu:14.04 /bin/bash
# docker run -it --cpuset-mems="1,3" ubuntu:14.04
/bin/bash
24. extra commands.
docker inspect -f "{{ .RestartCount }}"
my-container
docker inspect -f "{{ .State.StartedAt }}"
my-container
sudo docker inspect -f "{{ .NetworkSettings.IPAddress
}}" Container_Name
NOTE: Stop the container and docker engine before editing the below files.
# cd
/var/lib/docker/containers/<container_ID>
#
docker container stop <container_ID>
# vim
hostconfig.json
[{"HostIp":"","HostPort":"9091"}]
#systemctl
restart docker.service
#
docker container start <container_ID>
#
docker container port <container_ID>
No comments:
Post a Comment