Docker Commands - Part 2

Docker Commands - Part 2

·

9 min read

👉 docker logs [container]: The docker logs command is used to view the logs of a specific container.

docker logs [OPTIONS] CONTAINER
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the docker logs command. Common options include -f to follow the logs in real-time (similar to tail -f), --tail to specify the number of lines to show from the end of the logs, and --since to show logs since a specific timestamp.

  • CONTAINER: This is the name or ID of the container whose logs you want to view.

For example, to view the logs of a container named my_container, you can use:

docker logs my_container

If you want to follow the logs in real-time, you can use the -f option:

docker logs -f my_container

This will continuously output new log messages as they are written to the container’s log file.

If you want to limit the number of lines shown, you can use the --tail option followed by the number of lines you want to display:

docker logs --tail 100 my_container

This will show only the last 100 lines of the container’s logs.

👉 docker login: The docker login command is used to log in to a Docker registry, such as Docker Hub or a private registry.

docker login [OPTIONS] [SERVER]
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the docker login command. Common options include -u to specify a username, -p to specify a password, --password-stdin to read the password from stdin, and --username-stdin to read the username from stdin.

  • [SERVER]: This is the URL of the Docker registry you want to log in to. If you omit this, Docker will default to Docker Hub.

When you run the docker login command without any options, Docker will prompt you to enter your username and password for the registry. For example:

docker login

You will be prompted to enter your Docker Hub username and password. Once you have entered your credentials, Docker will authenticate you with the registry, and you will be logged in.

Alternatively, you can provide the username and password as options:

docker login -u USERNAME -p PASSWORD

Replace USERNAME and PASSWORD with your Docker Hub username and password, respectively.

It’s worth noting that providing the password directly on the command line can be insecure, especially in shared environments, as it may be visible in the shell history. Instead, you can use --password-stdin to read the password from stdin:

echo "PASSWORD" | docker login -u USERNAME --password-stdin

Replace PASSWORD with your Docker Hub password and USERNAME with your Docker Hub username.

Once you have successfully logged in, Docker will save your credentials in the Docker configuration file (~/.docker/config.json) for future use.

👉 docker push: The docker push command is used to push Docker images to a registry, such as Docker Hub or a private registry.

docker push [OPTIONS] NAME[:TAG]
  • [OPTIONS]: This is where you can specify various options to customize the behavior of the docker push command. Common options include --all-tags to push all tags of the specified image, --disable-content-trust to disable content trust verification, and -f to force pushing the image, even if it already exists in the registry.

  • NAME[:TAG]: This is the name and tag of the Docker image you want to push to the registry.

For example, to push an image named my_image with the latest tag to Docker Hub, you would use:

docker push my_image:latest

If you want to push an image with a specific tag, you would replace latest with the desired tag:

docker push my_image:tag_name

This command will push the specified image to the default registry, which is usually Docker Hub. If you want to push to a different registry, you would specify the full registry URL along with the image name and tag.

👉 docker logout: The docker logout command is used to log out from a Docker registry, such as Docker Hub or a private registry.

docker logout

When you run the docker logout command without specifying a server, Docker will log you out from the default Docker registry (usually Docker Hub).

If you want to log out from a specific registry, you can specify its URL as follows:

docker logout myregistry.example.com

Replace myregistry.example.com with the URL of the registry from which you want to log out.

Once you’ve successfully logged out, Docker will remove your credentials from the Docker configuration file (~/.docker/config.json), and you will need to log in again the next time you want to push or pull images from that registry.

👉 docker run [options] [image] [command]: docker run command is used to create and start a new container based on a Docker image.

If you want to create a detached, interactive terminal session inside a new Ubuntu container. You can use the following command.

docker run -dit ubuntu /bin/bash

Here's what each part of the command does:

  • docker run: This is the command used to run a new container.

  • -dit: These are options passed to docker run:

  • -d: Detached mode. It runs the container in the background.

  • -i: Interactive mode. It keeps STDIN open even if not attached, allowing you to interact with the container.

  • -t: Allocates a pseudo-TTY. This option ensures that Docker allocates a terminal for the container, enabling an interactive shell.

  • ubuntu: This is the name of the Docker image used to create the container. In this case, it's the official Ubuntu image.

  • /bin/bash: This is the command to run inside the container. It starts a Bash shell session, providing you with an interactive terminal.

