Tutorial 6.1.2: Docker - McGill-ECSE429-Winter2022/tutorials GitHub Wiki

1. Introduction

1. What and Why?

You can think of Docker as a platform to develop, deploy and run applications with containers. This means that your application works in exactly the same environment whether that's on your computer or someone else's.

2. Installation

  1. Install docker on mac: Steps to follow
  2. Install docker on windows: Steps to follow

2. Working with Docker

1. Using Images

Docker Image Commands:

  • docker image ls -a
  • docker image rm <image_id>
  • docker image rm $(docker image ls -a -q)

2. Running a Container

A Container is a running Docker image. So in this section on containers, let's create our own Docker file that we'll run in a container. Let's use the Ubuntu image from the previous demo, and let's add Python on top.

Follow on-screen demo for the following commands:

  • docker container run first .
  • docker container run -it first .
  • docker container ls
  • docker container ls -a -q
  • docker container rm

3. Docker Under the Hood

The docker daemon runs on your host operating system. A daemon is just a computer program that runs as a background process. The daemon exposes a REST API. This REST API specifies interfaces which other tools can use to talk to the daemon and tell it what to do. The command-line interface, or docker CLI, is the most common means of communicating with the docker daemon. The docker daemon creates and manages Docker objects. These are images, containers, networks, and volumes. So this is a client server architecture. The daemon is the server and is always listening out for any requests, and the docker CLI that we've been using all along is one of the clients that you can use to communicate with the server and tell it what to do.

  • docker container run hello-world

3. Working with Dockerfiles

1. Basics

Docker builds images automatically by reading the instructions from a Dockerfile in order. So let's look at the components of a Dockerfile.

  • FROM (image-name): Dockerfile usually starts with a FROM instruction. The image can be any valid image.

  • LABEL <key=value>:

You can also add labels to your image to help organize images by project. The label instruction adds metadata to an image and is a key value pair. You can include spaces within a label value by using quotes and run across several lines using a backslash

  • WORKDIR /dir

The WORKDIR command sets the working directory for any run, command, entry point, copy and add instructions that fall in the Dockerfile. Now if the working directory doesn't exist it will be created even if it's not used in any further command.

* COPY . /data
The COPY instruction copies new files or directories from the source and adds them to the file system of the container at the path destination.

* RUN cmd
The RUN instruction will execute any commands in a new layer on top of the current image and commits the results. The image that you'll get will be used for the next steps in the Dockerfile, so it's a good idea to have fewer RUN statements and split long or complex RUN statements across multiple lines separated with backslashes this makes your Dockerfile more readable

RUN pip install jupyter &&/
pip install pandas &&/
pip install matplotlib

* **EXPOSE **
The EXPOSE instruction indicates the ports in which a container listens for connections so it's always a good idea to use well known traditional ports for application where possible. The EXPOSE instruction is really a message from the creator of the container to whoever uses it. You're telling them which ports will need to be used. However, you will only publish the port when you use the dash P flag when running a container.

* CMD ['command1','command2']

2. Uploading images to Docker Hub

Now one of the easiest ways to share your work with other people is to push the images to Docker Hub.
Step 1: Create a docker image in the local machine. (For this demo we have pulled a small hello-world image from the standard repo).
Step 2: Tag the imageid with your dockerid using the command docker tag image_id docker_id:tagname .
Step 3: Login to docker hub from the cmd using command docker login .
Step 4: Push the local image to the docker hub using command docker image push repository_name:tagname .
Step 5: Verify in the docker hub.
Step 6: Now to test the uploaded image, copy the pull command from your user repo and run. e.g docker pull neeraj587/dockerhub:ecse429 .
Step 7: Once the pull is done, run the image using command docker container run neeraj587/dockerhub:ecse429 .
Step 8: Verify the container in your docker application or website.
The below screenshot shows the overview of the above steps.

Reference for more exploration.

⚠️ **GitHub.com Fallback** ⚠️