Skip to main contentIBM Garage Cloud Native Bootcamp

Containers Lecture

Containers

Containers are a standard way to package an application and all its dependencies so that it can be moved between environments and run without change. They work by hiding the differences between applications inside the container so that everything outside the container can be standardized.

For example, Docker created standard way to create images for Linux Containers.

Resources

Basic Docker Commands

ActionCommand
Get Docker versiondocker version
Run hello-world Containerdocker run hello-world
List Running Containersdocker ps
Stop a containerdocker stop <container-name/container-id>
List Docker Images docker images
Login into registrydocker login
Build an imagedocker build <image_name>:<tag> .
Inspect a docker object docker inspect <name/id>
Inspect a docker image docker inspect image <name/id>
Pull an image docker pull <image_name>:<tag>
Push an Image docker push <image_name>:<tag>
Remove a container docker rm <container-name/container-id>

Running Docker on Kubernetes

  1. Make sure you are logged in to your Kubernetes cluster through a terminal or IBM Cloud Shell.
  2. Next, we need to apply the yaml file for the Docker in Docker pod.
    kubectl apply -f https://raw.githubusercontent.com/ibm-cloud-architecture/learning-cloudnative-101/master/static/yamls/containers/dind.yaml
  3. Then, we need to bash into the running pod.
    kubectl exec -it dind bash
  4. Finally check to make sure you can run docker commands, such as docker version

Docker Lecture Walkthroughs

    1. Check the Docker Version
      docker version
    2. Run the `hello-world` container
      docker run hello-world
    3. Check the output:
      Hello from Docker!
      This message shows that your installation appears to be working correctly!....
    1. Start and nginx container and name it `nginx`.
      docker run --rm -d --name nginx -p 8080:8080 bitnami/nginx
    2. Check to see if the container is running
      docker ps
    3. Bash into the running nginx container
      docker exec -it nginx bash
    4. Accessing the running nginx container
      curl http://localhost:8080
    5. Stopping the nginx container
      docker stop nginx
    1. Let's start by creating a directory named assets and add an index.html file to it.
      mkdir -p assets; echo "Welcome to the Cloud Native BootCamp" > assets/index.html; cd assets
    2. Download the sample Dockerfile into the assets directory.
      wget https://raw.githubusercontent.com/ibm-cloud-architecture/learning-cloudnative-101/master/static/yamls/containers/Dockerfile
    3. Build the docker image and name it webserver with a tag of 1.0.
      docker build -t webserver:1.0 .
    4. Run the new image in a container.
      docker run --rm -d -p 8000:8080 webserver:1.0
    5. Access the new container
      curl http://localhost:8000
    1. Login to Docker Hub using YOUR created username and password.
      docker login -u USERNAME -p PASSWORD
    2. Tag the docker image with docker hub username
      docker tag webserver:1.0 USERNAME/webserver:1.0
    3. Push the image to the registry
      docker push USERNAME/webserver:1.0
    4. Run the new image
      docker run --rm -p 8000:8080 USERNAME/webserver:1.0
    5. You can also pull down other images from other users.
      docker pull USERNAME/webserver:1.0
    1. Create a Docker file
      docker run --rm --name nginx -e USERNAME=Carlos -e PASSWORD=IlikeYAML -e LOG_LEVEL=ERROR -p 8080:8080 bitnami/nginx
    2. Create a Docker file
      docker exec -it nginx sh -c "env | grep 'USERNAME\|PASSWORD\|LOG_LEVEL'"
    1. Run the image with a volume attached.
      docker run --rm --name nginx -p 8080:8080 -v $PWD/assets:/app bitnami/nginx

Activities

TaskDescriptionLinkTime
Walkthroughs
What is a Container?A look under the the covers at what is a Linux Container?Understand Containers10 min
What is an Image?A look under the the covers at what is a Linux Container Image?Container Images10 min
Docker BasicsSet of walkthroughs that cover docker basicsDocker Basics10 min
Try It Yourself
IBM Container RegistryBuild and Deploy Run using IBM Container RegistryIBM Container Registry30 min
Docker LabRunning a Sample Application on DockerDocker Lab30 min

Once you have completed these tasks, you should have a base understanding of containers and how to use Docker.