DockerGetting Started

Docker

Docker is a software that provides operating system level virtualization called containers.

A container contains all the nescessary code, libraries, and dependencies to run an application. They are considered to be lightweight compared to traditional virtual machines because containers do not virtualize the underlying hardware. Containers share the same kernel as the host OS.

Running a Container

docker run --name <container_name> -p <host_port>:<container_port> <image_name> 

This will pull the latest <image_name> from docker hub and creates a container called <container_name>. The -p flag will publish the <container_port> to <host_port>. Any incoming requests to the host on <host_port> will be forwarded to the container.

Interacting with a Container

docker exec -it <container_name> <command>

This will create a pseudo-terminal connection to <container_name> and execute the <command> specified. The -i flag will keep stdin open so furthur commands can be issued.

Container Network

docker network ls
docker inspect <network_id>

Docker has its own DNS at 127.0.0.11. By default the host can not directly access a container unless the port is published or the container is created under network_mode = host. Inspecting a network will show all containers in the network.

Container Filesystem

docker exec -it <container_name> sh

This allows a shell session to the container where you can freely navigate around the container filesystem.

Volume

Containers are stateless, so data is lost when a container is destroyed. One way to preserve data is to use a volume. Volumes are managed by Docker.

Bind Mount

Bind Mount is like a hard link to a location on the host’s Filesystem. Any changes to the files on the host will reflect in the container.

Bind Mounts are dependent on directory structure and OS of the host machine, so problems will occurr if someone uses Windows while another uses Linux.

Bind Mount vs Volume

I found Bind Mounts to be more effective for development containers since the image size will be smaller and you can take advantage of features like hot-reload in many frameworks. However, Volumes are more portable and is used for production containers.