Docker is a lightweight containerization solution that helps developers build, publish, and run applications more easily. When using Docker, it is necessary to be familiar with some common commands. This article will introduce some commonly used Docker commands, and give examples and explanations.
Docker command
Container Management Commands
1. docker run
docker run
command to create and start a new container. Here are some common options:
-d
: Run the container in the background.--name
: Specify a name for the container.-p
: Map container ports to host ports.-v
: Mount the host directory into the container.
Example:
docker run -d --name mycontainer -p 8080:80 -v /host/data:/container/data nginx
2. docker start/stop/restart
These commands are used to start, stop and restart containers.
docker start mycontainer # 启动容器
docker stop mycontainer # 停止容器
docker restart mycontainer # 重启容器
3. docker exec
docker exec
command is used to execute commands in a running container.
docker exec -it mycontainer bash # 进入容器内部的bash shell
docker exec mycontainer ls # 在容器内部执行ls命令
4. docker rm
docker rm
command is used to delete one or more containers.
docker rm mycontainer # 删除容器
docker rm container1 container2 # 同时删除多个容器
5. docker ps
docker ps
command to list running containers.
docker ps # 列出正在运行的容器
docker ps -a # 列出所有容器,包括停止的容器
Image Management Commands
1. docker pull
docker pull
The command is used to pull the image from the remote warehouse to the local.
docker pull nginx # 拉取最新的nginx镜像
docker pull nginx:1.19.10 # 拉取指定版本的nginx镜像
2. docker build
docker build
Commands are used to build images.
docker build -t myimage:1.0 . # 在当前目录下的Dockerfile文件中构建名为myimage的镜像,标签为1.0
3. docker push
docker push
The command is used to push the local image to the remote repository.
docker push myimage:1.0 # 推送名为myimage,标签为1.0的镜像到远程仓库
4. docker images
docker images
command to list local mirrors.
docker images # 列出本地的所有镜像
network management commands
1. docker network create
docker network create
command is used to create a network.
docker network create mynetwork # 创建名为mynetwork的网络
2. docker network connect/disconnect
These commands are used to connect and disconnect containers to and from the network.
docker network connect mynetwork mycontainer # 将容器连接到mynetwork网络
docker network disconnect mynetwork mycontainer # 将容器从mynetwork网络断开
3. docker network ls
docker network ls
command to list existing networks.
docker network ls # 列出所有网络
Of course, I can help you write an article about common commands of Docker. The following is a sample article (Markdown table format) about common commands of Docker:
organized into tables
Container Management Commands
Here are some commonly used container management commands and their usage:
Order | usage | describe |
---|---|---|
docker run | docker run [OPTIONS] IMAGE [COMMAND] [ARG...] |
Create and start a new container |
docker stop | docker stop [OPTIONS] CONTAINER [CONTAINER...] |
stop one or more containers |
docker start | docker start [OPTIONS] CONTAINER [CONTAINER...] |
Start one or more stopped containers |
docker restart | docker restart [OPTIONS] CONTAINER [CONTAINER...] |
restart one or more containers |
docker rm | docker rm [OPTIONS] CONTAINER [CONTAINER...] |
delete one or more containers |
docker ps | docker ps [OPTIONS] |
list running containers |
docker inspect | docker inspect [OPTIONS] CONTAINER [CONTAINER...] |
Get details about a container |
Using the above commands, you can create, stop, start, restart, delete containers, and view the status and details of the containers.
Image Management Commands
The following are some commonly used image management commands and their usage:
Order | usage | describe |
---|---|---|
docker pull | `docker pull [OPTIONS] NAME[:TAG | @DIGEST]` |
docker push | docker push [OPTIONS] NAME[:TAG] |
Push the local image to the remote warehouse |
docker build | `docker build [OPTIONS] PATH | URL |
docker images | docker images [OPTIONS] [REPOSITORY[:TAG]] |
List local mirrors |
docker rmi | docker rmi [OPTIONS] IMAGE [IMAGE...] |
delete one or more images |
Using the above commands, you can pull, push, build and delete mirrors, and view the list of local mirrors.
network management commands
Here are some commonly used network management commands and their usage:
Order | usage | describe |
---|---|---|
docker network create | docker network create [OPTIONS] NETWORK |
create a new network |
docker network connect | docker network connect [OPTIONS] NETWORK CONTAINER |
Connect a container to a network |
docker network disconnect | docker network disconnect [OPTIONS] NETWORK CONTAINER |
Disconnect a container from the network |
docker network ls | docker network ls [OPTIONS] |
list current networks |
docker network inspect | docker network inspect [OPTIONS] NETWORK [NETWORK...] |
get network details |
Using the above commands, you can create, connect, disconnect, list and view networks and their details.
Data Volume Management Commands
The following are some commonly used data volume management commands and their usage:
Order | usage | describe |
---|---|---|
docker volume create | docker volume create [OPTIONS] [VOLUME] |
Create a new data volume |
docker volume ls | docker volume ls [OPTIONS] |
List local data volumes |
docker volume inspect | docker volume inspect [OPTIONS] VOLUME [VOLUME...] |
Get the details of the data volume |
docker volume rm | docker volume rm [OPTIONS] VOLUME [VOLUME...] |
Delete one or more data volumes |
Using the above commands, you can create, list, view and delete data volumes.
container log command
Here are some commonly used container logging commands and their usage:
Order | usage | describe |
---|---|---|
docker logs | docker logs [OPTIONS] CONTAINER |
View container logs |
docker logs --follow | docker logs --follow [OPTIONS] CONTAINER |
Track container log output in real time |
docker logs --tail | docker logs --tail [OPTIONS] CONTAINER |
View the log for the last few lines of the container |
docker logs --since | docker logs --since [OPTIONS] CONTAINER |
View the logs of the container from the specified time |
in conclusion
This article introduces some commonly used Docker commands, covering container management, image management, and network management. By being familiar with these commands, you can more conveniently use Docker to develop, release, and deploy applications. If you need a more in-depth understanding of Docker commands, please refer to the official documentation.