When you run this command, Docker will create a new container based on the Ubuntu image, start a Bash shell inside it, and put it in detached mode. You won’t see any output immediately because it’s running in detached mode, but you can interact with the container using docker exec or docker attach commands.

For example, to interact with the container’s Bash shell, you can use docker exec:

docker exec -it [container_id] bash

Replace [container_id] with the ID of the container you just created. This will give you a shell prompt within the container, allowing you to run commands and interact with it.

👉 docker attach: The docker attach command is used to attach to a running container and interact with its standard input, output, and error streams. It allows you to connect to the main process of a container and interact with it in real-time.

docker attach [OPTIONS] CONTAINER
  • [OPTIONS]: Common options include -i for interactive mode (keeping STDIN open), -t for allocating a pseudo-TTY, and --detach-keys to specify the escape keys sequence.

  • CONTAINER: This is the name or ID of the running container you want to attach to.

For example, to attach to a running container named my_container, you would use:

docker attach my_container

This command will attach your current terminal session to the specified container, allowing you to interact with its main process. Press Ctrl + P, Ctrl + Q to detach from the container without stopping it.

It’s important to note that docker attach attaches to the main process of the container. If the main process is a long-running command or a service with its own terminal interface (like a web server), attaching to it may not provide a usable interactive session. In such cases, you may need to use docker exec to run commands within the container or start a new interactive shell session using docker exec -it.

For example, to start an interactive Bash shell session within a running container named my_container, you would use:

docker exec -it my_container /bin/bash

This command will launch a new interactive Bash session within the my_container container, allowing you to run commands interactively as if you were working directly within the container's environment.

👉 docker inspect: docker inspect command is used to obtain detailed information about Docker objects such as containers, images, volumes, networks, and more.

docker inspect [OPTIONS] OBJECT [OBJECT...]
  • [OPTIONS]: Common options include --format to specify a Go template to format the output, and --type to specify the type of object to inspect (e.g., container, image, volume, network, etc.).

  • OBJECT [OBJECT...]: This is the name or ID of the Docker object(s) you want to inspect.

For example, to inspect a container named my_container, you would use:

docker inspect my_container

This command will output detailed JSON-formatted information about the specified container.

If you want to inspect multiple objects, you can specify their names or IDs separated by spaces:

docker inspect container1 container2

This will output detailed information about both container1 and container2.

You can also use the --format option to customize the output format. For example, to display only the container's name and its state, you can use:

docker inspect --format='{{.Name}} {{.State}}' my_container

This will output something like:

/my_container running

The docker inspect command is useful for obtaining detailed information about Docker objects, which can be helpful for debugging, troubleshooting, and automation tasks.

👉 docker tag : docker tag command is used to create a new tag for an existing Docker image. This is commonly used to apply meaningful tags to images before pushing them to a registry. Here's how you can use it:

docker tag [SOURCE_IMAGE] [TARGET_IMAGE]
  • [SOURCE_IMAGE]: This is the name of the existing Docker image that you want to tag.

  • [TARGET_IMAGE]: This is the new name and tag you want to apply to the image.

For example, let’s say you have an image named my_image and you want to create a new tag v1.0 for it:

docker tag my_image my_registry/my_image:v1.0

This command creates a new tag v1.0 for the my_image image. The my_registry/ prefix is an example of a registry namespace where the tagged image will be stored. You can replace my_registry/ with your own Docker Hub username, or the URL of your private registry, as needed.

For example, If you have an existing Docker image named my_image with the tag latest, and you want to create a new tag v1.0 for it. You can use the docker tag command like this:

docker tag my_image:latest my_image:v1.0
  • SOURCE_IMAGE[:TAG]: This is the name and optional tag of the existing Docker image you want to tag. If you don't specify a tag, Docker will default to latest.

  • TARGET_IMAGE[:TAG]: This is the new name and optional tag you want to assign to the image.

This command creates a new tag v1.0 for the my_image image.

If you want to tag an image with a different name entirely, you can do so:

docker tag my_image:latest my_repository/my_image:v1.0

This would create a new image with the tag v1.0, but under a different repository or namespace (my_repository).

After tagging the image, you can push it to a registry (like Docker Hub) using the docker push command, if you want to make it accessible to others:

docker push my_repository/my_image:v1.0

This will push the tagged image to the specified repository, making it available for others to pull and use.

Tagging images allows you to organize and manage your images more effectively, especially when working with multiple versions or distributing images across different environments.

Did you find this article valuable?

Support Megha Sharma by becoming a sponsor. Any amount is appreciated!

Â