Get Started with Docker CE with example
Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications.
In other words, Docker allows you to create (almost) anything without starting from scratch. Instead of creating one (gigantic) virtual machine with all the applications we need, we create multiple tiny containers. These containers are pre-created and ready to use.
For more information about Docker, see their what-docker page.
Let's get started!
Download Docker CE
First we need to download docker community edition.
Once docker is installed, you can verify the installed version by running:
docker version
This will show something like:
Client: Version: 18.05.0-ce API version: 1.37 Go version: go1.9.5 Git commit: f150324 Built: Wed May 9 22:17:48 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.05.0-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: f150324 Built: Wed May 9 22:15:57 2018 OS/Arch: linux/amd64 Experimental: false
The hard part is over!
Quick example
Docker offers a massive library of applications, server applications and other tools you can get and start your project right away.
In this article, we will use nginx and build a web server.
To build the container we can either run it using the
docker command or the
docker-compose command.
Both commands will eventually provide the same result. This all depends on how you want to orchestrate your environment.
docker command
In order to use the docker command, you will need to provide all parameters required for docker to create the container.
Example:
docker run --name my-nginx \ -h hostname.com -l my-label -p 8081:80 -v /my/path/to/nginx.conf:/etc/nginx/nginx.conf \ -v /my/path/to/html/:/usr/share/nginx/html \ -d nginx:latest
docker-compose command
The different with docker-compose is it requires a yaml file.
version: "3" services: my-nginx: image: nginx:latest container_name: bookofzeus labels: name: "my-label" hostname: "hostname.com" ports: - "8081:80" volumes: - /my/path/to/nginx.conf:/etc/nginx/nginx.conf - /my/path/to/html/:/usr/share/nginx/html
Then to start the container, simply run:
docker-compose -f mynginx.yml up -d
Understanding the variables
Images
A images is a file that is a snapshot of a container. Images once created by the build command, they will produce a container.
Port
What does the dual port mean? The first port is the port you need to use to connect to the container. The second port is the container port where the service is running. Read more about this docker networking.
Volumes
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. In our case, we will have our local configurations and html pages in our host.
The important thing to know here is if the -d
is not provided, the instance will start NOT detached.
This mean you will be able to see what's going on in the container. To close, use CTRL+C
Navigate to the website
Before you go the web server, make sure you have HTML pages in your local volume (/my/path/to/html/).
Now that the web server is running, you can connect to the 0.0.0.0 to it using the port 8081
http://0.0.0.0:8081
Teardown
To stop the container, if the -d
option was provided, you can run the following command:
docker-compose -f mynginx.yml stop