In continuation of Day-4 we will be working on basic operations on Docker Images and Containers as below.
Stopping and Starting Containers
* To see all containers
PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 13 hours ago Up 31 minutes 0.0.0.0:3000->80/tcp exciting_nash PS D:\Docker>
*To stop Container
PS D:\Docker> docker stop 835b3ea24ee6 835b3ea24ee6 PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 13 hours ago Exited (137) 3 seconds ago exciting_nash PS D:\Docker>
*To see only running Container
PS D:\Docker> docker ps 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 13 hours ago Up 31 minutes 0.0.0.0:3000->80/tcp exciting_nash
*To start a stopped container
PS D:\Docker> docker start 835b3ea24ee6 835b3ea24ee6 PS D:\Docker> PS D:\Docker> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 13 hours ago Up 3 seconds 0.0.0.0:3000->80/tcp exciting_nash PS D:\Docker>
Attaching and Detaching Containers
Attaching and detaching containers are common tasks when working with Docker. It's basically facilitate to work in Foreground and Background manner with Containers. Here are some useful information and commands to help you:
To attach to a running container, you can use the docker attach command with the container name or id. This will connect your terminal to the container's standard input, output and error streams. For example: docker attach test_redis.
* To attach a Running Container, we execute attach command with container.
PS D:\Docker> docker attach 835b3ea24ee6
When we start a container using Docker start Container_name/Container_ID it's by default starts in detach mode.
PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 7 days ago Exited (0) 27 minutes ago 0.0.0.0:3000->80/tcp exciting_nash PS D:\Docker> PS D:\Docker> docker start 835b3ea24ee6 835b3ea24ee6 PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835b3ea24ee6 37ec243acddd "docker-entrypoint.s…" 7 days ago Up 10 seconds 0.0.0.0:3000->80/tcp exciting_nash
To run a container in attached or detached mode, you can use the docker run command with different options. The default mode is attached, which means the container runs in the foreground and connects to your terminal's streams. You can use the -d flag to run the container in detached mode, which means it runs in the background and prints its id. You can use the -it flag to run the container in interactive mode, which means it allocates a pseudo-terminal and allows you to interact with the container's shell.
PS D:\Docker> docker run -p 3000:80 -d 37ec243acddd
80f0ef5bfb03f6b3cfc41ae3cc8c163a3062c9c8ae9e035891eeea6b06deca80
PS D:\Docker>
PS D:\Docker> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80f0ef5bfb03 37ec243acddd "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 0.0.0.0:3000->80/tcp vigorous_pascal
PS D:\Docker> Printing Logs from Container
The docker logs command shows information logged by a running container You can use different options to customize the output, such as:
--follow or -f to follow the log output continuously
--since to show logs since a given timestamp or relative duration
--tail or -n to show a specific number of lines from the end of the logs
--timestamps or -t to show timestamps for each log entry
--until to show logs before a given timestamp or relative duration
--details to show extra details provided to logs
To use the docker logs command, you need to specify the container name or id as an argument.
For example: docker logs test_redis
PS D:\Docker> docker logs 80f0ef5bfb03
PS D:\Docker>
PS D:\Docker> docker logs -f 80f0ef5bfb03
Butter
Bread
For example: docker run -it ubuntu bash
Number Addition project in Python language for Interactive Mode example
To start with interactive mode example, we will be creating another project which is written in python language and it will just add two numbers which is supplied by user as below.
Dockerfile
FROM pythonWORKDIR /appCOPY . /appCMD ["python", "add_num.py"] add_num.py
first_number = int(input('First number: '))second_number = int(input('Second number: '))
final_result = first_number + second_numberprint(final_result)Files in VCS as below.
=> Let we Build image for this Python project as below.
PS D:\Docker> docker build . => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/python:latest 3.8s => CACHED [2/3] WORKDIR /app 0.0s => => exporting layers 0.1s => => writing image sha256:6b9f3fc72c8597618e36ffee1eaac5a694c9576b18e23e39dcfce5cbabbf9ec6 0.0s
=> Let we start a Container from this Image as below. But we will get error eventually as we did not define interactive mode.
PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 6b9f3fc72c85 5 seconds ago 1.01GB PS D:\Docker> PS D:\Docker> docker run 6b9f3fc72c85 File "/app/add_num.py", line 1, in <module> ^^^^^^^^^^^^^^^^^^^^^^^ PS D:\Docker>
=> Now we will run the container in Interactive Mode as below using -it option.
PS D:\Docker> docker run -it 6b9f3fc72c85 First number: 10 Second number: 20 30
=> If we check now Container is in stopped state and exited as below.
PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b33867791988 6b9f3fc72c85 "python add_num.py" 9 seconds ago Exited (1) 8 seconds ago keen_pascal
=> What if we need to restart that container and excute the same process again , we would not be able to do so as we will get error, so to start container in attached mode with interactive mode we will execute below command.
PS D:\Docker> docker start -a -i b33867791988 First number: 10 Second number: 20 30 PS D:\Docker>
Removing Containers.
There are different ways to remove Docker containers depending on your needs. Here are some useful commands and examples:
• To remove one or more specific containers, use the docker container rm command with the container name or id. You can use the docker container ls -a command to locate the containers you want to remove.
PS D:\Docker> docker ps -a 729322edf7bb 7ce7e25dab6a "python add_num.py" 7 seconds ago Up 6 seconds sweet_ritchie f165b8cb01c7 7ce7e25dab6a "python add_num.py" 32 seconds ago Exited (1) 31 seconds ago heuristic_hertz 7dac6f76b80d 7ce7e25dab6a "python add_num.py" 2 minutes ago Exited (0) 2 minutes ago inspiring_zhukovsky 36f1e9b9e26e 6b9f3fc72c85 "python add_num.py" 5 minutes ago Exited (0) 5 minutes ago xenodochial_feistel 0459de547b29 6b9f3fc72c85 "python add_num.py" 5 minutes ago Exited (1) 5 minutes ago practical_tesla 8c3d9c1b21e3 6b9f3fc72c85 "python add_num.py" 5 minutes ago Exited (0) 5 minutes ago beautiful_northcutt 1923474d5c1f 6b9f3fc72c85 "python add_num.py" 13 minutes ago Exited (0) 11 minutes ago eager_cartwright PS D:\Docker> PS D:\Docker> docker rm f165b8cb01c7 f165b8cb01c7 PS D:\Docker> docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 729322edf7bb 7ce7e25dab6a "python add_num.py" 4 minutes ago Up 4 minutes sweet_ritchie 7dac6f76b80d 7ce7e25dab6a "python add_num.py" 6 minutes ago Exited (0) 6 minutes ago inspiring_zhukovsky e30e80efe03b 6b9f3fc72c85 "python add_num.py" 9 minutes ago Exited (0) 9 minutes ago agitated_curie 36f1e9b9e26e 6b9f3fc72c85 "python add_num.py" 9 minutes ago Exited (0) 9 minutes ago xenodochial_feistel 0459de547b29 6b9f3fc72c85 "python add_num.py" 9 minutes ago Exited (1) 9 minutes ago practical_tesla 8c3d9c1b21e3 6b9f3fc72c85 "python add_num.py" 9 minutes ago Exited (0) 9 minutes ago beautiful_northcutt 1923474d5c1f 6b9f3fc72c85 "python add_num.py" 17 minutes ago Exited (0) 15 minutes ago eager_cartwright PS D:\Docker>
• To remove a container that is running, you need to stop it first or use the -f flag to force remove it. However, forcing a running container to stop is not recommended as it can cause data loss or corruption
PS D:\Docker> docker ps -a 729322edf7bb 7ce7e25dab6a "python add_num.py" 8 minutes ago Up 8 minutes swee7dac6f76b80d 7ce7e25dab6a "python add_num.py" 11 minutes ago Exited (0) 11 minutes ago inspiring_zhukovsky e30e80efe03b 6b9f3fc72c85 "python add_num.py" 13 minutes ago Exited (0) 13 minutes ago agitated_curie 36f1e9b9e26e 6b9f3fc72c85 "python add_num.py" 13 minutes ago Exited (0) 13 minutes ago xenodochial_feistel 0459de547b29 6b9f3fc72c85 "python add_num.py" 13 minutes ago Exited (1) 13 minutes ago practical_tesla 8c3d9c1b21e3 6b9f3fc72c85 "python add_num.py" 14 minutes ago Exited (0) 14 minutes ago beautiful_northcutt 1923474d5c1f 6b9f3fc72c85 "python add_num.py" 21 minutes ago Exited (0) 20 minutes ago eager_cartwright PS D:\Docker> PS D:\Docker> docker rm 729322edf7bb Error response from daemon: You cannot remove a running container 729322edf7bb7f0b40479d4d51dac39d53de649d3e128ed089a28bf12d4bc047. Stop the container before attempting removal or force remove PS D:\Docker> PS D:\Docker> docker stop 729322edf7bb 729322edf7bb PS D:\Docker> docker rm 729322edf7bb 729322edf7bb PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7dac6f76b80d 7ce7e25dab6a "python add_num.py" 11 minutes ago Exited (0) 11 minutes ago inspiring_zhukovsky e30e80efe03b 6b9f3fc72c85 "python add_num.py" 14 minutes ago Exited (0) 14 minutes ago agitated_curie 36f1e9b9e26e 6b9f3fc72c85 "python add_num.py" 14 minutes ago Exited (0) 14 minutes ago xenodochial_feistel 0459de547b29 6b9f3fc72c85 "python add_num.py" 14 minutes ago Exited (1) 14 minutes ago practical_tesla 8c3d9c1b21e3 6b9f3fc72c85 "python add_num.py" 14 minutes ago Exited (0) 14 minutes ago beautiful_northcutt 1923474d5c1f 6b9f3fc72c85 "python add_num.py" 21 minutes ago Exited (0) 20 minutes ago eager_cartwright PS D:\Docker>
• To remove all stopped containers, you can use the docker container prune command. This will delete any containers that have a status of exited.
PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7dac6f76b80d 7ce7e25dab6a "python add_num.py" 13 minutes ago Exited (0) 13 minutes ago inspiring_zhukovsky e30e80efe03b 6b9f3fc72c85 "python add_num.py" 16 minutes ago Exited (0) 16 minutes ago agitated_curie 36f1e9b9e26e 6b9f3fc72c85 "python add_num.py" 16 minutes ago Exited (0) 16 minutes ago xenodochial_feistel 0459de547b29 6b9f3fc72c85 "python add_num.py" 16 minutes ago Exited (1) 16 minutes ago eager_cartwright PS D:\Docker> PS D:\Docker> docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 7dac6f76b80d218e07426019540a64f1f150e7f0e60302d68f16a7d778817df4 e30e80efe03b2aa611c4a866760ac9e133ba873f0bef2ed17713cb378a49c6f1 36f1e9b9e26ea18fed2355ed4237ccef495b2df5977a26b8fd09ae209bdb2ac8 0459de547b291e752f65b90de9094a95ee064162a2ccbfcc2e4e377fa196dd7d 8c3d9c1b21e3236db1bee36dfaced092decf7fd0ab8aaa9b19995a1ebde041ef 1923474d5c1f511ea4d76947b0e5cb7fc166593cd034a1c73cef7de3591768c5 Total reclaimed space: 195.5kB PS D:\Docker> PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES PS D:\Docker>
Removing stopped containers automatically is a useful feature to avoid cluttering your system with unused containers. There are different ways to achieve this depending on your Docker version and preferences.
• To remove a container automatically when it exits, you can use the --rm flag when running the container. This will delete the container and its associated volumes after the container stops. Example as below.
PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 7ce7e25dab6a 16 hours ago 1.01GB PS D:\Docker> PS D:\Docker> docker run -it --rm 7ce7e25dab6a First number: 10 Second number: 90 Final Added Values 100 PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES PS D:\Docker> PS D:\Docker> docker run -it 7ce7e25dab6a First number: 90 Second number: 90 Final Added Values 180 PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5b3136e9adaa 7ce7e25dab6a "python add_num.py" 7 seconds ago Exited (0) 2 seconds ago zen_hellman PS D:\Docker>
Removing Docker Images
Removing Docker images is another common task when you want to free up disk space or keep your system organized. There are different ways to remove Docker images depending on your needs. Here are some useful commands and examples:
• To remove one or more specific images, use the docker image rm or docker rmi command with the image name or id. You can use the docker image ls -a or docker images -a command to locate the images you want to remove. For example: docker image rm ubuntu.
PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 7ce7e25dab6a 25 minutes ago 1.01GB <none> <none> 6b9f3fc72c85 55 minutes ago 1.01GB PS D:\Docker> PS D:\Docker> docker image rm 7ce7e25dab6a Deleted: sha256:7ce7e25dab6a71c5311a0faa90c0ecde6bc1bf29066c7ff3e31aaa31632d4b85 PS D:\Docker> PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 6b9f3fc72c85 55 minutes ago 1.01GB PS D:\Docker>
• To remove dangling images, which are untagged images that are not used by any container, you can use the docker image prune command .This will delete any images that have a value of <none> in the repository or tag column.
PS D:\Docker> docker image prune WARNING! This will remove all dangling images. Are you sure you want to continue? [y/N] y Deleted Images: deleted: sha256:6b9f3fc72c8597618e36ffee1eaac5a694c9576b18e23e39dcfce5cbabbf9ec6 Total reclaimed space: 0B PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE PS D:\Docker>
• To remove all unused images, which are images that are not associated with any container, you can use the docker image prune -a command. This will delete any images that are not referenced by any container, including the dangling ones
PS D:\Docker> docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Total reclaimed space: 0B PS D:\Docker>
Copying file to and from Container
There are different ways to copy files to and from Docker containers. One of the most common methods is to use the docker cp command, which has the following syntax:
docker cp <SRC> <DEST>
where <SRC> and <DEST> can be either a file or directory on the host machine or inside the container. For example, to copy a file from the host machine to a container named my-container, you can use:
docker cp /tmp/file.txt my-container:/path/to/copy/file.txt
To copy a file from the container to the host machine, you can use:
docker cp my-container:/path/to/file.txt /tmp/file.txt
PS D:\Docker> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fab13a64d16d 7ce7e25dab6a "python add_num.py" 3 seconds ago Up 2 seconds epic_mahavira
PS D:\Docker>
PS D:\Docker> docker cp .\on_locahost epic_mahavira:/app/
Successfully copied 2.05kB to epic_mahavira:/app/
PS D:\Docker>You can also use container IDs instead of names. For more examples and details, you can check out this tutorial.
Another method is to use volume mounts, which means you make a directory from the host system available inside the container. To use volume mounts, you have to run your container with the -v flag:
docker run -d --name=my-container -p 3000:3000 -v /tmp:/transfer image-name
This command runs a container named my-container and mounts the /tmp directory from the host machine as a new directory inside the container named /transfer. You can then use the Unix cp command to copy files between these directories.
Naming and Tagging methods for Containers and Images
As we saw yet, when we create a container, it's gotten a default name as like below.But in practice this is not a good an option.
PS D:\Docker> docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 729322edf7bb 7ce7e25dab6a "python add_num.py" 4 minutes ago Up 4 minutes sweet_ritchie 7dac6f76b80d 7ce7e25dab6a "python add_num.py" 6 minutes ago Exited (0) 6 minutes ago inspiring_zhukovsky PS D:\Docker>
• Naming containers: You can assign a name to a container when you run it using the --name flag.
For example, docker run --name my-container image-name.
This allows you to refer to the container by its name instead of its ID.
You can also rename an existing container using the docker rename command.
For example, docker rename my-container new-name.
PS D:\Docker> docker run -it --name "Python_named_container" 7ce7e25dab6a First number: 10 Second number: 20 Final Added Values 30 PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 314247cf7f89 7ce7e25dab6a "python add_num.py" 8 seconds ago Exited (0) 3 seconds ago Python_named_container fab13a64d16d 7ce7e25dab6a "python add_num.py" 4 hours ago Exited (0) 4 hours ago epic_mahavira 5b3136e9adaa 7ce7e25dab6a "python add_num.py" 5 hours ago Exited (0) 5 hours ago zen_hellman PS D:\Docker>
• Tagging Docker images: Tagging Docker images is a way to assign a human-readable name and version to an image. This helps you to identify, organize, and deploy your images more easily. You can tag an image when you build it or after it is built.
By default, no tag got assigned to images as below.
PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 7e00745701d3 3 seconds ago 1.01GB <none> <none> 7ce7e25dab6a 20 hours ago 1.01GB PS D:\Docker> PS D:\Docker>
Here are some steps and examples of how to tag Docker images:
• Tagging an image during the build process: You can use the --tag or -t flag with the docker build command to specify a name and optionally a tag for your image. For example, docker build -t my-image:1.0 . will build an image from the current directory and tag it as my-image:1.0. If you omit the tag part, Docker will use latest by default. You can also use multiple -t flags to assign multiple tags to the same image.
PS D:\Docker> docker build -t python_new_image:1.0 . [+] Building 1.5s (8/8) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 107B 0.0s => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/python:latest 1.2s => [1/3] FROM docker.io/library/python@sha256:d73088ce13d5a1eec1dd05b47736041ae6921d08d2f240035d99642db 0.1s => => resolve docker.io/library/python@sha256:d73088ce13d5a1eec1dd05b47736041ae6921d08d2f240035d99642db 0.1s => [internal] load build context 0.1s => => transferring context: 91B 0.0s => CACHED [2/3] WORKDIR /app 0.0s => CACHED [3/3] COPY . /app 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:7e00745701d3f4558abecbeabe80a7dbebe6f804173efe4d5ba97a5674b206f5 0.0s => => naming to docker.io/library/python_new_image:1.0 0.0s PS D:\Docker> PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE python_new_image 1.0 7e00745701d3 2 minutes ago 1.01GB <none> <none> 7ce7e25dab6a 20 hours ago 1.01GB PS D:\Docker>
• Tagging an existing image: You can use the docker tag command to tag an existing image in your host system. You need to specify the source image name and optionally a tag, and the target image name and optionally a tag. For example, docker tag my-image:1.0 new-image:2.0 will create a new tag for the existing image my-image:1.0 as new-image:2.0. You can also use image IDs instead of names. For example, docker tag 0e5574283393 my-image:1.0 will tag the image with ID 0e5574283393 as my-image:1.0.
PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE python_new_image 1.0 7e00745701d3 2 minutes ago 1.01GB <none> <none> 7ce7e25dab6a 20 hours ago 1.01GB PS D:\Docker> PS D:\Docker> docker tag 7ce7e25dab6a old_python_image:1.0 PS D:\Docker> docker images REPOSITORY TAG IMAGE ID CREATED SIZE python_new_image 1.0 7e00745701d3 4 minutes ago 1.01GB old_python_image 1.0 7ce7e25dab6a 20 hours ago 1.01GB PS D:\Docker>
We can further combine Named container and tagged images to create new container as below.
PS D:\Docker> PS D:\Docker> docker run -it --name "Python_container" python_new_image:1.0 First number: 10 Second number: 20 Final Added Values as 30 PS D:\Docker> PS D:\Docker> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3022b713f6d3 python_new_image:1.0 "python add_num.py" 7 seconds ago Exited (0) 4 seconds ago Python_container 314247cf7f89 7ce7e25dab6a "python add_num.py" 13 minutes ago Exited (0) 13 minutes ago Python_named_container fab13a64d16d 7ce7e25dab6a "python add_num.py" 4 hours ago Exited (0) 4 hours ago epic_mahavira 5b3136e9adaa 7ce7e25dab6a "python add_num.py" 5 hours ago Exited (0) 5 hours ago zen_hellman PS D:\Docker>
No comments:
Post a Comment