Robust Codes

Getting Started with Docker

April 05, 2020

Docker is a platform to build applications and shiping them to different enviroments

You can see bellow the docker logo and you can thing the whale there as ship that has containers

Docker Logo

You can think containers as vitrual machines at least from the point of view of a regular user but in reality they are function very differently

Containers instead of using physical hardware resources like VMs, the are using operating system recources. Which makes to take up less space and be more efficient

Run a container

Let run a demo http server and explain the passed options

docker container run -p 8080:80 -d --name NginxDemo nginx

We can browse to localhost:8080

http:localhost:8080

And see the default nginx page

Nginx Default Page

Options explained

  • Docker
docker

All docker commands start with the docker keyword calling the docker binari as is defined in your PATH

  • container
docker container

We are going to use a command for manage containers

  • run
docker container run

It creates a new container from specific image and then run the container

  • image
docker container run nginx

nginx here is the name of the image

Images are kind of like a blueprint to build an container instance

The docker deamon here will check if the nginx image exists locally otherwise will download it from docker hub

Then it will create and start a new container with a random name

The container is attached to terminal so you can see the logs from nginx

If you are on bash press ctr+P ctrl+Q to detached from the container

  • --name name
docker container run --name NginxDemo nginx

Insted of a random name the container will named NginxDemo

  • -d
docker container run -d --name NginxDemo nginx

The container starts in detached mode

If you want to attach on it later, run

docker container attach NginxDemo
  • -p host:container
docker container run -p 8080:80 -d --name NginxDemo nginx

The port 8080 on our host machine is mapped to listen the port 80 inside container

Alternative Syntaxes

Let’s take our example

docker container run -p 8080:80 -d --name NginxDemo nginx

Below we can see how to have the exact same functionallity using different commands

  • docker run
docker run -p 8080:80 -d --name NginxDemo nginx

Early versions of docker don’t follow the pattern

docker [command] [subcommand]

  • --option=“name”
docker container run -p 8080:80 -d --name="NginxDemo" nginx

Alternative Syntax to pass argumets

  • registry/image
docker container run -p 8080:80 -d --name NginxDemo docker.io/nginx

Every image it’s strored to a registry

If we are not specify, the default docker hub registry will apply

  • image:tag
docker container run -p 8080:80 -d --name NginxDemo nginx:latest

Every image has a tag

If we are not specify, the default latest will apply

  • docker image pull && docker container create && docker container start
docker image pull nginx
docker container create -p 8080:80 -d \-\-name NginxDemo nginx
docker container start NginxDemo

The docker container run executes all the above commands at once

First checks if the image exists locally or should pull it

Then create and start a new container based on provided options


Personal blog by Alex Kantas