Add a subheading
Add a subheading
What Is Docker?
Stopping a Container
In case you don't believe me that this webpage is being
hosted by you on your local machine, allow me to prove it.
Docker has two commands that we can use to stop a
Docker container from running:
docker stop
This stops the container by issuing a SIGTERM signal
to the container. You'll typically want to use docker
stop. If it doesn't work you can always try the harsher
docker kill.
docker kill
This stops the container by issuing a SIGKILL signal to the
container
Statefulness
Many docker containers are "stateless", or at least
stateless in the persistent sense. That is, when you create
a new container from the same image, it won't store any
information from before. When you restart the
docker/getting-started container, for example, you're
starting from scratch.
That said, Docker does have ways to support a "persistent
state", and the recommended way is through storage
volumes.
Volumes Are Independent of Containers
Ping
We've already done just a bit of networking in Docker.
Specifically, we've exposed containers to the host network
on various ports and accessed web traffic.
Now, let's force a container into offline mode!
Offline Mode
You might be thinking, "why would I want to turn off networking"??? Well,
usually for security reasons. You might want to remove the network
connection from a container in one of these scenarios:
You're running 3rd party code that you don't trust, and it shouldn't need
network access
You're building an e-learning site, and you're allowing students to
execute code on your machines
You know a container has a virus that's sending malicious requests over
the internet, and you want to do an audit
Using the "ping" Utility
The ping command allows you to check for connectivity to a
host by sending small packets of data. Try pinging google.com:
Press ctrl+c to kill the command after the first few pings.
Custom Network
Docker allows us to create custom bridge networks so that our
containers can communicate with each other if we want them to,
but remain otherwise isolated. We're going to build a system
where the application servers are hidden within a custom
network, and only our load balancer is exposed to the host.
Let's create a custom bridge network called "caddytest".
You can see if it worked by listing all the networks:
Each time you run curl, you should get a response from a different
server!
Building Images
I've used Docker in the past to install third-party software; both on my local
machine and on the production servers of companies I've worked for.
However, as a back-end developer, I've more often used it to build images of
my software.
Dockerfiles
Creating a Dockerfile
Create a single file called Dockerfile in your working directory. If you're using VS
Code, I'd recommend installing the Docker extension. It will give you some nice
syntax highlighting among other features.
Inside the Dockerfile add these lines of text:
Build a new image from the Dockerfile:
Dockerizing Python
We need to add the entire Python runtime to our image to
be able to run Python code in a container!
Before the first COPY line, let's install Python. We'll use
the RUN command, and because this is a Debian/Linux
image, we'll use apt to get some dependencies, then we'll
build the Python source code.
I've included the full Dockerfile you'll need here, along
with some annotations about how it works.
# Build from a slim Debian/Linux image
FROM debian:stable-slim
# Update apt
RUN apt update
RUN apt upgrade -y