导航菜单

03 - Docker Basic Commands

Docker basic commands

With Docker installed, learn the everyday commands to manage images and containers.

Info and help

Version

docker --version

System info

docker info

Help

docker help
docker run --help

Image commands

Search on Docker Hub

docker search nginx

Pull image

docker pull nginx:latest

List local images

docker images
# or
docker image ls

Remove image

docker rmi nginx:latest
# or
docker image rm nginx:latest

Container commands

Create & start a container

docker run -d --name my-nginx -p 8080:80 nginx

Flags:

  • -d run detached
  • --name my-nginx container name
  • -p 8080:80 map host 8080 → container 80

List running containers

docker ps

List all containers

docker ps -a

Start/stop/restart

docker start my-nginx
docker stop my-nginx
docker restart my-nginx

Exec into container

docker exec -it my-nginx bash

-i keeps STDIN open; -t allocates a pseudo-TTY.

Logs

docker logs my-nginx
docker logs -f my-nginx

Inspect container

docker inspect my-nginx

Remove container

docker rm my-nginx        # stopped
docker rm -f my-nginx     # force remove running

Practical examples

Run a web server

docker run -d --name webserver -p 8080:80 nginx

Visit http://localhost:8080.

Run a database

docker run -d --name mysql-db \
  -e MYSQL_ROOT_PASSWORD=my-secret-pw \
  -v mysql-data:/var/lib/mysql \
  mysql:5.7
  • -e sets env vars.
  • -v mounts the named volume.

Resource usage

docker stats

Docker cheat sheet

CommandDescription
docker pullPull image
docker runCreate & start container
docker psList running containers
docker imagesList local images
docker startStart stopped container
docker stopStop running container
docker restartRestart container
docker execExec in running container
docker logsView logs
docker rmRemove container
docker rmiRemove image
docker buildBuild from Dockerfile
docker volumeManage volumes
docker networkManage networks

Summary

You now know the daily Docker commands for images and containers. Next: build custom images with Dockerfile.

搜索