Docker is a popular platform for creating, deploying, and running applications in containers. It provides an efficient way to package and deploy applications, ensuring that they run consistently regardless of the underlying environment. In this article, we will cover all the essential Docker commands that you need to know to work with Docker effectively.
- Docker run
The Docker run command is used to create a container from a Docker image and start it. The syntax for the command is:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Where:
OPTIONS: are optional flags that you can use to customize the container behavior.
IMAGE: is the Docker image that you want to use to create the container.
COMMAND: is the command that you want to run inside the container.
ARG: are the arguments passed to the command.
Some common options include:
-d: Run the container in detached mode.
-p: Bind container ports to the host.
--name: Set a custom name for the container.
-e: Set environment variables.
-v: Bind mount a volume.
Example:
docker run -d --name mycontainer -p 8080:80 nginx
This command creates a container from the Nginx image, runs it in detached mode, and binds port 8080 on the host to port 80 in the container.
Docker build
The Docker build command is used to build a Docker image from a Dockerfile. A Dockerfile is a text file that contains instructions for building the image. The syntax for the command is:
docker build [OPTIONS] PATH
Where:
OPTIONS: are optional flags that you can use to customize the build process.
PATH: is the path to the directory containing the Dockerfile.
Some common options include:
-t: Set a custom name and optional tag for the image.
--no-cache: Build the image without using the cache.
Example:
docker build -t myimage:1.0 .
This command builds a Docker image named myimage with version 1.0 from the Dockerfile located in the current directory.
Docker images
The Docker images command is used to list all the Docker images that are available on the system. The syntax for the command is:
docker images [OPTIONS] [REPOSITORY[:TAG]]
Where:
OPTIONS: are optional flags that you can use to customize the output.
REPOSITORY: is the repository name of the image.
TAG: is the tag of the image.
Some common options include:
-a: List all images, including intermediate images.
--filter: Filter images based on various criteria.
Docker ps
The Docker ps command is used to list all the running containers on the system. The syntax for the command is:
docker ps [OPTIONS]
Where:
- OPTIONS: are optional flags that you can use to customize the output.
Some common options include:
-a: List all containers, including stopped ones.
-q: List only container IDs.
--filter: Filter containers based on various criteria.
Docker rm
The Docker rm command is used to remove one or more stopped containers. The syntax for the command is:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Where:
OPTIONS: are optional flags that you can use to customize the removal process.
CONTAINER: is the name or ID of the container to remove.
Some common options include:
-f: Force the removal of a running container.
-v: Remove the associated volumes.
docker exec
The Docker exec command is used to run a command inside a running container. The syntax for the command is:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Where:
OPTIONS: are optional flags that you can use to customize the execution process.
CONTAINER: is the name or ID of the container to execute the command in.
COMMAND: is the command to run inside the container.
ARG: are the arguments passed to the command.
Some common options include:
-i: Keep STDIN open even if not attached.
-t: Allocate a pseudo-TTY.
-u: Set the user to run the command as.
-e: Set environment variables.
Example:
bashCopy codedocker exec -it mycontainer bash
This command runs the Bash shell inside the container named mycontainer.
Docker logs
The Docker logs command is used to display the logs of a container. The syntax for the command is:
codedocker logs [OPTIONS] CONTAINER
Where:
OPTIONS: are optional flags that you can use to customize the log output.
CONTAINER: is the name or ID of the container to display the logs of.
Some common options include:
-f: Follow the log output in real-time.
--tail: Show the last N lines of logs.
--since: Show logs since a specific timestamp.
Example:
codedocker logs mycontainer
This command displays the logs of the container named mycontainer.
Docker network
The Docker network command is used to manage Docker networks. Networks are used to isolate containers from each other and provide communication between them. The syntax for the command is:
codedocker network [OPTIONS] COMMAND
Where:
OPTIONS: are optional flags that you can use to customize the network management.
COMMAND: is the sub-command to run.
Some common sub-commands include:
create: Create a new network.
ls: List all available networks.
inspect: Display detailed information about a network.
Example:
codedocker network create mynetwork
This command creates a new Docker network named mynetwork.
Docker Compose Commands
docker-compose up
The docker-compose up
command is used to start the services defined in your docker-compose.yml file. The syntax for the command is:
codedocker-compose up [OPTIONS] [SERVICE...]
Where:
OPTIONS: are optional flags that you can use to customize the behavior of the
docker-compose up
command.SERVICE: is the name of the service(s) defined in your docker-compose.yml file that you want to start.
Some common options include:
-d: Run the services in detached mode (in the background).
--build: Build the images before starting the containers.
--no-recreate: Don't recreate containers that already exist.
Example:
codedocker-compose up
This command starts all the services defined in your docker-compose.yml file.
docker-compose down
The docker-compose down
command is used to stop and remove the containers created by docker-compose up
. The syntax for the command is:
codedocker-compose down [OPTIONS]
Where:
- OPTIONS: are optional flags that you can use to customize the behavior of the
docker-compose down
command.
Some common options include:
-v: Remove the containers and their volumes.
--rmi: Remove the images used by the containers.
Example:
codedocker-compose down
This command stops and removes the containers created by docker-compose up
.
docker-compose ps
The docker-compose ps
command is used to list the services and containers defined in your docker-compose.yml
file. The syntax for the command is:
codedocker-compose ps [OPTIONS] [SERVICE...]
Where:
OPTIONS: are optional flags that you can use to customize the output of the
docker-compose ps
command.SERVICE: is the name of the service(s) defined in your
docker-compose.yml
file that you want to list.
Some common options include:
-q: Only display container IDs.
--services: Only display service names.
Example:
codedocker-compose ps
This command lists all the services and containers defined in your docker-compose.yml
file.
docker-compose logs
The docker-compose logs
command is used to display the logs of the services defined in your docker-compose.yml
file. The syntax for the command is:
codedocker-compose logs [OPTIONS] [SERVICE...]
Where:
OPTIONS: are optional flags that you can use to customize the output of the
docker-compose logs
command.SERVICE: is the name of the service(s) defined in your
docker-compose.yml
file that you want to display the logs for.
Some common options include:
-f: Follow the logs in real-time.
--tail: Only display the last N lines of logs.
Example:
codedocker-compose logs myservice
This command displays the logs of the service named myservice
.
docker-compose build
The docker-compose build
command is used to build the Docker images for the services defined in your docker-compose.yml
file. The syntax for the command is:
codedocker-compose build [OPTIONS] [SERVICE...]
Where:
OPTIONS: are optional flags that you can use to customize the behavior of the
docker-compose build
command.SERVICE: is the name of the service(s) defined in your
docker-compose.yml
file that you want to build the images for.
Some common options include:
--no-cache: Do not use cache when building the image.
--pull: Always attempt to pull a newer version of the image.
Example:
codedocker-compose build myservice