Image
- List all stored images
docker image ls
- Pull an image
docker pull xxx
- Build an image
$ docker build -t imagename:2.0 .
- Delete specific images
docker rmi -f $(docker images -q myname)
# or
docker rmi -f image-name
- Delete all images
docker rmi -f $(docker images -aq)
Container
- Show all containers
docker ps -a
- Run/stop a container
docker run -it ubuntu bash
# -d: Run in detached mode
# -v: HOST_PATH:CONTAINER_PATH
# --restart=always
# Stop a specific container
docker stop container_name
# Stop all containers
docker kill $(docker ps -q)
- Enter/exit a container
# Enter an exited container
docker start myname
docker exec -it myname bash
# Detach from a container
Ctrl-p + Ctrl-q inside the container shell
# Re-attach into an up container shell
docker attach myname
- Delete a specific container
docker rm -f container_name
- Delete all containers
docker rm -f $(docker ps -aq)
- Mirror ports
# HOST_PORT:CONTAINER_PORT
docker run -p 8000:8000 myimage
- Copy file from/to container
docker cp test.txt mycontainer:/app/test.txt
Volume
- List all volumes
docker volume ls
- Mount a volume from another container
--volumes-from container_name
Networking
- Link containers (legacy)
--link container_name (legacy)
# Then you can ping container_name
# It’s better to create a network and add the containers to the network as below
- Create a network
docker network create network-name
# Creates a bridge network by default
- network mode (–net)
- none: No network connection
- container: Mirror a specific container
- host: Mirror the host
- bridge: Default. Like NAT
- custom created network: Overlay or bridge
Tag
- Tag an image for pushing
docker tag image-name DockerHubAcc/image-name
Save/Export and Load/Import
- save (for image)
docker save myimage > myimage.tar
# Using gzip for compression
docker save myimage:tag | gzip > myimage_tag.tar.gz
- load (for image)
docker load < myimage.tar
- export (for container)
docker export mycontainer > mycontainer.tar
- import (for container)
docker import mycontainer.tar image-name
Logging/Stats
- Show log for a container
docker logs container_name
# Use -f to watch the logs
- Show HW stats
docker stats
Cleanup
- Delete all stopped containers
docker container prune
- Clear all space used by images, containers and volumes
docker system prune --volumes # Only remove dangling images
# -a: Remove all images with no container associated
Dockerfile
- FROM
- RUN
Use &&
and \
to chain commands to avoid too many intermediate images get created.
- ADD/COPY
ADD can auto extract .tar.gz
and handle remote URL, otherwise use COPY.
- ENV
- VOLUME
Declare auto created volumes using container paths
- EXPOSE
Only for declaration purpose. docker run -P
takes a random port to map to this exposed port.
- CMD/ENTRYPOINT
Most likely the same if used alone. If ENTRYPOINT is used, commands are appended as its arguments (either specified in command line or using CMD. CMD will serve as a default argument in this case)
Docker-compose
- Pull all related images
docker-compose pull
- Create/delete all objects
# Create all stuffs
docker-compose up -d
# Delete all stuffs
docker-compose down
- Start/stop the containers
# Start the containers
docker-compose start
# Stop the containers
docker-compose stop
- Show information
docker-compose logs
docker-compose ps