Docker - Intro Cheatsheet

Docker is hot 🔥 right now so I just did the introduction to docker course by Andrew Tork Baker on O’Reily Media and I absolutely loved it. I can already see its usefulness and how integrating Docker in my projects will be helpful. Look for a short cheatsheet of Docker commands further down.

What Is It?

Simply put, Docker provides the isolation power of traditional VMs but without the overhead of running separate guest operating systems. Instead, the docker-engine is the containerization engine and allows the individual applications to potentially share resources, like reused libraries, for better efficiency. Super portable & lightweight!

Dockerfiles define what your container will.. well, contain 💡… and what commands it should run on startup. It’s important to always optimize your Dockerfile to reduce its size and reuse layers from other images in your repository. You first specify your base image, install any dependencies, then you finally add a RUN command.

Build the image from your Dockerfile with docker build . from the location of the file (docker will look for a file named ‘Dockerfile’ in this directory). Then use the docker run command to create a container from that image.

I found myself reusing many of the same commands over and over again when playing around with Docker, so for reference here’s a short cheat sheet of useful Docker commands. Hope it helps 😊. (Words between curly braces are variables)

CHEATSHEET:


docker run -it {image} : Launch the container in interactive mode (attach to terminal).


docker run -d {image} : Launch the container in detached mode (in the background).


docker run -p 8000:80 {image} : Map port 80 from the container to port 8000 on the host machine.


docker ps : List active containers.


docker ps -a : List all containers (including stopped containers).


docker ps -l : List latest container.


docker start/stop/pause {container} : Self-explanatory.


docker rm {container} : Delete a stopped container.


docker rm -f {container} : Force remove a container (even if its running).


docker logs {container} : Display the container’s logs.


docker logs -f {container} : Follow the logs in real-time as they come.


docker attach {container} : Attach the container to the terminal. This is basically like running docker run -it except on an already running container. But be careful, it will also listen to your input so if you exit attached mode the container will shut down.


docker diff {container} : Displays the diff of files that have been changed since container was created.


docker cp {container}:{container_filepath} {destination} : Copy files from container to host machine.


docker inspect {container} : View all the detailed information about your container.


Note: I’m currently running Docker on my Windows 7 machine (with VirtualBox & no native hypervisor).