It’s not an exaggeration to say that Docker has changed the way developers and engineers do their jobs. It gives you a way to move code between systems, improve security, and decouple dependencies between applications. So if you’re not already using Docker as part of your development process, it’s time to give it a close look.
In this article, you’ll learn five useful Docker commands. They’ll get you started on your way to mastering this valuable tool.
#1: Run a Container With Docker
The first Docker command you need to know is how to start an image. The good news is that it’s easy!
Docker run expects the name of a valid Docker image. It checks its local registry and starts a container if the name is already there. If it’s not, Docker checks with its remote registries and pulls from one of them.
Run has an extensive set of command-line arguments. You can get them with Docker run -help.
This is only the beginning of the help message. Docker has many options, but you only need to worry about a few to get started with Docker commands.
For the “hello-world” image, we didn’t use any options. But the banner it displayed gave us a hint. Let’s try running the Ubuntu container that “hello-world” recommended in its banner message.
You’re running a bash session inside an Ubuntu container! Docker went to the repository, downloaded a container with an Ubuntu runtime, and started it. You used the -it argument, which tells Docker to create a pseudo-terminal for the container and connect it to your session. The last argument to the run command was the command to pass to the container. So, you’re running bash in Ubuntu.
#2: List Processes With Docker
Now you’ve seen how easy it is to run containers. Let’s take a look at how to check on them.
Leave the Ubuntu container running in one terminal and run this in another:
Docker listed information about the container, including its name: “laughing_elion” in my case. The data is wrapped and hard to read on a standard terminal. Try making it wider on your system.
#3: Stop and Remove Docker Containers
Let’s stop the Ubuntu container. On your system, use the name Docker chose.
If you check back in the terminal where you started it, you’ll see that you’re back at the command line on your host system.
Now, rerun Docker ps. The listing is empty. Is the container gone?
Let’s try adding another option to Docker ps.
You may have guessed that -a means listing all containers, including those that you stopped. My listing shows the Ubuntu container and a handful of hello-world containers.
Let’s get rid of the “hello-worlds.”
After running Docker rm, Docker ps verifies that the “hello-world(s)” are gone.
So, to remove a container, pass Docker rm and the container’s name. You can pass in more than one name too.
#4: Restart and Connect to Containers
You left the Ubuntu container stopped on your system. Let’s use the Docker commands for restarting and using it.
You’re back inside the Ubuntu container with bash.
Docker start restarts a stopped container. This is important for services like a web server or microservice that you want to stop to save on cloud costs and then quickly restart when needed. Docker containers start much faster than virtual machines.
Docker exec executes a command inside a running container. The command-line arguments are similar to run, but instead of an image name, you specify a container loaded and started.
#5: Name Your Containers
That name for your Ubuntu container isn’t very helpful, is it? If you were to leave a bunch of containers and then come back a few days later, you might have trouble telling them apart.
First, let’s get rid of that container.
Docker rm removes a stopped container. You have to stop a container before you can remove it.
Now, let’s start it with a more useful name.
The — name option allows you to specify a container name. So Docker ps shows us a container name “ubuntu_shell” instead of a randomly generated name.
Five Docker Commands
You now know how to start and stop Docker containers. You also learned how to give your containers custom names so they’re easier to keep track of. Finally, you’ve seen how to stop and restart containers and how to clean images on your system.
Don’t stop here. Check out Cprime’s Docker Containerization Boot Camp, a three-day hands-on workshop that will show you how to simplify your application development radically.