Docker Commands
Docker Commands
1. General Docker Commands
-
docker --version
Check the installed Docker version. -
docker info
Display system-wide information about Docker.
2. Docker Image Commands
-
docker build -t <image_name> .
Build a Docker image from a Dockerfile in the current directory. -
docker pull <image_name>
Download an image from Docker Hub or another registry. -
docker push <image_name>
Push a local image to a Docker registry. -
docker imagesordocker image ls
List all available images on your local machine. -
docker rmi <image_name>
Remove an image from your local machine. -
docker tag <source_image> <target_image>
Tag an image with a new name. -
docker inspect <image_name>
Get detailed information about an image.
3. Docker Container Commands
-
docker run <image_name>
Create and start a container from an image. -
docker psordocker container ls
List all running containers. -
docker ps -aordocker container ls -a
List all containers, including stopped ones. -
docker exec -it <container_name> <command>
Execute a command inside a running container. -
docker start <container_name>
Start a stopped container. -
docker stop <container_name>
Stop a running container. -
docker restart <container_name>
Restart a container. -
docker pause <container_name>
Pause a running container. -
docker unpause <container_name>
Unpause a paused container. -
docker rm <container_name>
Remove a container (must be stopped). -
docker logs <container_name>
View logs of a running or stopped container. -
docker top <container_name>
Display running processes in a container. -
docker exec -it <container_name> bash
Start a bash shell inside a container.
4. Docker Volume Commands
-
docker volume ls
List all Docker volumes. -
docker volume create <volume_name>
Create a new Docker volume. -
docker volume rm <volume_name>
Remove a Docker volume. -
docker volume inspect <volume_name>
Get details about a specific volume.
5. Docker Network Commands
-
docker network ls
List all Docker networks. -
docker network create <network_name>
Create a new Docker network. -
docker network rm <network_name>
Remove a Docker network. -
docker network inspect <network_name>
Get details about a Docker network. -
docker network connect <network_name> <container_name>
Connect a container to a network. -
docker network disconnect <network_name> <container_name>
Disconnect a container from a network.
6. Docker Compose Commands
-
docker-compose up
Create and start containers defined in adocker-compose.ymlfile. -
docker-compose down
Stop and remove containers, networks, and volumes defined indocker-compose.yml. -
docker-compose build
Build images defined in adocker-compose.ymlfile. -
docker-compose logs
View logs from containers started bydocker-compose. -
docker-compose ps
List containers managed by Docker Compose.
7. Docker System Commands
-
docker system prune
Remove unused data (containers, networks, images, etc.). -
docker system df
Show disk usage information. -
docker info
Display detailed system-wide information about Docker.
8. Docker BuildKit Commands (Experimental)
-
DOCKER_BUILDKIT=1 docker build
Enable BuildKit for building Docker images (experimental feature).
9. Docker Other Commands
-
docker save -o <file_name>.tar <image_name>
Save a Docker image to a tarball file. -
docker load -i <file_name>.tar
Load a Docker image from a tarball file. -
docker login
Log into a Docker registry. -
docker logout
Log out of a Docker registry.
10. Docker Login Commands
-
docker login
Log into a Docker registry (e.g., Docker Hub). -
docker logout
Log out of a Docker registry.
docker run -p <host_port>:<container_port> <image_name>
Mapping port number with two different containers
In Docker, detached mode and attached mode refer to how a container runs and how you interact with it:
In Docker, attach and detach modes are used to control how you interact with the container's processes and terminals. Here's an explanation of both modes:
1. Attach Mode
In attach mode, you connect your terminal to the running container's standard input (stdin), standard output (stdout), and standard error (stderr). This allows you to interact with the container directly, as if you're running the process inside the container.
Key Characteristics of Attach Mode:
-
You can interact with the container’s primary process directly (for example, if it's running a shell or a service that uses standard input/output).
-
You cannot run multiple processes in the container from a single terminal. If you exit the process, the connection will be closed.
-
To detach from the container and leave it running, you need to use a specific detach key combination (usually
Ctrl + CorCtrl + Pfollowed byCtrl + Q).
Command to Run in Attach Mode:
To run a container in attach mode, simply use the docker run command without any specific options for detaching.
For example:
This command starts an ubuntu container and attaches your terminal to the bash shell running inside the container. You can interact with the shell in real-time.
Detaching from Attach Mode:
To detach from a container running in attach mode (without stopping it), use the following key sequence:
-
Press
Ctrl + Pfollowed byCtrl + Q.
This will detach your terminal from the container, leaving the container running in the background.
2. Detach Mode
In detach mode, you run the container in the background, and your terminal is freed up for other tasks. The container continues to run, but you don’t interact with it directly unless you reattach or check logs.
Key Characteristics of Detach Mode:
-
The container runs in the background, and the terminal is available for other commands.
-
The container runs independently, and you can disconnect from it without affecting its execution.
-
You can reattach to the container later or view its logs.
Command to Run in Detach Mode:
To run a container in detach mode, you use the -d flag:
For example:
This will start the nginx container in the background and return the container's ID.
Reattaching to a Running Container:
If you want to reattach to a container that’s running in the background (detached mode), you can use the following command:
For example:
This will reconnect your terminal to the running container and let you interact with its main process.
Viewing Logs of a Detached Container:
If you don't want to reattach to a running container but want to see its output, you can use the docker logs command:
For example:
This will display the logs of the container without needing to attach to it.
Summary of Commands:
| Mode | Command | Key Features |
|---|---|---|
| Attach Mode | docker run -it <image_name> | Directly interact with the container’s primary process. |
| Detach Mode | docker run -d <image_name> | Run container in the background. Use docker attach or docker logs to view output. |
| Detach from Attach Mode | Ctrl + P, Ctrl + Q | Detach from the container without stopping it. |
| Reattach to Container | docker attach <container_id> | Reconnect to a running container in attach mode. |
Key Differences Between Attach and Detach Modes:
-
Attach Mode keeps your terminal attached to the running container, and you can interact with the primary process (e.g., a shell or service).
-
Detach Mode runs the container in the background, freeing your terminal for other tasks while the container continues running.
Let me know if you need more details or examples!
Comments
Post a Comment