Commonly Used Docker Commands

This article introduces commonly used Docker commands, including their advantages, implementation details, and related resources to improve efficiency when using Docker commands.

Jul 7, 2018 · 3 min read · 545 Words · -Views -Comments · Programming
Deployment and Operations 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

Here I’ll record frequently used Docker commands for future reference

Docker

# For logging into custom sources, add domain registry address
docker login [x.xxx.com]

# View Docker version, status, proxy information, etc.
docker info

# Check Docker service status on Linux
service docker status

# Start Docker service on Linux
service docker start

# View all containers (including those not running)
docker ps -a
# Delete all containers
docker rm -f $(docker ps -aq)

# Create a container, -P means Docker randomly maps a port to the internal container's open port, -p 8888:80 means specify port mapping to container port 80
docker run -p 8888:80 -d --name "hellonginx" nginx:latest 

# Enter Docker container bash
docker exec -it containerId bash

# Stop container
docker stop containerId

# Delete container
docker rm contaierId

# View all images
docker images

# Delete all images
docker rmi -f $(docker images -aq)

# Create image, can specify output location with -o, note that multiple tags can be specified, such as -t t1 -t t2
docker build -t toolkit:v1 . 

# Push image, supports including repository address, such as private source address
docker push toolkit:v1

# View logs, useful when container doesn't start normally
docker logs containerId --timestamps

# Output image file as tar package, image ID, image name/TAG all work
docker save <imageId> -o <filename>.tar

# Update container restart policy
docker update --restart=always my-container

docker-compose

Official website: https://docs.docker.com/compose/reference/

# Start orchestration service, (default: docker-compose.yml), note if -f is set, it must be at the beginning, don't change parameter order
docker-compose -f marvel-docker-compose.yml up -d

# Recreate containers --force-recreate
docker-compose up -d --force-recreate [service]

Common Issues Explained

-p parameter

Write the port mapping completely, such as -p 8888:80, don’t use abbreviated form

Port format is host port:container port or HOST:CONTAINER

For port configuration of each container in docker-compose, follow the same format

build supports URL

Sometimes when building images for private repositories, you need to add the URL for build and push, like docker build github.com/creack/docker-firefox

Stop and delete target container

docker ps -q --filter "name=hellonginx" | grep -q . && docker stop hellonginx && docker rm -fv hellonginx

Error: Permission denied

For example, if you get permission issues when reading after starting nginx with Docker, it’s because the Docker mounted disk doesn’t have sufficient permissions. The solution is:

chmod -R 777 volume/

Why does this permission issue occur? Because when Docker runs, the system user for external access is Docker, not root, so there’s a possibility of insufficient permissions. You can check the permission situation of the corresponding disk directory.

Accessing host services from inside the container

You can’t directly access 127.0.0.1; you need to configure the network for normal access

https://static.1991421.cn/2022/2022-05-03-000011.png

docker-compose

Configure as follows, and use host.docker.internal instead of local IP address for access

extra_hosts:
 - "host.docker.internal:host-gateway"

.dockerignore/Copy blacklist

If you don’t want to copy files into the image, you can solve this by configuring a blacklist through .dockerignore

https://docs.docker.com/engine/reference/builder/#dockerignore-file

Pulling a specific architecture

By default, Docker pulls the image matching the architecture of the machine running the command. If you need a specific architecture, use the --platform option.

docker pull --platform linux/amd64 nginx:1.29.4
docker pull --platform linux/arm64 nginx:1.29.4

Conclusion

The above commands may change or become invalid; always refer to the official CLI as the definitive resource, click here

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover