Docker Cheatsheet

Last updated April 27, 2020

What is Docker?

Docker creates a container for your program. Let’s say we want to run a Python program on our computer. We have to install Python to run any programs. Then we can create and run the program on our computer. Docker is its own little computer that has nothing installed except what you install on it, which in our instance is Python. Instead of running locally on our computer, we can run our program in Docker and then put the entire Docker container on a server to run automatically.

How do we create a Python container? Well we don’t have to create it from scratch. Docker Images are pre-configured Docker containers that we can copy. When we want to create a new container, we can either create it ourselves from scratch or copy one from DockerHub which is kinda like GitHub, only for Docker Images specifically. When we run docker run python, if we don’t have an image called Python on our computer, Docker will automatically grab it from DockerHub!

Remember, Docker is just a container that makes sure your program has everything it needs to run. There is a lot more functionality included with Docker that I’m not going to get into. I taught myself Docker in a couple of weeks using these resources which I liked because they had labs in browser:

  1. KodeKloud Docker for Beginners
  2. KataKoda Docker Labs
  3. KodeKloud Kubernetes for Beginners

Command Cheatsheet

Basic Commands

docker search someImageSearch for image
docker run --name containerName -d imageNameRun container in background
docker run --name containerName -a imageNameRun container in foreground
docker psView all running containers
docker run -p hostPort:containerPort imageNameRun on specific port
docker run -p 8000 imageNameSet a random container port
docker port containerName hostPortFind random port
docker run -v hostDirectory:containerDirectoryStore changes to host
I enjoy engineering software and writing about it. Follow me on Twitter for updates!