Skip to content

Instantly share code, notes, and snippets.

@leoluyi
Forked from buonzz/docker-compose-cheatsheet.sh
Last active February 22, 2019 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoluyi/e979102bfdcb1e2c3b96ff87a437f0d5 to your computer and use it in GitHub Desktop.
Save leoluyi/e979102bfdcb1e2c3b96ff87a437f0d5 to your computer and use it in GitHub Desktop.
docker-compose cheatsheet

docker-compose cheatsheet

docker-compose up # Starts multiple containers from docker-compose.yaml in same dir
docker-compose exec <service> <cmd> # Execute command in running container
docker-compose down # Removes associated containers, networks and volumes

$ docker-compose up -d         # start containers in background
$ docker-compose kill          # stop containers
$ docker-compose up -d --build # force rebuild of Dockerfiles
$ docker-compose rm            # remove stopped containers
$ docker ps                    # see list of running containers
$ docker exec -ti [NAME] bash # ssh to the container

# list all images
docker images

# tag and publishing
docker tag <id> buonzz/name:version
docker login
docker push buonzz/name

# delete image
docker rmi -f <id>

# run an image
docker run yourusername/docker-whale

# view logs
docker logs [OPTIONS] CONTAINER

# set the port to expose in host
docker run -p 3000 my_image

# start new container interactively
docker container run -it

Docker

docker build . -t <namespace>/<imgname>:<tag> # Build a Dockerfile
docker logs <container> # Show container output
docker volume ls # List all volumes
docker stop $(docker ps -q) # Stop all containers
docker rm -f $(docker ps -aq) # Remove(and stop) all containers
docker container prune # Same as the above command
docker rm $(docker ps -a -q -f "status=exited*") # Removed only stopped containers
docker volume rm $(docker volume ls -q) # Remove all volumes
docker volume prune # Same as above command
docker rm -fv <container> # Removes both container and associated volumes
docker volume rm $(docker volume ls -qf dangling=true) # Remove any associated volumes
docker image prune # Removes any unused images
docker rmi $(docker images -qf dangling=true) # Remove dangling images
 
docker network ls # List container networks
docker network inspect <network_name> # Show network details
 
docker inspect <container> # Container info as json
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" <containerid> # Get container IP Address
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) # Get Ip Address for all containers
 
# Connect to an existing running container
docker exec -it <container> powershell
 
# Copy files into a container
docker cp <Host path> <Container ID>:<Container path>
 
# Example
docker cp /tmp/mydb.mdf d6b75213ef80:/var/opt/mssql/data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment