Shashikant shah

Thursday 10 September 2020

Docker container commands. Part-3

Docker container commands :- 

# container home :- /var/lib/docker/containers/<container_ID>

# docker container --help

showing total details for docker

# docker info

  1. 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)

 

  1. 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

  1. 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

  1. Particular IP add with container.

# docker container run --name web_tomcat  -d  -p 192.168.1.24:8080:8080 tomcat8:latest

  1. Start/ Stop /Restart command for Container.

# docker container stop <container_ID>

# docker container start <container_ID>

# docker container restart <container_ID>

  1. Check running container and history.

# docker container ls

# docker container ps

# docker container ls -a

  1. 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}

  1. show container details like IP, volume, port

# docker container inspect <container_ID>

# docker container port <container_ID>

  1. Login in container

# docker container exec -it <container ID> /bin/bash

# docker container attach <container ID>

  1. Check cpu load and memory for container running.

# docker container top <container ID>

# docker container stats

  1. diff command for show change create, delete , add file.

   # docker container diff <container name>

  1. 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

  1. Rename container name

# docker container rename <container ID> <new name>

  1. Copy data from localhost to container.

# docker container cp test/ <container ID>:/tmp/

  1. update command memory/cpu

docker container update -m "300M" --memory-swap -1 0306685bc0d7

  1. create container backup.

# docker container export <container ID> -o ubnutu_file.tar

# docker image import ubnutu_file.tar ubnutu_file

  1. 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 

 18. data copy from local machine to docker container.

# cd /var/data

# ls index.html

# docker cp <containerID>:/usr/share/nginx/html/index.html .

        OR

# docker cp index.html  <containerID>:/usr/share/nginx/html/index.html

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.

# sudo docker run -d --name testing_restarts --restart on-failure:5 testing_restarts

 
How to check container restart  :-

# docker inspect always-policy | grep -i restartcount
        "RestartCount": 4, 

how many times the container has restarted:-

#docker inspect -f "{{ .RestartCount }}" <container_name>
 

Last time the container restarted:-

# docker inspect -f "{{ .State.StartedAt }}" <container_name>


############## always #################
# sudo docker run -d --name testing_restarts --restart always testing_restarts
# sudo docker ps

################# on-failure ###########


1. we’ll create a Docker container that executes a simple bash script named crash.sh.

# vim crash.sh

#!/bin/bash
sleep 30
exit 1

2.Building and Running a Custom Container


# 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 #############

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.

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


25. Docker Change Port Mapping for an Existing Container .

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