This repo contains three sample projects for the docker workshop I ran on 14th of October, 2019. The slides used for the workshop are here.
Check if docker is working.
docker run hello-world
- This pulls down the hello-world image from docker hub.
- Then it creates & runs a new container, based on that image.
- Pull the latest ubuntu image.
docker pull ubuntu:latest
- Run a instance of the ubuntu image (ie. the container).
docker run -it --name ubuntu-container ubuntu
- Stop the container.
docker container stop ubuntu-container
- Remove the container.
docker container rm ubuntu-container
- Remove the image (optional).
docker image rm ubuntu:latest
This example shows you how to run python script in a docker container.
- Build the image.
docker image build -t python-image ./python-app
- Run the container, using the image we just built.
docker container run --name python-container python-image
- Remove the container.
docker container rm python-container
- Remove the image (optional).
docker image rm python:3
We're now building ontop of the previous example. In this example we run a web server that just returns "Hello World!" when we visit http://localhost:5000
Flask is just a python library that allows us to run a web server.
- Build the image.
docker image build -t flask-image ./flask-app
- Run the container, using the image we just build.
docker container run -d -p 5000:5000 --name flask-container flask-image
- -d ~ run as a daemon process.
- -p HOST_PORT:CONTAINER_PORT ~ map host & container port
- Stop the container.
docker container stop flask-container
- Remove the container (perhaps so we can create it again??).
docker container rm flask-container
- Remove the image (optional).
docker image rm flask-image
- Switch to the directory
cd flask-mongodb-app
- Build the containers.
docker-compose build
- Run the containers.
docker-compose up -d
- Stop the containers.
docker-compose down
INSTRUCTION (ARGUMENT)
- ADD copies the files from a source on the host into the container’s own filesystem at the set destination.
- RUN executes command(s) in a new layer and creates a new image.
- CMD sets default command and/or parameters, which can be overwritten from command line when docker container runs.
- ENTRYPOINT sets a default application to be used every time a container is created with the image.
- ENV sets environment variables.
- EXPOSE associates a specific port to enable networking between the container and the outside world.
- FROM defines the base image used to start the build process.
- MAINTAINER defines a full name and email address of the image creator.
- USER sets the UID (or username) which is to run the container.
- VOLUME is used to enable access from the container to a directory on the host machine.
- WORKDIR sets the path where the command, defined with CMD, is to be executed.
- LABEL allows you to add a label to your docker image.