Commonly Used Docker Commands

Commonly Used Docker Commands

7月 7, 2018 · 3 分钟阅读时长 · 507 字 · -阅读 -评论

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

Conclusion

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

Alan H
Authors
开发者,数码产品爱好者,喜欢折腾,喜欢分享,喜欢开源