Docker Command CheatSheet: Difference between revisions

From CMU -- Language Technologies Institute -- HPC Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 6: Line 6:
[[Category:Cloud Computing]]
[[Category:Cloud Computing]]
[[Category:Tips]]
[[Category:Tips]]


[https://www.docker.com/ Docker] is an open-source platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow developers to package up an application with all parts it requires, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have, which could differ from the machine used for writing and testing the code. This page provides a comprehensive cheat sheet for various Docker commands categorized into building, running, and modifying containers and images. It serves as a quick reference guide for developers and system administrators working with Docker.
[https://www.docker.com/ Docker] is an open-source platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow developers to package up an application with all parts it requires, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have, which could differ from the machine used for writing and testing the code. This page provides a comprehensive cheat sheet for various Docker commands categorized into building, running, and modifying containers and images. It serves as a quick reference guide for developers and system administrators working with Docker.

Latest revision as of 10:40, 23 August 2023


Docker is an open-source platform designed to make it easier to create, deploy, and run applications by using containers. Containers allow developers to package up an application with all parts it requires, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have, which could differ from the machine used for writing and testing the code. This page provides a comprehensive cheat sheet for various Docker commands categorized into building, running, and modifying containers and images. It serves as a quick reference guide for developers and system administrators working with Docker.


Build

[edit | edit source]

Building and Managing Images

[edit | edit source]
  • Build an image:

docker build -t <image-name>:<tag> <path>

  • List images:

docker images

  • Remove an image:

docker rmi <image-id>

  • Save an image to a tar file:

docker save -o <path-for-tar-file> <image-name>:<tag>

  • Load an image from a tar file:

docker load -i <path-to-tar-file>

Running and Managing Containers

[edit | edit source]
  • Run a container:

docker run <image-name>

  • Run a container in detached mode:

docker run -d <image-name>

  • Run a container and publish a port:

docker run -p <host-port>:<container-port> <image-name>

  • Run a container and mount a volume:

docker run -v <host-directory>:<container-path> <image-name>

  • Run a container and set an environment variable:

docker run -e "KEY=VALUE" <image-name>

  • Run a container with a specific user (e.g., root):

docker run -u 0 <image-name>

  • Run a container with a specific user or UID/GID:

docker run -u <user> <image-name> docker run -u <uid>:<gid> <image-name>

  • Run a container and give it a name:

docker run --name <container-name> <image-name>

  • Run a container and link it to a network:

docker run --network <network-name> <image-name>

  • Run a container with limited memory:

docker run --memory <memory-limit> <image-name>

  • Run a container and restart it on-failure:

docker run --restart on-failure <image-name>

  • Run a container and override the default command:

docker run <image-name> <command>

Modify

[edit | edit source]

Managing Containers and Networks

[edit | edit source]
  • List running containers:

docker ps

  • List all containers (including stopped):

docker ps -a

  • Stop a running container:

docker stop <container-id>

  • Remove a container:

docker rm <container-id>

  • Execute a command inside an existing container:

docker exec -it <container-id> <command>

  • Create a network:

docker network create <network-name>

  • Connect a container to a network:

docker network connect <network-name> <container-id>

  • Disconnect a container from a network:

docker network disconnect <network-name> <container-id>

  • View logs for a container:

docker logs <container-id>

  • Copy files/folders between a container and the local filesystem:

docker cp <container-id>:<path> <local-path> docker cp <local-path> <container-id>:<path>

Image Distribution

[edit | edit source]
  • Push an image to Docker Hub:

docker push <image-name>:<tag>

  • Pull an image from a repository:

docker pull <image-name>:<tag>