Simple Introduction to Docker

Hannah Do
5 min readSep 30, 2021

--

Docker : Fast & consistent delivery for your application!

Overview

  • Docker is an open platform for developing, shipping, and running applications.
  • Docker can reduce the delay between writing code and deploying it for production.
  • Docker uses light components called “containers” for continuous integration and delivery workflows.
Photo by Kelly Sikkema on Unsplash

Let’s say you’re a developer working on a PC game. For your app to become popular, it would need to be accessible to everyone, meaning it needs to be deployable to all OS types— MacOS, Linux, and Windows.

You can try using your friend’s, your family’s or maybe co-worker’s PC to download the game and test it out. You may also run it in an emulator, or in a Virtual Machine to fix any issues that are found in different OS. However these can be time consuming, costly, and quite uncomfortable.

Docker takes care of this by simply wrapping up your application and handling its deployment, runtime issues, storage management, and many more.

Just like in the picture below, your application (cat) is wrapped up in a container (box) to be run on Docker Engine.

Photo by Piotr Musioł on Unsplash

Docker uses the concept of images and containers.

Image :

Template that can be run by Docker

Images are immutable / read-only files that contain the source code, libraries, dependencies, configuration, environment variables, and other metadata.

Container :

Running image in a certain state

Container is a portable unit in which you can start up an application, which becomes an another process on your machine that has been isolated from all other processes on the host machine.

Note that images can exist without containers, whereas a container needs to run with an image to exist. (image vs container)

In another words, a container with an image becomes a standardized unit of software that allows developers to isolate their app from its environment. (why-docker-containers)

Let’s look at some examples (MacOS)

1. Install Docker

To get started, follow the link to install the right version of Docker to your desktop : https://docs.docker.com/get-docker/

Get Docker for Mac, Windows, Linux

When it’s downloaded, feel free to open it on your desktop and sign up with your credentials — Docker remains free for small businesses, personal use, and education.

2. CLI Commands

Other than the Desktop app, you need to know that Docker mainly works with the command prompt, terminal or a bash window. Try running the following command on your terminal, which will get you started with Docker.

$ docker run -d -p 80:80 docker/getting-started

If it’s run properly, you will be able to find the image generated on the Docker App Dashboard — Images on disk — docker/getting-started.

Let’s also pull an image called whalesay, with a tag name of ‘latest.’

$ docker image pull docker/whalesay:latest

Now you will see both docker/getting-started & docker/whalesay on the Docker Dashboard.

docker / getting-started & whalesay

As previously mentioned, these images cannot be run without a container, therefore we will create a container with the image and run it all in one line.

$ docker container run docker/whalesay:latest cowsay good night

As the code is run, we can see a whale saying “good night” as the parameters were delivered to the container.

whalesay — “good night”

Another interesting example available on docker is danielkraic’s asciiquarium. Following code shows a warning at first — Unable to find image
‘danielkraic/asciiquarium:latest’ locally — however pulls image automatically from danielkraic/asciiquarium and runs the container.

$ docker container run -it --rm danielkraic/asciiquarium:latest

As the result — the ascii aquarium pops up on the terminal window, showing different types of fish, snake, swan, and many creative styles on view. If you would like to exit the screen, simply click Ctrl+C.

danielkraic/asciiquarium

Additional command lines

We can also remove the containers with the following command,

$ docker rm <container name>

name the containers,

$ docker container run -—name <container name> docker/whalesay:latest cowsay <whale talks>

and view the entire list of the containers.

$ docker container ps -a
list of containers (docker)

Notice on the right side of the container list - some of the container names are auto-generated, and are quite memorable if you look closer.

Naming Convention — Docker (Go)

Docker uses the names-generator from Go, with preset adjectives and names. And if you scroll down to the bottom of the code — you can see that they have manually set a secret code so that adjective boring will never be next to wozniak. :)

Why boring_wozniak Will Never Be Generated as a Container Name in Docker? / Guray Yildirim

3. Connect remotely via httpd

Lastly —you can allow the Docker container to run on your local web browser with a given port number.

Run the following code and check your browser by typing the port number (818:80) next to 127.0.0.1.

$ docker container run --name <container_name> --rm -p 818:80 <image_file>
port # 127.0.0.1:818:80

If the container is running, you will see 'It works!’ on your browser with according IP address.

That’s it for a short introduction on Docker, more information is available on Docker documentation.

Photo by Philippe Bout on Unsplash

--

--