[Docker] Quickly entering a lightweight shell
docker run -it --rm alpine /bin/ash
docker run -it --rm alpine /bin/ash
First, get the internal IP of the target container:
docker container inspect $MY_CONTAINER
Then you can access the desire port by its IP
x.x.x.x:$TARGET_PORT
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)
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
docker rm -f container_name
docker rm -f $(docker ps -aq)
# HOST_PORT:CONTAINER_PORT
docker run -p 8000:8000 myimage
docker cp test.txt mycontainer:/app/test.txt
docker volume ls
--volumes-from container_name
--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
docker tag image-name DockerHubAcc/image-name
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
docker logs container_name
# Use -f to watch the logs
docker stats
docker container prune
docker system prune --volumes # Only remove dangling images
# -a: Remove all images with no container associated
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 pull
# Create all stuffs
docker-compose up -d
# Delete all stuffs
docker-compose down
# Start the containers
docker-compose start
# Stop the containers
docker-compose stop
docker-compose logs
docker-compose ps