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
Command | Arguments | What it does |
============> | Containers – list | |
docker container ls | Display running containers | |
docker container ls -a | a: also show containers that are not running | Display all containers, running or not |
docker ps | Show running containers | PS is the same as LS but older |
============> | Containers – Run | |
docker run --name mycontainer -i -t imagename | 1- 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 | -d for running the container in the background | |
docker stop my_container | Stop the running container | |
docker exec -it | -it flag allows you to run a container in interactive mode | If 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/ | ||
ctrl+p followed by ctrl+q | Detach 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