Docker K8s Labs

Docker commands cheat-sheet

Build an image

docker build <docker file path>

Run a container

docker run <image id/ image name>

List running containers

docker ps

List all containers

docker ps -a

List images

docker images

Stop a container

docker stop <container name>

Exposing ports

p flag is called publish

docker run -p <local port>:<container port> <container name>

Restarting existing container

docker start <container name/id>

Detached mode

use -d flag to start in detached mode

Attach to a running container

docker attach <container name/id>

Fetch container logs

docker logs <container name>

add -f to attach and see future logs

Interactive mode

docker run -it <image id/ name>

OR

docker start -a -i <container name/ id>

Remove a container

You cannot remove a running container. It needs to be stopped first

docker rm <container_name1> <container_name2> ...

Removing images

Images can only be removed if they aren't being used by any containers including stopped containers

docker rmi <imageid>

Remove unused images

docker image prune

Automatically remove container when it exits

Use the --rm flag

docker run --rm <image_id>

Inspect image

docker inspect <image_id>

Copy files to/from containers

docker cp <source> <dest>
docker cp local_path/ container_name:/path
docker cp container_name:/path local_path/

Name a container

docker run <imageid> --name myapp

Tag an image

docker build -t myname:mytag .