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?
ENTRYPOINT 'executable', 'param1', 'param2' ENTRYPOINT command param1 param2 Configures a container that will run as an executable. ENTRYPOINT exec top -b This will use shell processing to substitute shell variables, and will ignore any CMD or docker run command line arguments. Metadata LABEL version='1.0'. And for more information on any Docker command, run: docker swarm command –help. Find the most important of these commands and a Docker Commands basic structure reminder on our attached Docker Commands Cheat Sheet. An all in one PDF to keep at hand. Docker run: # Run a container and connect to it docker run -it: # Run a container in the background docker run -d: # Stop a container docker stop # Kill a container docker kill Images/Repository # List available local images docker images # Search for docker images docker search. Image Management Commands. Image Transfer Comnands. Builder Main Commands. The Docker CLI Manage images docker build. Docker build options.t “app/containername” # name. Create an image from a Dockerfile. Docker run options IMAGE # see `docker create` for options. Run a command in an image. Manage containers docker create. Docker Command Essentials. The idea of docker essentials is to summarize all the most use full docker commands. Docker status related command. To check that the docker is working correctly. You need at least to have server detected. $ docker info Container. Container related command. To create and run a container.
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.
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.
Term | Description |
---|---|
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. |
Now with the basic terminology cleared, it’s time to look at the command line utilities that will make you love Docker.
Useful Docker Commands
This list of Docker commands includes the most useful commands you will use day to day while working with Docker containers.
Action | Command |
---|---|
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)”
Docker Command Cheat Sheet
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
Docker Compose Command Cheat Sheet
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.
Using Docker Compose
We are now in a position to create and run a container. Let’s stick a full copy of your workstation into a single container. Actually no, instead we’ll be smart and isolate various components from each other.
Consider the following typical project: a web application consists of one or more backend services and a database. While it is possible to fit all that into a single container, that is clearly not a best practice. So we’ll run these parts of the system independently and manage a couple of containers.
Luckily, there’s the a utility that helps us with just that, docker-compose. Docker compose allows you to specify a relationship between several containers in a yml file, and then run all of these containers at once.
Docker Compose Example
The syntax for the docker-compose.yml is pretty straightforward. You specify the container name, the image it should use and the starting command for the container. Additionally you can specify the mapping of the ports to your host machine ports and the mapping of the filesystem, so your docker container can see the files from your host system.
version: “2” services: web: container_name: “web” image: java:8 # image name command: java -jar /app/app.jar # command to run ports: # map ports to the host – “4567:4567” volumes: # map filesystem to the host – ./myapp.jar:/app/app.jar mongo: # container name image: mongo # image name
The best part of all this is that you can link the containers together. In the example above, we link the web container to the mongo container, so our web application will be able to find the database. And docker-compose takes care of providing the port mappings between the linked containers.
Let’s chat more about port mappings. You most likely won’t be able to access your web application on your host machine using localhost as the hostname. This is because your container is running in the docker-machine, instead of your host machine.
Docker Cli Commands Cheat Sheet
One way to overcome that is to use the following command to figure out the IP of your docker-machine:
Docker Commands The Ultimate Cheat Sheet Answers
docker-machine ip default
Alternatively you can use the forward2docker utility, that our colleague Sergei created, to monitor your docker-machine usage and automatically forward the ports to the localhost. This way, you can use your running application in a docker container as if it were available on localhost.
Run Tomcat on Java 8 with a custom javaagent attached. This command illustrates mapping the volumes on your host system into the docker container filesystem, see the -v flag part.
docker run -it –rm -p 8080:8080 -v /host/path/to/your/agent/agent.jar:/agent.jar -e JAVA_OPTS=”-javaagent:/agent.jar” tomcat:8.0.29-jre8
Final Thoughts
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:
Docker Cheat Sheet 2020
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.
Docker swarm cheat sheet. List of all commands to create, run, manage container cluster environment, Docker Swarm!
Docker swarm is a cluster environment for Docker containers. Swarm is created with a number of machines running docker daemons. Collectively they are managed by one master node to run clustered environment for containers!
In this article, we are listing out all the currently available docker swarm commands in a very short overview. This is a cheat sheet you can glance through to brush or your swarm knowledge or quick reference for any swarm management command. We are covering most used or useful switches with the below commands. There are more switches available for each command and you can get them with --help
Read all docker or containerization related articles here from KernelTalk’s archives.
Docker swarm commands for swarm management
This set of command is used mainly to start, manage the swarm cluster as a whole. For node management, within the cluster, we have a different set of commands following this section.
docker swarm init
: Initiate swam cluster- –advertise-addr: Advertised address on which swarm lives
- –autolock: Locks manager and display key which will be needed to unlock stopped manager
- –force-new-cluster: Create a new cluster from backup and dont attempt to connect to old known nodes
docker swarm join-token
: Lists join security token to join another node in swarm as worker or manager- –quite: Only display token. By default, it displays complete command to be used along with the token.
- –rotate: Rotate (change) token for security reasons.
docker swarm join
: Join already running swarm as a worker or manager- –token: Security token to join the swarm
- –availability: Mark node’s status as active/drain/pause after joining
docker swarm leave
: Leave swarm. To be run from the node itself- -f: Leave forcefully ignoring all warnings.
docker swarm unlock
: Unlocks swarm by providing key after manager restartsdocker swarm unlock-key
: Display swarm unlock key- -q: Only display token.
- –rotate: Rotate (change) token for security reasons.
docker swarm update
: Updates swarm configurations- –autolock: true/false. Turns on or off locking if not done while initiating.
Docker swarm node commands for swarm node management
Node is a server participating in Docker swarm. A node can either be a worker or manager in the swarm. The manager node has the ability to manage swarm nodes and services along with serving workloads. Worker nodes can only serve workloads.
docker node ls
: Lists nodes in the swarm- -q : Only display node Ids
- –format : Format output using GO format
- –filter : Apply filters to output
docker node ps
: Display tasks running on nodes- Above all switches applies here too.
docker node promote
: Promote node to a manager roledocker node demote
: Demote node from manager to worker roledocker node rm
: Remove node from the swarm. Run from the manager node.- -f : Force remove
docker node inspect
: Detailed information about the node- –format : Format output using GO format
- –pretty : Print in a human-readable friendly format
docker node update
: Update node configs- –role : worker/manager. Update node role
- –availability : active/pause/drain. Set node state.
Docker swarm service commands for swarm service management
Docker Commands The Ultimate Cheat Sheet Pdf
Docker service is used to create and spawn workloads to swarm nodes.
docker service create
: Start new service in Docker swarm- Switches of
docker container run
command like -i (interactive), -t (pseud terminal), -d (detached), -p (publish port) etc supported here.
- Switches of
docker service ls
: List services- –filter, –format and -q (quiet) switches which we saw above are supported with this command.
docker service ps
: Lists tasks of services- –filter, –format and -q (quiet) switches which we saw above are supported with this command.
docker service logs
: Display logs of service or tasksdocker service rm
: Remove service- -f : Force remove
docker service update
: Update service config- Most of the parameters defined in service create command can be updated here.
docker service rollback
: Revert back changes done in service config.docker service scale
: Scale one or more replicated services.- servicename=number format
docker service inspect
: Detailed information about service.