Giter VIP home page Giter VIP logo

docker-examples's Introduction

Docker Examples - by geerlingguy

Build Status

The web is full of Docker examples and tutorials and repos.

There are many like it, but this one is mine.

Philosophy

I like learning from first principles. Docker masks a surprising amount of complexity, and most tutorials try to gloss over them to show 'the cool shiny things' before you can even understand what's going on.

I'd rather start really simple, and build from there until I fully understand what's going on. Therefore the examples in this repo build on each other until we get to some actual 'this could do something useful' kinds of infrastructure.

Installation

  1. Install Docker for Mac.
  2. Start Docker.app
  3. Open Terminal, make sure it's running with docker --version.

Getting Started

First Docker command

To kick the tires and make sure things are working, run:

docker run hello-world

This command is doing the following:

  • docker - The main Docker command.
  • run - Run a container.
  • hello-world - The name of the Docker Hub repository to pull from. In this case, we'll get the latest hello-world Docker image. If you don't specify a version, this is interpreted as hello-world:latest.

If things are working correctly, you should see some output, then the container will exit.

First real-world example - Simple Nginx Webserver

Docker's tutorial provides a simple example of running an Nginx webserver on localhost with the command:

docker run -d -p 80:80 --name webserver nginx

This command is doing the following:

  • docker - The main Docker command.
  • run - Run a container.
  • -d - Run a container detached; when the process (nginx, in this case) exits, the container will exit.
  • -p 80:80 - Publish or expose a port ([host-port]:[container-port], so in this case bind the container's port 80 to the host's port 80).
  • --name webserver - Assign a name to a container.
  • nginx - The name of the Docker Hub repository to pull from. In this case, we'll get the latest nginx Docker image. If you don't specify a version, this is interpreted as nginx:latest.

The first time you run this command, it will download the Nginx Docker image (it actually downloads a few 'layers' which build up the official image), then run a container based on the image.

Run the command, then access http://localhost:80/ in a web browser. You should see the 'Welcome to nginx!' page.

Playing with the Nginx container

  • Run docker ps to see a list of running containers; you should see the Nginx container you just started in the list.
  • Run docker stop webserver to stop the named container (you can also use the 'container ID' if you want).
  • Run docker ps -a to see a list of all containers on the system, including stopped containers.
  • Run docker start webserver to start the named container again.
  • Run docker rm webserver to delete the container entirely (you can also pass --rm to the run command if you want the container deleted after it exits).

Note: Starting and stopping a container is usually quicker than building it from scratch with docker run, so if possible, it's best to generate the container with run once and use start/stop until you need to rebuild the container.

Second real-world example - Simple Python Flask App

Docker has another tutorial that digs a little deeper into Docker CLI usage, but for our purposes, we'll just run the main command, and this time allow Docker to map an ephemeral port (any available high port number on our host) to the port configured in the container's configuration:

docker run -d -P training/webapp python app.py

Besides the obvious, this command is doing a couple new things:

  • -P - Publish all ports that are EXPOSEd by the docker container to ephemeral ports on the host (unlike -p, which requires specification of each port mapping).
  • training/webapp - The name of the Docker Hub repository to pull from. In this case we'll get the latest training/webapp Docker image.
  • python app.py - This is the command that will be run inside the container when it's launched. Until the app.py exits, or you docker stop or docker kill the container, it will keep running.

Once the container is started, run docker ps to see what host port the container is bound to, then visit that port in your browser, e.g. http://localhost:32768/. You should see the text "Hello world!" in your browser.

Since we didn't specify a --name when we ran this docker run command, Docker assigned a random name to the container (in my case, romantic_bell), so to stop, rm, or otherwise interact with the container, you have to use the generated name or the container ID.

Other Essential commands

At this point, you should be somewhat familiar with the main Docker CLI. Some other commands that come in handy at this point are:

  • docker images: Show a list of all images you have downloaded locally.
  • docker rmi [image-name]: Remove a particular image (save some disk space!).
  • docker logs [container-name]: Tail the logs (stdout) of a container (try this on the Flask app while refreshing the page!).

Diving Deeper

Other examples warrant their own dedicated directories, with example code and individual detailed README's explaining how they work. Included examples:

  • /flask - Python Flask and MySQL.
    • Introduces docker-compose.
  • /php - PHP-FPM and Nginx.
    • Introduces extra package installation.
    • Introduces HEALTHCHECK.
  • /symfony - Symfony and SQLite.
    • TODO.
  • /traefik - Traefik proxy.
    • Introduces proxying of traffic for multiple hostnames on one port.
    • Introduces the .env file.

License

This project is licensed under the MIT open source license.

About the Author

Jeff Geerling is the author of Ansible for DevOps and manages tons of infrastructure, as well as open source projects like Drupal VM.

docker-examples's People

Contributors

geerlingguy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

docker-examples's Issues

why a 'data' container in the php+nginx example ?

I was curious why your nginx+php-fpm example uses a separate 'data' container based on busybox to provide the volumes. When I run the example via docker-compose the data container exits immediately, but the volumes work fine between nginx and the php-fpm container of course. This isn't a pattern I've seen documented before, is there a particular reason for the third tiny container ?

Add Drupal example

Something more real-worldish, and more Dockerish, than examples I've seen so far (e.g. no mega-container with Apache+PHP+Composer+everything-under-the-sun inside).

If you're going to use Docker, don't jam in all the bad things about legacy deployment into one massive 1.2+ GB container!

Also, if there's any easy way to work on simple configurability (á la Drupal VM to make something like a 'Drupal VM lite', I'd like to explore it. So far most of the good Docker Drupal examples I've seen are a pain to customize/adapt, while most of the flexible Docker Drupal examples I've seen are slow, bloated, messy, and offer very little benefit over using plain old VMs.

Add PHP-FPM + Nginx Example

In this example, I'd like to highlight customizing base images using Dockerfiles, especially installing extra packages (the right way) and adding a HEALTHCHECK.

Explore automatic host-to-container DNS resolution/routing

There are some interesting solutions to the problem of "how do I use friendly domain names that can be automatically/easily configured" with Docker containers. Some important background:

Add Symfony Example

Symfony is a PHP framework for web projects.

I'd like to add an example that builds a generic 'hello-world' Symfony app and connects to SQLite in a separate container.

One interesting aspect (and one about which I wrestle with in the 'this-isn't-something-for-prod-therefore-why-is-it-done-in-acontainer' way) is the idea of doing Composer work inside Docker.

IMO, it would be better to do all the app build process in a separate pipeline, then run the app in Docker containers. Technically the build process could be managed in some Docker containers... but you should always run your app in the exact same way as Production. And Production should not have Composer, nor should it be involved in your build process (especially since that saps up resources!).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.