-
Notifications
You must be signed in to change notification settings - Fork 84
Docker Basics
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.
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.
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 containersTo 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 debianWhen you want to get out of the container, type exit at the command prompt.
exit
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 debianHINT: 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?

Running docker ps will show you your running containers, along with their container ID.
docker psIf you don't see your container there, add the -a option to the previous command to show all containers, running or stopped.
docker ps -aWhen 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.

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