-
Notifications
You must be signed in to change notification settings - Fork 0
Docker Command Documentation
Docker is a platform for developing, shipping, and running applications in containers. Containers package an application’s code, libraries, and dependencies together in an isolated environment. The Docker CLI (command-line interface) provides a rich set of commands for managing images, containers, networks, and volumes. The sections below detail the basic Docker commands along with their syntax, description, and usage examples.
-
docker version
Displays detailed version information for both the Docker client and daemon.docker version
-
docker info
Provides system-wide information about Docker (such as number of images, containers, storage driver, and more).docker info
These commands are often the first step to verify that Docker is properly installed and running on your system.
Docker images are the blueprints used to create containers.
-
docker pull
Downloads an image from a Docker registry (default is Docker Hub).
Syntax:docker pull [OPTIONS] IMAGE[:TAG]
Example:
docker pull ubuntu:latest
This command downloads the latest Ubuntu image.
-
docker build
Creates an image from a Dockerfile and context (the set of files at a specified location). Syntax:docker build [OPTIONS] PATH | URL | -
Example:
docker build -t myapp:1.0 .
Here, Docker reads the Dockerfile in the current directory (.) and tags the resulting image as
myapp:1.0
.
-
docker images
Lists all images on your local system.docker images
-
docker rmi
Removes one or more images from your local repository. Syntax:docker rmi [OPTIONS] IMAGE [IMAGE...]
Example:
docker rmi myapp:1.0
-
docker tag
Creates a new tag for an image. Syntax:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Example:
docker tag myapp:1.0 myusername/myapp:latest
Containers are running instances of Docker images.
-
docker run
Creates and starts a container from a specified image. Syntax:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Key Options:
-
-d
: Run container in detached (background) mode. -
-it
: Allocate a pseudo-TTY and keep STDIN open (interactive mode). -
--name
: Assign a custom name to the container. -
-p
: Publish a container’s port(s) to the host.
Example:
docker run -d -p 8080:80 --name webserver nginx:latest
This command runs the NGINX container in the background, maps port 80 in the container to port 8080 on the host, and names it “webserver”.
-
-
docker ps
Lists all currently running containers.docker ps
-
docker ps -a
Lists all containers (including stopped ones).docker ps -a
-
docker stop
Gracefully stops a running container. Syntax:docker stop CONTAINER [CONTAINER...]
Example:
docker stop webserver
-
docker restart
Restarts one or more containers.docker restart webserver
-
docker kill
Immediately kills a running container without a graceful shutdown.docker kill webserver
-
docker rm
Removes one or more containers. (Use-f
to force remove a running container.)docker rm webserver
-
docker exec
Runs a command inside a running container. Syntax:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example (interactive shell):
docker exec -it webserver bash
This opens an interactive Bash shell inside the container named “webserver”.
-
docker logs
Fetches and displays the logs from a container. Syntax:docker logs [OPTIONS] CONTAINER
Example:
docker logs webserver
You can add the
-f
flag to follow log output in real time.
-
docker inspect
Returns low-level information about a container (or other Docker objects) in JSON format.docker inspect webserver
Persistent data and networking are essential for containerized applications.
-
docker volume create
Creates a new volume.docker volume create my-volume
-
docker volume ls
Lists all volumes.docker volume ls
-
docker volume rm
Removes a volume.docker volume rm my-volume
Volumes allow you to persist data independently from the container lifecycle.
-
docker network create
Creates a new network.docker network create my-network
-
docker network ls
Lists all networks.docker network ls
-
docker network rm
Removes a network.docker network rm my-network
Using custom networks helps containers communicate securely.
Over time, unused images, containers, networks, and volumes can accumulate.
-
docker system prune
Removes all stopped containers, dangling images, unused networks, and optionally, volumes.docker system prune -a --volumes
This command helps free up disk space.
-
docker image prune
Removes dangling images (images not tagged or referenced by any container).
-
docker tag
– Retag an existing image. -
docker commit
– Create a new image from changes made to a container’s filesystem.docker commit CONTAINER newimage:tag
-
docker login
anddocker logout
– Authenticate with and sign out from a Docker registry.docker login docker logout
These commands round out the basic functionality needed to work effectively with Docker.
This documentation covers the core Docker commands required to manage images, containers, networks, and volumes. Mastery of these commands is essential for developers and DevOps professionals to build, run, and maintain containerized applications efficiently.
For further details, always refer to the official Docker documentation and CLI reference:
- Official Docker CLI Reference:
- Docker container management documentation:
By understanding and practicing these commands, you will have a strong foundation in Docker’s capabilities and be well on your way to containerizing your applications successfully.
Below is an updated section of the Docker command documentation that not only covers the basic commands but also provides detailed, step‐by‐step instructions on how to start a containerized application using Docker. This guide explains the process, the flags used, and best practices, so that you can easily run your containerized application and understand what is happening behind the scenes.
When your application is containerized—that is, packaged into one or more Docker images—you use Docker to start (or “run”) containers based on those images. This section explains in detail how to launch a containerized application, along with explanations of the commonly used options.
The fundamental command to create and start a container is:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- IMAGE: This is the name (and optionally the tag) of the Docker image that contains your application.
- COMMAND (optional): You can override the default command defined in the Dockerfile by specifying your own command here.
- ARG (optional): Any arguments you wish to pass to the command.
Imagine you have containerized a web application using an image named myapp:latest
. To start this container so that the web service is accessible, you might use:
docker run -d -p 8080:80 --name my-webapp myapp:latest
Let’s break down what each flag means:
-
-d
(Detached mode):
Runs the container in the background. This is ideal for long-running services (e.g., web servers) since you aren’t tying up your terminal. -
-p 8080:80
(Port mapping):
Maps port 80 of the container (the port on which the application listens) to port 8080 on your host.
This means when you open your browser and navigate tohttp://localhost:8080
, you are accessing the application running inside the container. -
--name my-webapp
(Container name):
Assigns a custom name (“my-webapp”) to the container, making it easier to reference in subsequent commands (e.g., for stopping, inspecting, or removing the container). -
myapp:latest
(Image name and tag):
Specifies the Docker image that the container will be created from. The tag “latest” denotes that the most recent version of the image is used.
Sometimes you may want to interact directly with the container’s shell for debugging or manual configuration. To do so, you use interactive mode along with a pseudo-TTY:
docker run -it --name debug-container myapp:latest bash
-
-it
:
Combines-i
(interactive – keeps STDIN open) and-t
(allocates a pseudo-TTY) to give you an interactive shell session inside the container. -
bash
:
Overrides the default command and starts a Bash shell so that you can manually inspect or change configurations within the container.
Many containerized applications require configuration via environment variables (for example, setting a database connection string or an API key). You can pass these variables using the --env
(or -e
) flag:
docker run -d -p 8080:80 --name my-webapp -e APP_ENV=production -e API_KEY=abcdef123456 myapp:latest
-
-e APP_ENV=production
and-e API_KEY=abcdef123456
:
These flags set environment variables inside the container. The application can then read these variables at runtime to adjust its behavior accordingly.
If your application writes data that you don’t want to lose when the container stops or is removed, you can use volumes. For example, if your web application stores uploaded files, you might run:
docker run -d -p 8080:80 --name my-webapp -v /host/path/uploads:/app/uploads myapp:latest
-
-v /host/path/uploads:/app/uploads
:
Maps the host directory/host/path/uploads
to the container’s/app/uploads
directory. Any files written by the container to/app/uploads
will be stored on your host machine.
If your application is part of a larger system (e.g., a multi-container setup with a separate database), you might need to attach the container to a custom Docker network:
docker network create my-app-network
docker run -d --name my-webapp --network my-app-network -p 8080:80 myapp:latest
-
docker network create my-app-network
:
Creates a new user-defined bridge network. -
--network my-app-network
:
Connects your container to this network so it can easily communicate with other containers attached to the same network (such as a database container).
For applications that require multiple services (e.g., a web server, a database, a cache), Docker Compose is a powerful tool that allows you to define and run multi-container Docker applications. A typical docker-compose.yml
file might look like this:
version: "3.8"
services:
web:
image: myapp:latest
container_name: my-webapp
ports:
- "8080:80"
environment:
- APP_ENV=production
volumes:
- ./uploads:/app/uploads
networks:
- app-network
db:
image: postgres:13
container_name: my-database
environment:
- POSTGRES_PASSWORD=secret
volumes:
- db-data:/var/lib/postgresql/data
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
db-data:
To start the multi-container application defined in the file, simply run:
docker-compose up -d
-
docker-compose up -d
:
This command reads thedocker-compose.yml
file, builds (if necessary) and starts all the defined services in detached mode.
Docker Compose simplifies the orchestration of multiple containers, ensuring that they are all configured correctly and can communicate with each other.
-
Verify Docker is Running: Always check your installation with
docker version
anddocker info
. -
Use Meaningful Names: The
--name
flag helps in managing containers. -
Map Ports Thoughtfully: Ensure the host-to-container port mapping (with
-p
) does not conflict with other services. -
Leverage Environment Variables: Use
-e
to pass configuration parameters securely. -
Persist Data with Volumes: Use
-v
to keep data safe even if containers are recreated. - Use Networks for Multi-Container Apps: Custom networks ensure secure and efficient inter-container communication.
- Consider Docker Compose for Complex Setups: It simplifies the management of multi-container applications.
⚠️ Disclaimer: These documents are not official documentation and may contain outdated, incomplete, or incorrect information. Please verify the information before use.