Skip to content

Docker Basics

Michael_Scopic edited this page May 31, 2022 · 16 revisions

Your local, friendly and simplified Docker wiki.

Table of contents:

What is Docker?

What is a container?

Basic Docker commands

Running containers

Running a detached container

Getting a container's ID

Stopping a container

Attaching a container

What is Docker?

Docker is software to allow you to run services as containers. These containers are completely isolated from the host machine, but still tightly integrated with the host (as in you can interact with the container), they are extremely lightweight compared to virtual machines, and they are portable.

What is a container?

A container is pretty much a stripped down version of a virtual machine (aka vm). If you are using an EC2 instance in AWS, those EC2 machines are actually virtual machines.

The main difference between a virtual machine and a container is that containers are more lightweight than vm's.

They are able to accomplish this because they lack an init system, daemons, have a very minimal set of packages installed by default, and lack any other users (by default) besides root.

In addition to this, most containers have only one focus rather than multiple others, unlike their virtual machine counterparts.

Docker basics

These are commands that you will usually be using with Docker:

docker help
# shows Docker help menu

docker images
# shows your images

docker rmi image_foo
# removes an image (replace "image_foo" for the image you want to remove)

docker ps
# shows status of running containers

docker run 
# runs a container

docker container ls
# shows your containers

Running Docker containers

To start a quick container you can run docker run -it image_foo (replace "image_foo" for the image you want to use)

If you want Docker to remove the container after you are done using it, add the --rm option to it.

docker run -it --rm debian

When you want to get out of the container, type exit at the command prompt.

exit

Running a detached container

If you want to run a container but don't want it to hog your terminal, run it in detached mode. You can do this with the -d option.

docker run -it -d debian

HINT: You can combine options into one option!

For example, you already combined -i and -t into -it, and you can combine that with -d, so a complete command could look like: docker run -itdp 8081:8080 ubuntu

This is the same as typing: docker run -i -t -d -p 8081:8080 ubuntu.... long and messy, huh?

Getting the container ID

Running docker ps will show you your running containers, along with their container ID.

docker ps

If you don't see your container there, add the -a option to the previous command to show all containers, running or stopped.

docker ps -a

Stopping containers

When you want to stop a container, run docker stop followed by the container's ID that you got when you ran docker ps.

docker stop 7d579c28
  • replace "7d579c28" with the ID that you want to stop. This will not match your system.

Attaching containers

If you exited or put a container in the background (with -d), you may need to back to that container.

You can do that with docker attach, and will attach the container to your terminal.

docker attach image_foo

Clone this wiki locally