This demo script is intended to walk people new to Docker through running a container from Docker Hub, creating then running their own image, and finally using Docker Compose to automate the running of multiple containers.
- Make sure that you have installed VirtualBox and Vagrant, then clone this repository using Git.
- Open a terminal,
cd
to your cloned repository, then runvagrant up
to start the virtual machine. - Wait a bit while the provisioner runs. It's installing Docker and some other things.
- Run
vagrant ssh
to open an SSH session to the virtual machine.
- Start by running
docker run alpine
. You'll see Docker pulling in the image and it will then launch your container. - It ran - but it just kicked you straight out.
docker run -it alpine
and have a look around. The-i
and-t
switches open an interactive session and allocate a TTY, allowing you to have a proper console session inside of the container.- Press CTRL+D to exit the container terminal and return to the virtual machine.
- First of all,
cd /vagrant
as that's where the Dockerfile is. - To build the container image, run
docker build .
- note the last dot: that's the build context which in this case is the current directory. - Docker will now build the container, doing what is defined in the Dockerfile.
- Once it's built,
docker image list
reveals our image, but it's only referred to by hash. - Rebuild the image, but this time add a tag - run
docker build --tag webapp .
. - Notice that the build was much quicker - almost instant. This is because Docker is able to determine that nothing has changed and simply rebuilds the image using a cache.
- Run
docker image list
again to show the image with a more usable name.
- With the image built, it can be run by executing
docker run --name=webapp-container webapp
. - Running that command will result in an attached session - you'll be able to
see the container's
stdout
stream. - The server says it's listening! Browse to http://localhost:5000 to try and load the page.
- It's likely that will result in a connection reset error. This is correct, as no ports are being forwarded to the container - the Docker daemon has no idea where to route this traffic.
- Press CTRL+C to kill the container, then run
docker run --name=webapp-container -p5000 webapp
. - Try browsing to http://localhost:5000 again - you should see a web page now.
- Kill the container and start it again. Notice how the ID has changed.
- Make sure that any other containers are killed - run
docker ps -a
to list containers anddocker rm -f $CONTAINER_NAME
to remove them. - Run
docker-compose up -d --scale webapp=4
. - Browse to http://localhost and refresh. Nginx is load balancing your requests across four containers.