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
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 nginxWe can browse to localhost:8080
http:localhost:8080And see the default nginx page
Options explained
- Docker
dockerAll docker commands start with the docker keyword calling the docker binari as is defined in your PATH
- container
docker containerWe are going to use a command for manage containers
- run
docker container runIt creates a new container from specific image and then run the container
- image
docker container run nginxnginx 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 nginxInsted of a random name the container will named NginxDemo
- -d
docker container run -d --name NginxDemo nginxThe 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 nginxThe 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 nginxBelow we can see how to have the exact same functionallity using different commands
- docker run
docker run -p 8080:80 -d --name NginxDemo nginxEarly versions of docker don’t follow the pattern
docker [command] [subcommand]
- --option=“name”
docker container run -p 8080:80 -d --name="NginxDemo" nginxAlternative Syntax to pass argumets
- registry/image
docker container run -p 8080:80 -d --name NginxDemo docker.io/nginxEvery 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:latestEvery 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 NginxDemoThe 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