Docker Primer

Docker Primer #

Think of this as a speed run through getting Docker up and running quickly with minimal fluff and detail.

Installation #

I’m mostly on a Mac. To get started, install Docker Desktop for Mac.

Set up a Docker account as needed.

Version #

Check the version

docker version

DockerHub #

DockerHub contains Docker images.

Testing out Docker #

Whalesay #

Super simple Docker image, similar to cowsay. It’s a good way to test a Docker installation.

docker pull docker/whalesay

docker run docker/whalesay cowsay moo

Note: Looks like this image is no longe supported. :(

Hello World #

This is a Docker official image.

docker pull hello-world

docker run hello-world

If all’s well, it should return a message that starts with

Hello from Docker!
This message shows that your installation appears to be working correctly.

On Docker Desktop, check images that the image is installed.

Containers would show that the image instance is running.

Images #

The templates of containers.

Check for available images #

docker images

Download an image #

docker pull <image>

Remove an image #

Need to ensure no containers are running off the image before the image gets removed. Stop and delete the dependent containers first.

docker rmi <image>

Containers #

Instances of images.

Containers are not meant to host operating systems; they’re meant to run specific tasks or processes. Once the function is done, the container exits.

Containers live only as long as they’re needed.

Run #

Starts a container from an image.

If the image doesn’t already exist, it’ll download it from Dockerhub.

docker run <application>

For more in-depth coverage, check out the Docker run page.

Running check - ps #

Lists running containers, plus details like image ID, image, status, and more.

docker ps 

To see containers that were previously run and perhaps exited already:

docker ps -a

Stop #

To stop a Docker container, referenced using the name or unique identifier that comes out of docker ps

docker stop <container id or name>

Note that the container still exists after a stop, it’s just no longer running.

Remove a container #

docker remove <container id or name>

Sleep #

Add a parameter to a container to go to sleep for a certain number of seconds.

docker run <image> sleep <number of seconds>

Execute a command on a running container #

docker exec <container name or id> <command>

Example:

docker exec <container> cat /etc/hosts

Attach and detach #

This runs a Docker container in the foreground in attached mode, which is connected to the command line and outputs to stdout.

To stop container, hit ctrl + C.

docker run <docker app>

Alternatively, run the container in the background in detached mode. Container runs, and the prompt is interactive.

docker run -d <docker app>

To explicitly attach a container that’s running detached:

docker attach <container name or id>