Docker Cheat Sheet

Like the name implies, this is a cheat sheet to quickly find the command you need, they are ordered by the frequency a command is used, or at least what i think is going to be needed more frequently, I have also grouped them by function

The container name in the examples is mycontainer, it is just a name that you will need to replace with your own container name, the container ID here is always 12345abcdef

CommandArgumentsWhat it does
============>Containers – list
docker container lsDisplay running containers
docker container ls -aa: also show containers that are not runningDisplay all containers, running or not
docker psShow running containersPS is the same as LS but older
============>Containers – Run
docker run --name mycontainer -i -t imagename1- The name of the container to run (mycontainer)
2- The i flag indicating you’d like to open an interactive SSH session to the container. The i flag does not close the SSH session even if the container is not attached.
3- The t flag allocates a pseudo-TTY which much be used to run commands interactively.
4- The base image to create the container from (imagename).
Runs the container, and leaves you on a shell prompt that executes commands on that container (As if you have ssh-ed into it)
docker run --name mycontainer -d imagename-d for running the container in the background
docker stop my_containerStop the running container
docker exec -it mycontainer /bin/bash-it flag allows you to run a container in interactive modeIf this doesn’t work, you may not have bash installed, you can try the next command

Gives you access to the shell, much like opening an SSH session to the container
docker exec -it username/mycontainer /bin/sh
ctrl+p followed by ctrl+qDetach from container

Sometimes, accessing a container throght the command line may not be enough, there is a chance you want to access it for file transfer for example, in that case, you want port 22 exposed, and you want to be connected to it like you would connect to a virtual machine

Leave a Reply

Your email address will not be published. Required fields are marked *