DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Beginner's Guide to Docker Compose
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues
  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular

Trending

  • You Don't Have to Pick One: A Hybrid Approach to Enterprise Data Refresh
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • How to Evaluate Coding Agents With Harbor Framework, Python, Docker

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Docker Commands Beginners Should Know

Docker Commands Beginners Should Know

This tutorial reviews basic Docker commands for a new user.

By 
Eric Goebelbecker
DZone Core CORE ·
Aug. 17, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Docker is an open-source platform for creating, deploying, and running containers. It's available for macOS, Windows, and Linux, and enjoys wide support from all the major cloud providers. So, you can create your containers on your favorite operating system and deploy them anywhere.

Let's look at the Docker commands you need to know to manage Docker images and containers.

List Running Containers With docker ps

First, seeing what Docker is doing is a good place to start, so let's open with getting a list of running containers. You do this with docker ps:

This system has three running containers. Docker shows you their IDs, names, status, and a few more useful statistics. If you look at the fifth column, you'll see that all the containers are "up."

Let's add the -a argument to docker ps and see if there is a difference:

This command line option added another container to the list, and its status is exited. The -a argument means "all," including containers that are stopped or exited.

Run Containers With docker run

Listing containers is great, but running them is even more fun.

Let's start with a shell running in an Ubuntu container. For this, we use docker run:

We used three sets of command line arguments to start this shell:

  • -it combines i for interactive and t for creating a pseudo-terminal for the shell.
  • --name ubuntu_shell assigns a name to the container, instead of one chosen from random.
  • ubuntu is the name of the image we want Docker to run.

If you don't tell Docker what to name your containers, you'll get a combination of random words. Specifying a name makes it much easier to keep track of what's running.

Docker run has an enormous set of command-line options. Let's look at one more variation before moving on.

Let's run nginx:

We started this container with a different set of command line arguments:

  • -d puts the container in the background. This makes sense with a web server.
  • --name nginx names the container nginx.
  • -p 8080:80 tells Docker to redirect input to port 8080 on the host to port 80 for the container.
  • nginx is the image we want Docker to start for this container.

After starting the container, docker ps -a shows it running, with the port mappings.

Let's retrieve a page from the new web server.

There it is! The web server responds to a request on port 8080.

Stop and Remove Containers

What happens when we exit the Ubuntu shell?

The container transitioned from running to exited, but it's still there.

We need to remove it:

docker rm with the container name removes it from memory. This is a reason to explicitly name your containers: It's easier to figure out what's running and what's stopped.

How do we stop the web server, though? 

docker stop with the container name will stop it. Then you still need to use rm to remove it.

Docker Statistics

Finally, let's look at the resources our containers are using with docker stats:

docker stats shows you how much memory, CPU, and I/O your containers are consuming. It refreshes every few seconds until you quit out of it, so it's a great tool for keeping an eye on a container while you're testing it.

Basic Docker Commands

Getting started with Docker is easy. You can start, stop, remove, and watch your containers with a few simple commands. Get started with containers today!

Command (computing) Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • A Beginner's Guide to Docker Compose
  • A Beginner's Guide to Essential Commands to Fix Container Setup Issues
  • How To Use the Node Docker Official Image
  • Using Environment Variable With Angular

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook