Saturday, August 24, 2024

Docker - Quick Notes


 

How to check the IP address of running container

#docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $INSTANCE_ID

#docker load -i name.tar [ Load a docker image from tar file]

#docker prune --force [reclaim a unused a docker containers]

* Docker provide a restart policy option to let you container restart automatically incase of certain failures.

* Docker restart policy are applied on per container level. We can define through yaml file [K8S] or through command line while provision a docker container.

#docker container run --restart [policy]

[Policy] - No, Always, unless-stopped, on-failures

#docker inspect dockername | grep -i restartcount [Restarted count]

Always policy will start automatically if a docker dameon has been restarted, but unless-stopped will not start automatically.

on-failures restart policy will restart the container it was exited with non zero code.

#docker build -t filename .

#docker run --rm -p [remove the container after exit]

𝐇𝐞𝐫𝐞 𝐚𝐫𝐞 𝐬𝐨𝐦𝐞 𝐜𝐨𝐦𝐦𝐨𝐧 𝐞𝐫𝐫𝐨𝐫𝐬 𝐚𝐧𝐝 𝐢𝐬𝐬𝐮𝐞𝐬 𝐰𝐞 𝐨𝐟𝐭𝐞𝐧 𝐞𝐧𝐜𝐨𝐮𝐧𝐭𝐞𝐫 𝐰𝐡𝐞𝐧 𝐰𝐨𝐫𝐤𝐢𝐧𝐠 𝐰𝐢𝐭𝐡 𝐃𝐨𝐜𝐤𝐞𝐫:

1.𝐈𝐦𝐚𝐠𝐞 𝐍𝐨𝐭 𝐅𝐨𝐮𝐧𝐝: You might encounter this error when trying to run a container with an image that doesn't exist locally or on the specified Docker registry. Ensure that you've pulled the image or that the image name and tag are correct.

2.𝐏𝐞𝐫𝐦𝐢𝐬𝐬𝐢𝐨𝐧 𝐃𝐞𝐧𝐢𝐞𝐝: Docker containers often run as non-root users for security reasons. If your application requires access to certain resources or directories, make sure you've set the appropriate file permissions and user/group settings within the container.

3.𝐏𝐨𝐫𝐭 𝐂𝐨𝐧𝐟𝐥𝐢𝐜𝐭𝐬: Trying to run multiple containers that use the same host port can lead to port conflicts. Ensure that the ports you specify in the docker run command do not conflict with existing ports in use on your host system.

4.𝐎𝐮𝐭 𝐨𝐟 𝐃𝐢𝐬𝐤 𝐒𝐩𝐚𝐜𝐞: Docker uses disk space for images, containers, and logs. Over time, this can consume a significant amount of disk space. Periodically clean up unused images and containers using the docker system prune command.

5.𝐂𝐨𝐧𝐭𝐚𝐢𝐧𝐞𝐫 𝐂𝐫𝐚𝐬𝐡: Containers may crash for various reasons, such as misconfiguration or application issues. Use docker logs <container_name> to check the container logs for error messages that can help diagnose the problem.

6.𝐃𝐨𝐜𝐤𝐞𝐫 𝐁𝐮𝐢𝐥𝐝 𝐅𝐚𝐢𝐥𝐮𝐫𝐞𝐬: Issues may arise during the build process of a Docker image. Common problems include incorrect Dockerfile syntax, missing files or dependencies, and network problems while downloading packages during the build.

7.𝐍𝐞𝐭𝐰𝐨𝐫𝐤𝐢𝐧𝐠 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬: Docker containers can have connectivity problems if not properly configured. Ensure that the container is attached to the correct network, that firewalls are not blocking required ports, and that DNS settings are correct.

8.𝐕𝐨𝐥𝐮𝐦𝐞 𝐌𝐨𝐮𝐧𝐭 𝐄𝐫𝐫𝐨𝐫𝐬: Failing to properly mount volumes can result in data loss or incorrect behavior. Double-check the paths and permissions when using the -v flag in docker run or docker-compose.

9.𝐑𝐞𝐬𝐨𝐮𝐫𝐜𝐞 𝐂𝐨𝐧𝐬𝐭𝐫𝐚𝐢𝐧𝐭𝐬: Docker containers can consume significant CPU and memory resources. Make sure you allocate appropriate resources using the --cpu and --memory flags when running containers.

10.𝐈𝐦𝐚𝐠𝐞 𝐕𝐮𝐥𝐧𝐞𝐫𝐚𝐛𝐢𝐥𝐢𝐭𝐢𝐞𝐬: Using outdated or insecure base images can introduce security vulnerabilities. Regularly update your Docker images and use tools like Clair or Trivy to scan for vulnerabilities.

11.𝐑𝐞𝐬𝐨𝐮𝐫𝐜𝐞 𝐋𝐞𝐚𝐤𝐬: Containers should clean up after themselves when they exit. Be cautious about resource leaks, such as leaving behind orphaned processes or unreleased file handles.

#docker run -d --name test --publish 8080:8080 dockerimage

 -d will specified to run in background

 * Docker is ability to reduce the resources by application using a cgroup technology by linux.

Declare the memory and swap:

[root@thiru nodejs]# docker run -d --name test --publish 8080:8080 --memory 200m --memory-swap 200m simple-node

0a6fb440d5f2d6738d342f195d27127a32ff7aaf21bc58705f1bf4e08a451a53

--cpu-shares : restrict the CPU for particular CPU.

No comments:

Post a Comment