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!
Dockerrunexpects 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.
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-itargument, which tells Docker to create a pseudo-terminal for the container and connect it to your session. The last argument to theruncommand was the command to pass to the container. So, you’re runningbashin 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:
#3: Stop and Remove Docker Containers
Let’s stop the Ubuntu container. On your system, use the name Docker chose.
Now, rerun Docker ps. The listing is empty. Is the container gone?
Let’s try adding another option to Docker ps.
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.
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.
Now, let’s start it with a more useful name.
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.
Docker Containerization Boot Camp