Docker Commands Cheat Sheet
Today, we're presenting our Docker Commands Cheat Sheet — a one-page guide to using Docker that includes a glossary of common terms, useful one-liners, cleanup commands, machine commands, compose syntax and instructions on how to interact with a container.
If you want to grab a copy, head to the bottom of the page and download it today. But, if you want to learn more about Docker, and see some Docker command examples, be sure to read the rest of the article.
What Is Docker?
Docker is an open platform to develop, ship, and run applications, more commonly known as a container manager.
What's a container you ask? A container is an encapsulated environment, which runs on top of a very shallow level of abstractions, providing a virtual machine like isolation for running processes. (Learn more about containers in our webinar, Containers: From Chroots to Clusters).
Now if you are just started with Docker, it’s important we define the vocabulary that you'll need to learn as well as a number of utilities you need to master to become proficient with Docker.
What Is a Docker Machine?
A Docker machine is a utility that lets you install Docker on virtual hosts, and then manage those hosts from the command line.
Why should developers be interested in docker machine? It is currently the preferred way to run Docker on OSX and Windows. It also helps you to provision and manage docker containers on a network of machines, but that is less interesting from the developer's point of view. Docker machine will create a tiny virtual machine that is capable or running docker itself.
Popularity of Docker
How popular is Docker in 2021? Find out in our annual Java Developer Productivity Report.
Useful Docker Terms
Let's start with the terms. In a nutshell Docker uses images to specify what the container infrastructure should look like for you to run your apps in it.
|Layer||Set of read-only files to provision the system. Think of a layer as a read only snapshot of the filesystem.|
|Image||Read-only layer that is the base of your container. It can have a parent image to abstract away the more basic filesystem snapshot. So a Java image would inherit from a linux image with the preinstalled utilities. A tomcat image will have a Java image as the parent because it depends on Java to run Tomcat.|
|Container||Runnable instance of the image, basically it is a process isolated by docker that runs on top of the filesystem that an image provides.|
|Registry / Hub||Central place where all publicly published images live. You can search it, upload your images there and when you pull a docker image, it comes the repository/hub.|
|Docker machine||VM within which you can run Docker containers. On Linux you can run docker containers natively, but on OSX and Windows you need a layer of abstraction. A docker machine will spin a very lightweight virtual machine that integrates with the docker command line utilities really well.|
|Docker compose||Utility to run multiple containers as a system of containers. It will take care of making them aware of each other and ensure they’re properly connected to each other. This means you can run your application in one container and your database in a different container, and your analytics application in a different container, and so on. This is the ultimate isolation and it means that your applications are independent and are run in development in a very similar way to how the system might work in production.|
Useful Docker Commands
This list of Docker commands includes the most useful commands you will use day to day while working with Docker containers.
|Download an image, and all its parents, from the registry.||docker pull image_name|
|Run a shell command inside a freshly created and started container.||docker run -ti --name container_name image_name /command|
|Start a container||docker start container_name|
|Stop a container||docker stop container_name|
|Run a command inside a container from the image and remove the container when command is done.||docker run --rm -ti image_name /command|
|Run a shell command in the container.||docker exec -ti container_name "cmd"|
|Show/follow log output of the container.||docker logs -ft container_name|
|Kill all runnning docker containers.||docker kill $(docker ps -q)|
|Delete dangling Docker images.||docker rmi $(docker images -q -f dangling=true)|
|Remove all stopped containers.||docker rm $(docker ps -a -q)|
Running Commands on Docker Machines
After installing the machine you run a command like:
docker-machine start default
And your virtual machine named default is ready. However, what you also want is to make docker on the host system understand that it has to work with that virtual machine. To do that you'll run:
docker-machine env default eval "$(docker-machine env default)"
This command will configure your command line environment variables that will help you use docker with a particular docker-machine, in our case default, without additional complexity. As a developer you'll rarely need anything else from the docker machine, you can list the available machines with the docker-machine ls command and start or stop any particular of them by calling docker-machine start|stop machine-name. And now we're finally ready to dive into the heaven of containerized applications.
Want to see how many developers are using Docker in 2020? Check out our latest Java Productivity Report! You can watch a breakdown of the results via the video below, or download the full, free report by clicking here.
Building a Container in Docker
A docker container is basically a process or a set of processes running in isolation with a predefined provisioned file system. Think about it this way, a container is a collection of processes that have access to the files in the image.
Images are loaded from the registry, where they are publicly available to anyone. They form a hierarchy, so images can have dependencies on other images. This is really convenient, as your development platform images can depend on the image with common OS utilities.
docker pull image_name:tag
The pull command will download the image and all its parents needed to create the containers with that image. The tag parameter is usually used as the part of the name, but if your container evolves, or includes different versions of the software used, it's nice to tag them without changing the base image name.
docker create --name container_name image_name:tag
The create command is used to instantiate the container from the image. You almost always want to name it by providing the --name parameter. To start and stop the container, naturally, you'll use the docker start container_name and docker stop container_name commands respectively. Also, almost always when there's a stop command, there's a kill command too, to terminate the process less graciously.
Now there's a shorthand for create and start command, which is the run. It will create the container and run it, which is really useful for the various one-liners. We'll look at them in the next section of this post.
Sometimes you will want to create your own images, either to dockerize your project setup or just because you can. There are 2 ways to do that. Create a dockerfile in the directory that describes the image and then run:
docker build image_name .
Modify the container from the inside and then commit the changes to the image:
docker commit -m "commit message" -a "author" container_id username/imagename:tag
How do you change the container? You start it and then execute commands you like. To run a command inside the container namespace you run the following docker command:
docker exec -ti container_name "command.sh"
And while your container is running, you can do that multiple times: install necessary components, copy files around, etc. In general, you don't want to build your project into the container, you just map the filesystem on the host to a directory inside the docker container.
Then you don't have to worry about changing the build process for your project and you can run it in the same fashion on your host or in the docker container. But if you build the project in the container, map or commit your local Maven repository, npm modules or that of any dependency management tool you use: if will avoid downloading the internet again and again and make building the project so much faster.
Related Reading >> Preparing a Docker Compose Environment
Docker containers ensure applications run quickly and accurately across environments. It’s just one of many tools designed to increase developer productivity. JRebel cancels out the slow redeploys associated with Java development for instant code updates, while maintaining application state.
Download the Docker Cheat Sheet PDF
Our one-page Docker cheat sheet contains all the useful one-liners, Docker commands, syntax, and tips for interacting with a container that can all fit on one page. Be sure to download the pdf version by clicking the button below.
Other Java Development Cheat Sheets
Looking for other cheat sheets to help save you time during development?
Be sure to check out some of our Java cheat sheet collection:
- Java Regular Expressions Cheat Sheet
- SQL Cheat Sheet
- JVM Options Cheat Sheet
- Java Collections Cheat Sheet
- Maven Options Cheat Sheet
Want to Save Time During Docker Development?
JRebel can help developers working in Docker to skip redeploys, and instantly see changes to code. Want to try it on your Docker project? Click the link below to learn more about our free JRebel trial.