Docker and Containers

Do you know what a container is? If you said it was a box that held things, you are right! If you think containers are cool on ships and trucks you are gonna freak when you hear how cool they can be in computers. In addition to socks and old cds, containers are a way to package up code and all of its dependencies so that it can be easily moved and run on any machine.

What do containers do?

Just like in life, containers are a way to combine a set of things you want to keep around. This is great for developers because it means that they can easily move their code from one machine to another. This is great for users because it means that they can easily run code on their machine without having to worry about installing all of the dependencies since the container takes care of that.

What is Docker?

When you need a container you need to create a few files that will define its structure and contents. Docker is a tool that makes this easy. It is a command line tool that allows you to create, run, and manage containers. It is also a platform that allows you to share containers with others. You can install docker on most machines and it is available for Windows, Mac, Linux, and more.

Defining a container

You can define containers a few ways. The most common way is to use a Dockerfile or a docker-compose.yaml file. These are both just simple text files that contains a list of instructions that will be used to create the container.

Dockerfile instructions are similar to the commands you would use in the command line. For example, you can use the FROM command to specify the base image that you want to use. You can use the RUN command to run a command in the container. You can use the COPY command to copy files from your machine to the container. You can use the CMD command to specify the what should be run when the container is started. Just to name a few.

Creating A Container

Lets create a container that will run a simple python script. We will use the python:3.8 image as our base image. We will copy the hello.py file from our machine to the container. We will run the hello.py file when the container is started.

Save this as a text file called: Dockerfile in the same directory as the hello.py file.

FROM python:3.8
COPY hello.py . 
CMD ["python", "hello.py"]

What this does it uses the FROM command to specify the base server/machine image. It uses the COPY command to copy the hello.py file from our machine to the container. It uses the CMD command to specify the command that should be run when the container is started. It uses the EXPOSE command to expose port 5000 so that we can access the web server that is running in the container.

In the same directory as the Dockerfile create a file called hello.py with the following contents:

print("Hello World!")

Your files should look like this:

$ ls
Dockerfile  hello.py

Now we can build the container using the docker build command. This will create a container image that we can use to create containers.

$ docker build -t hello-01 .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM python:3.8
3.8: Pulling from library/python
bbeef03cda1f: Pull complete 
f049f75f014e: Pull complete 
56261d0e6b05: Pull complete 
9bd150679dbd: Pull complete 
5b282ee9da04: Pull complete 
03f027d5e312: Pull complete 
41b6012c8972: Pull complete 
e84941e8ff4e: Pull complete 
8848ba81439a: Pull complete 
Digest: sha256:0bdd43369c583eb82a809fbe6908d6ec721b7a29f7d9dce427d70924baf0ab7b
Status: Downloaded newer image for python:3.8
 ---> e96a212a8ca8
Step 2/4 : COPY hello.py .
 ---> b91d741e7741
Step 3/4 : CMD ["python", "hello.py"]
 ---> Running in 64ea2d48f578
Removing intermediate container 64ea2d48f578
 ---> 1223bd956265
Step 4/4 : EXPOSE 5000
 ---> Running in 9e2f664d453c
Removing intermediate container 9e2f664d453c
 ---> ee831a26fe74
Successfully built ee831a26fe74
Successfully tagged hello-01:latest

Command above uses the -t flag is to specify the name of the container image. The . is used to specify the directory that contains the Dockerfile. As you can see the container is call hello-01 and it is tagged as latest. This container has been made into an “image” that we can use to create containers.

Listing Images

After you create a few images it can get hard to keep track to if you want to see a list of all of the images you can use the docker images command.

$ docker images
REPOSITORY                                                      TAG         IMAGE ID       CREATED         SIZE
hello-01                                                        latest      ee831a26fe74   5 minutes ago   915MB

Running a container

To actually use the container we need to create a container from the image. We can do this using the docker run command. This will create a container from the image and start it. It will also print out the output from the container.

 $ docker run hello-01
Hello World!

This container only prints and then comes to an end.

Some containers run ion the background to process incoming requiests or data. If you want to run the container in the background you can use the -d flag. This will start the container and then return to the command line.

Listing Running Containers

You can then use the docker ps command to see the list of running containers.

$ docker ps 
CONTAINER ID   IMAGE                            COMMAND                  CREATED          STATUS          PORTS                                             NAMES
af395402bfce   foobar-flim-flam-website:latest  "/bin/sh -c 'exec gu…"   24 seconds ago   Up 23 seconds   80/tcp, 0.0.0.0:9090->8080/tcp, :::9090->8080/tcp crazy_jennings

Here you can see the container id, the image that was used to create the container, the command that was run, the time that the container was created, the status of the container, the ports that are exposed, and the name of the container.

Stopping a container

If your container is running in the background you can stop it using the docker stop command. This will stop the container gracefully. This means that it will finish any tasks that it is currently working on and then stop. If you want to stop the container abruptly you can use the docker kill command. This will stop the container immediately.

First get your containers id using docker ps or docker ps -a to see all containers

$ docker ps or docker ps -a to see all containers

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED       STATUS        PORTS     NAMES
f2m49d8djf2d   hello-01  "python"  2 hours ago   Up 2 hours    5000/tcp  hello-01   

Then stop the container gracefully using the docker stop command.

$ docker stop <container-id> $ docker stop hello-01

Kill a container abruptly using the docker kill command.

$ docker kill <container-id> $ docker kill hello-01

So What Good Is All This?

What containers allow you to do is to create a place that has all of the dependencies that you need to run your application. You can then share this container with others. They can then use this container to run your application without having to install all of the dependencies on their machine. This is especially useful if you are working on a team and you want to make sure that everyone is using the same version of the dependencies. If you think this is interesting you should check out Docker Hub. This is a place where you can find and share containers that other people have created. You can also create your own containers and share them with others.

For more information on how containers can benefit you and your business contact us