Image
docker image ls
docker pull xxx
$ docker build -t imagename:2.0 .
docker rmi -f $(docker images -q myname)
# or
docker rmi -f image-name
docker rmi -f $(docker images -aq)
Container
docker ps -a
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 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
docker rm -f $(docker ps -aq)
# HOST_PORT:CONTAINER_PORT
docker run -p 8000:8000 myimage
- Copy file from/to container
docker cp test.txt mycontainer:/app/test.txt
Volume
docker volume ls
- Mount a volume from another container
--volumes-from container_name
Networking
--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
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
docker tag image-name DockerHubAcc/image-name
Save/Export and Load/Import
docker save myimage > myimage.tar
# Using gzip for compression
docker save myimage:tag | gzip > myimage_tag.tar.gz
docker load < myimage.tar
docker export mycontainer > mycontainer.tar
docker import mycontainer.tar image-name
Logging/Stats
docker logs container_name
# Use -f to watch the logs
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
Use &&
and \
to chain commands to avoid too many intermediate images get created.
ADD can auto extract .tar.gz
and handle remote URL, otherwise use COPY.
Declare auto created volumes using container paths
Only for declaration purpose. docker run -P
takes a random port to map to this exposed port.
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
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
docker-compose logs
docker-compose ps