umma.dev

Docker for Beginners

A very high level overview of docker.

What is Docker?

Docker is a tool that enables you to deploy applications, which are run on operating systems such as Linux.

Getting Started

You need to install docker onto your machine. You can find the guide to set installing docker here.

What is an Image?

This is a buleprint of the application which forms the basis of containers.

What are Containers?

Containers are created from Docker images and run the application. Containers are created via docker run.

What is a Tag?

This is the latest version that has been deployed.

Deploying a Web App

First, download and run the image.

docker run --rm -it [path-to-site-folder]

You should be able to see Nginx is running... in the terminal.

Now that the server is running, we want to be able to see the website and what port it’s running on. You can rerun the docker run command to publish ports.

docker run -d -P --name [container-id] [path-to-site-folder]

The command above will:

  • Detach the terminal (-d)
  • Publish all exposed ports to random ports (-P)
  • --name corresponds to a name we want to give

To see the ports running, enter the following command:

docker port [container-id]
80/tcp -> 0.0.0.0:3542
443/tcp -> 0.0.0.0:3541

If you navigate to http://localhost:3542 you should be able to see the website. You can specify a customer port via:

docker run -p 8888:80 [path-to-site-folder]
Nginx is running...

To stop a detached container, run:

docker stop [container-id]

Use the docker pull command to pull the latest image down.

Dockerfile

A Dockerfile is a simple text file which contains a list of commands that the Docker client calls while creating an image.

Here is a sample docker file:

FROM python:3.8

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# define the port number the container should expose
EXPOSE 5000

# run the command
CMD ["python", "./app.py"]

When you run docker build, it will automatically create a Docker image from a Dockerfile.

Here is a sample output:

docker build -t yourusername/catnip .
Sending build context to Docker daemon 8.704 kB
Step 1 : FROM python:3.8
# Executing 3 build triggers...
Step 1 : COPY requirements.txt /usr/src/app/
 ---> Using cache
Step 1 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Using cache
Step 1 : COPY . /usr/src/app
 ---> 1d61f639ef9e
Removing intermediate container 4de6ddf5528c
Step 2 : EXPOSE 5000
 ---> Running in 12cfcf6d67ee
 ---> f423c2f179d1
Removing intermediate container 12cfcf6d67ee
Step 3 : CMD python ./app.py
 ---> Running in f01401a5ace9
 ---> 13e87ed1fbc2
Removing intermediate container f01401a5ace9
Successfully built 13e87ed1fbc2

Lastly, run the image to check it works.

docker run -p 88888:5000 username/folder

When this command runs you should be able to see:

Running on http://0.0.0.0.5000