0%

Docker Quick Reference

Images

Image: just like OS image, can be used to create a container.

Create image by:

  1. .tar file, with docker load -i <tar_file> command.
  2. Dockerfile script, with docker build -t command.

List images: Use docker images to show all available images on the device.

Container

Container: An instance running the OS. built from image.

Create from image by docker run command. You can pass in some configuration. (Note some config are fixed after creation. Be careful!) Some common parameters are shown below:

1
docker run -it -d -p <localport>:<dockerport> --gpus all --name <name> --volume </path/host/directory>:</path/container/directory> <IMAGE ID>
  • -d means “detach”, running the docker container in the backend.
  • -p <hostport>:<dockerport> means forward the docker’s port <dockerport> to the host machine’s port <hostport>. Usually, we forward 22 port from container to e.g., 2222 port in host for ssh connection.
  • --gpus all means all host’s GPU are visible to the container.
  • --name <name> indicates the name of the container. It will be shown in the docker ps and other places.
  • --volume </path/host/directory>:</path/container/directory> indicates which host directory are shared with container’s directory, therefore, you can access it in both the container and host.
  • <IMAGE ID> is the id of the image to be built.

List containers: Use docker ps -a to check status of all containers. Use docker ps to check all running containers.

Three ways to interact with the container:

  1. Docker desktop’s shell
  2. docker exec -it <container id> bash
  3. SSH, if you have set so

Start the container: By docker start <CONTAINER ID>, or do it in docker desktop.

Stop the container: By docker stop <CONTAINER ID>, or do it in docker desktop.

More info: please see the official cheat sheet at here.