Part 5 - 15 Docker Commands You Should Know
Part 5 - 15 Docker Commands You Should Know
Follow 586K Followers · Editors' Picks Features Deep Dives Grow Contribute About
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
In this article we’ll look at 15 Docker CLI commands you should know. If
you haven’t yet, check out the rest of this series on Docker concepts, the
ecosystem, Dockerfiles, and keeping your images slim. In Part 6 we’ll
explore data with Docker. I’ve got a series on Kubernetes in the works
too, so follow me to make sure you don’t miss the fun!
There are about a billion Docker commands (give or take a billion). The
Docker docs are extensive, but overwhelming when you’re just getting
started. In this article I’ll highlight the key commands for running vanilla
Docker.
Fruit theme
At risk of taking the food metaphor thread running through these articles
too far, let’s use a fruit theme. Veggies provided sustenance in the article
on slimming down our images. Now tasty fruits will give us nutrients as
we learn our key Docker commands.
Overview
Recall that a Docker image is made of a Dockerfile + any necessary
dependencies. Also recall that a Docker container is a Docker image
brought to life. To work with Docker commands, you first need to know
whether you’re dealing with an image or a container.
Once you know what you’re working with you can find the right
command for the job.
Commmand Commonalities
Here are a few things to know about Docker commands:
For example, docker container run my_app is the command to build and
run the container named my_app. I’ll use the name my_container to refer
to a generic container throughout the examples. Same goes for my_image ,
my_tag , etc.
I’ll provide the command alone and then with common flags, if
applicable. A flag with two dashes in front is the full name of the flag. A
flag with one dash is a shortcut for the full flag name. For example, -p is
short for the --port flag.
The goal is to help these commands and flags stick in your memory and
for this guide to serve as a reference. This guide is current for Linux and
Docker Engine Version 18.09.1 and API version 1.39.
First, we’ll look at commands for containers and then we’ll look at
commands for images. Volumes will be covered in the next article. Here’s
the list of 15 commands to know — plus 3 bonus commands!
Containers
Use docker container my_command
Images
Use docker image my_command
ls — List images.
rm — Delete an image.
Misc
docker version — List info about your Docker Client and Server versions.
Containers
Container Beginnings
The terms create, start, and run all have similar semantics in everyday
life. But each is a separate Docker command that creates and/or starts a
container. Let’s look at creating a container first.
Start
Now that you know how to create and start a container, let’s turn to
what’s probably the most common Docker command. It combines both
create and start into one command: run .
docker container run my_image — Create a new container and start it. It
also has a lot of options. Let’s look at a few.
You need to specify both -i and -t to then interact with the container Top highlight
-p is short for --port . The port is the interface with the outside
world. 1000:8000 maps the Docker port 8000 to port 1000 on your
machine. If you had an app that output something to the browser you
could then navigate your browser to localhost:1000 and see it.
docker container ls -a -s
-a is short for -all . List all containers (not just running ones).
Logs. Not sure how virtual logs are related. Maybe via reams of paper?
Container Endings
Sometimes you need to stop a running container.
To recap, you first create a container. Then, you start the container. Or
combine those steps with docker run my_container . Then, your app runs.
Yippee!
Then, you stop a container with docker stop my_container . Eventually you
delete the container with docker rm my_container .
Images
Here are seven commands for working with Docker images.
Developing Images
docker image build -t my_repo/my_image:my_tag . — Build a Docker image
named my_image from the Dockerfile located at the specified path or
URL.
-t is short for tag. Tells docker to tag the image with the provided tag. In
this case my_tag .
The . (period) at the end of the command tells Docker to build the
image according to the Dockerfile in the current working directory.
Build it
Once you have an image built you want to push it to a remote registry so
it can be shared and pulled down as needed. Assuming you want to use
Docker Hub, go there in your browser and create an account. It’s free.
😄
This next command isn’t an image command, but it’s useful to see here,
so I’ll mention it.
Push
Once you have some images you might want to inspect them.
Inspecting Images
Inspection time
docker image ls — List your images. Shows you the size of each image,
too.
docker image inspect my_image — Show lots of details about your image,
including the layers that make up the image.
Removing Images
docker image rm my_image — Delete the specified image. If the image is
stored in a remote repository, the image will still be available there.
docker image rm $(docker images -a -q) — Delete all images. Careful with
this one! Note that images that have been pushed to a remote registry
will be preserved — that’s one of the benefits of registries. 😃
Now you know most essential Docker image-related commands. We’ll
cover data-related commands in the next article.
Commands are like fruit — nutritious and delicious. Err. Yeah. — Image by silviarita from Pixabay
Misc
docker version — List info about your Docker Client and Server versions.
-a is short for --all . Delete unused images, not just dangling ones.
--volumes Remove unused volumes. We’ll talk more about volumes in the
next article.
Wrap
If you are just getting started with Docker, these are the three most
important commands:
docker container run my_image — Create a new container and start it.
You’ll probably want some flags here.
Containers
Use docker container my_command
Images
Use docker image my_command
ls — List images.
rm — Delete an image.
Misc
docker version — List info about your Docker Client and Server versions.
To view the CLI reference when using Docker just enter the command
docker in the command line. You can see the Docker docs here.
Now you can really build things with Docker! As my daughter might say in
emoji: 🍒 🥝 🍊 🍋 🍉 🍏 🍎 🍇. Which I think translates to “Cool!” So
go forth and play with Docker!
If you missed the earlier articles in this series, check them out. Here’s the
first one:
In the final article in this series we’ll spice things up with a discussion of
data in Docker. Follow me to make sure you don’t miss it!
I hope you found this article helpful. If you did, please give it some love
on your favorite social media channels.
I write about data science, cloud computing, and other tech stuff. Follow
me and read more here.
Docker on! 👏
Thanks to Kathleen Hale.
Every Thursday, the Variable delivers the very best of Towards Data Science: from
hands-on tutorials and cutting-edge research to original features you don't want to
miss. Take a look.
2.3K 2
Your home for data science. A Medium publication sharing concepts, ideas and
codes.
Review the basics and explore advanced concepts. In part 1 we explore the
architecture of our network. In part 2, we code it in Python and go deep into
back-prop & gradient descent. In part 3, we apply it to the Wisconsin Cancer
Data-set.
Let’s go…
708 7
97
924 1
What do you do ?
This is a difficult one for Data Scientists to answer, although they’re often
asked this very question which is why we’re writing this article. We will
try to explain what they do for a living while covering the basics of
Artificial Intelligence. So, we’re going to focus this article on what is a
Data Scientist, but also around the common misconceptions regarding
Artificial Intelligence in general.
This is not a tutorial on how to become a data scientist, or how to get
started with AI in general. …
180
In…
420 3
How do you turn 20 into Algorithm Tool Kit: ETL++: Reinvigorating Lawyers in Utah
50000 in 6 months ? Sorting the Data Integration Micha Turpin
Julia junemoye David Bae in
Market
DataDrivenInvestor John Morrell in Datameer inc
MAGENTO — An How we can create AWS Proton: First look at WTIA Virtual Career Fair
Ultimate E-Commerce documentation for API the preview edition on October 27
Platform while development in Vaibhav Rajput Green River Devs in Green River
Learn Vern
simple way Swagger Web & Mobile Developers
(PHP)
Satish A