Docker Basics

Docker is a very popular technology that significantly streamlines software deployment which makes it an excellent choice for cloud orchestration platforms such as Kubernetes. 



Exercise 1: Hello world

Suppose that we have a bash script named helloworld.sh that prints "$NAME: Hello World!". 

  1. Build a docker image
    • based on Ubuntu 18.04.
    • Set the working directory to /usr/src/app
    • Copy helloworld.sh to the working directory
    • Set environment variable NAME to your name. 
    • Make the script executable using chmod command.
    • Run the script when the container starts. 
  2. List all images. 
  3. Run a container with name hw, with your image.
  4. List all containers.
  5. Stop and remove the container. 
Jump to solution

Exercise 2: Keep container running

  1. Run a container with name hw with your image, and detach it and keep it running.
  2. Bash to the container and run the helloworld script. 
  3. Stop and remove the hw container. 

Jump to solution

Exercise 3: Mount a folder 

  1. Create a folder named 
  2. Run the container again and mount the folder to it. 
  3. Inspect the container to make sure the folder has been mounted. 
  4. Bash into the container and put a file in the mounted folder. 
  5. Check the file in the host. 
  6. Stop and remove the hw container.

Jump to solution

Exercise 4: Deploy a hello-world server

  1. Write a nodejs webserver that listens on port 3000, and responds "Hello World"
  2. Create an image that runs this webserver upon running. 
  3. Run a container with name hw, with your image in the background.
  4. Go to http://localhost:3000, you must see "Hello World".
  5. Stop and remove hw container.

Jump to solution

Exercise 5: Push your image to a registry

  1. Login to your dockerhub account via docker
  2. Push your local helloworld image to your dockerhub repository.
  3. Remove your helloworld image. 


Solutions

We first create a Dockerfile in the folder that we have our HelloWorld.sh script. 
Now, we run the following command in the same folder: 
docker build --tag helloworld . 

To list all images: 
docker image ls 

To run a container out of this image: 
docker run --name hw helloworld

To list containers:
docker container ls

To stop the container:
docker stop hw

To remove the container:
docker rm hw
First, comment out the CMD line in the Dockerfile, and create a new image. 

To run the container in background and keep it running:
docker run 

To bash to the container:
docker exec -it hw bash

Exercise 3

To mount the folder: 
docker run

To inspect: 

docker inspect hw

and check "Mounts". 

Exercise 4

Nodejs webserver:

Note that, you should not specify hostname as localhost, as this way you cannot access the webserver from the host machine. 

First, we create a Dockefile from node official image. We name it helloworld_server


To run the image and make port 3000 accessible for it:
docker run --publish 3000:3000 --detach --name hws helloworld_server

Now, http://localhost:3000 must show "Hello World". 

Create an account on https://hub.docker.com/.

To login:
docker login --username=myusername

Now, first, we tag our existing image: 
docker tag helloworld myusername/myrepo:new_tag
Note that the new_tag is optional. 

To push:
docker push myusername/myrepo:new_tag
To remove the image:
docker image rm helloworld


Exercises for Backend Technologies Series

In this series, I will share exercises to practice popular backend technologies. (See all exercises)


Comments

Popular posts from this blog

In-memory vs. On-disk Databases

ByteGraph: A Graph Database for TikTok

DynamoDB, Ten Years Later

Eventual Consistency and Conflict Resolution - Part 2