- docker cp sample.txt container_id:/path/in/container: docker cp command is used to copy the files/folders from local file system to running container.
- docker cp container_id:/path/in/container sample.txt: This will copy files/folder from the container to your local FS.
- docker diff container_id: Changes done to any folders or files in the container after running it will be captured by this command.
- docker port container_id: This command tells us info about the port mapping of the container with host machine. For example, I have port forwarded an elasticsearch container port 9200 to 9191 port in my local.
docker run -itd -p 9191:9200 elasticsearch:6.5.4 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 63a7dbbe30bd elasticsearch:6.5.4 "/usr/local/bin/dock…" 2 seconds ago Up 1 second 9300/tcp, 0.0.0.0:9191->9200/tcp focused_williamson ========================================================== docker port 63a7dbbe30bd 9200/tcp -> 0.0.0.0:9191
Port forwarding helps us in accessing the services/application running inside the container from outside. port forwarding makes the process running in container on a specific port to be available on host machine port.
Make sure that the host machine port that we are forwarding to should not be already in use. Or else we will face errors.
- docker rename container_id any_name_of_choice: This will rename the container
To inspect the logs of a container.
docker logs container_id:
This helps to analyse what is happening with the process running in the container.
docker top container_id:
Gives the info on what is the host PID of the running container.
Gives all the events/logging happening for the docker daemon
docker update is used to update configs of containers. We can set cpu resources,restart policy, memory limitations etc.
Example: Add a restart policy to a container that was already created
docker update --restart=always container_id
This command is used to wait or block until the container stops and then it outputs their exit codes, which means we cannot use our terminal if we are running this command on terminal
Example: I created a container named test and started wait command on it. This blocks the terminal where the command is running and when in other terminal I killed the container the output is 137 exit code which is expected in case of kill. It will be 0 if graceful shut down.
docker wait test
This command is used when we have certain tasks to be completed before starting another container. So we wait on the first container to be completed and then start with the others.
Docker image is a collection of filesystes(Root FS(which also includes the user FS) + Application FS).
When creating a container all layers if image are merged together and are mounted to the container.
/var/lib/docker(this can be found in docker info command) is the path to get the info of the file system layers associated with image.
- Inorder to save the changes done(Like adding new files/folders, changing existing ones) to a container in the form of an image, we can use commit command docker commit -m "commit message" -a "author name" container_id new_name_of_the_image This command commits all the changes done to the container given in the command to a new image.
- docker history new_name_of_the_image: To check what are the changes done on top of existing image, we can use history command on the committed image.
To save the image as a tar,
docker save -o name.tar name_of_image
To load the image which is saved as tar,
docker load -i name.tar:
Check the images using docker images command to see if the tar is loaded.
To share an image with larger group of people then tar is not a feasible option, so we push the image to a docker registry from where everyone can access the image.
- docker login -> Login to the registry.(should have created an account prior)
- tag the docker image according to the repository created in your registry. The name of docker image is the repository and we will face errors while pushing if the repository we specified doesn't exist. docker tag my_image mounica/devopsschool -> This tags my_image which is in my local to mounica/devopsschool(repository name in registry)
- *docker push mounica/devopsschool * -> push to Registry
- We can validate the same using docker pull mounica/devopsschool -> this should pull your image from registry.