HELPFUL DOCKER COMMANDS

  • NOTE: We're going to name containers using your username, because using names is easier than using ID's, but we'll need to avoid naming conflicts.

  • OTHER NOTE: We assume your environment already contains the image sg2, and also that the specified image has a tag at 1.0.



TO BUILD A CONTAINER

cd <DIR_CONTAINING_CLONED_REPO>
docker build --tag <DESIRED_IMAGE_NAME>:<TAG> . # EX: --tag my-container:1.0 .


TO START A LONG-RUNNING PROCESS INSIDE A CONTAINER SO THAT YOU CAN ENTER OR EXIT THE CONTAINER AT WILL, WHILE KEEPING THE CONTAINER (AND ITS PROCESS) RUNNING:

docker run --detach --tty --name $USER --user $(id -u):$(id -g) sg2:1.0 {LONG}/{RUNNING}/{PROCESS}

# EX: docker run --detach --tty --name $USER --user $(id -u):$(id -g) sg2:1.0 python3 /src/some_script.py

# EX: docker run --detach --tty --name $USER --user $(id -u):$(id -g) sg2:1.0 bash -c 'echo "Process started." && echo "Sleeping..." && sleep 300'

TO VIEW THE OUTPUT OF A RUNNING BUT DETACHED CONTAINER'S MAIN PROCESS: (The main process is the entrypoint specified when you started the container)

docker logs --follow $USER

# To exit and return to the host shell, press CTRL+C

TO "ATTACH" TO A DETACHED BUT RUNNING CONTAINER, AND ALSO START BASH IN YOUR TTY:

docker exec --interactive --tty $USER /bin/bash

# To exit and return to the host shell, type 'exit' and press enter, or press CTRL+C

TO STOP A RUNNING CONTAINER:

docker stop $USER

TO RESTART A STOPPED CONTAINER:

docker restart $USER

TO PAUSE A RUNNING CONTAINER (a paused container will not survive a reboot):

docker pause $USER

# To un-pause a paused container
# docker unpause $USER

STARTING A CONTAINER WITH READONLY ACCESS TO A DATASET (and access to host GPUs):

cd {DIR}/{WITH}/{CODE}

# Note that /{PATH/{TO}/{DATA} will be created if it doesn't exist. 
docker run --detach --tty --name $USER --user $(id -u):$(id -g) --gpus all -v $(pwd):/src -v /{PATH/{TO}/{DATA}:/data:ro sg2:1.

TO SAVE DOCKER IMAGE TO TAR FILE:

# See the docs: https://docs.docker.com/engine/reference/commandline/save/#description

TO REMOVE ALL STOPPED CONTAINERS:

# Note that this will not free significant disk space, 
# as the image itself is the primary contributor to disk usage.
docker container prune

TO REMOVE ALL UNUSED IMAGES:

docker image prune