Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We'll use a Dockerfile to build a new image, then run a new container based on that image.
Here's a Docker configuration file that builds a MongoDB service. Save it in a new directory, and name it Dockerfile
, with a capital "D" and no file name extension.
# Dockerfile for building a MongoDB service
# Pull base image.
FROM mongo
# Define mountable directories.
VOLUME ["/data/db"]
# Define working directory.
WORKDIR /data
# Define default command.
CMD ["mongod"]
# Expose ports.
# - 27017: process
# - 28017: http
EXPOSE 27017
EXPOSE 28017
- The
FROM
line specifies that this image will be based on another image, namedmongo
, that has MongoDB already installed. - The
CMD
line specifies a command that will be run when the container starts. In this case, it's the MongoDB service. - And the
EXPOSE
lines expose network ports within the container to the host operating system, so that other apps can make network connections to the apps running inside the container.
With this Dockerfile
saved to a directory, we can use the docker
command from our terminal to build an image that our Mongo containers will be based on. We just run the docker
command with the build
subcommand, and then pass the -t
flag to tag the image. We'll use an image name of "mongotest". Finally, we'll have it run in the current directory, which contains our Dockerfile
.
docker build -t mongotest .
Now, let's create a new container based on the image so we can try it out. We run the docker
command again, this time with a subcommand of run
.
In order to be able to communicate with MongoDB, we need to add a couple things to the command line. Even though we exposed ports 27017 and 28017 in the Docker image, those ports won't be accessible unless we also publish them, that is, make them accessible via a port on the host OS. So let's publish the exposed port 27017 first. We need to pass the -p
flag, which stands for "publish", 27107
, a colon, and the number of the exposed port on the container, which is also 27017
. Then we'll do the same for port 28017: -p 28017:28017
. Lastly, we provide the name of the image we want to base our container on: mongotest
.
docker run -p 27017:27017 -p 28017:28017 mongotest
New Terms
- Software Delivery Pipelines -- When an app is setup so that it’s easily sent through the process of build, test, and deployment. Often referred to as CI or CD (Continuous Integration or Continuous Delivery).
- Dockerized App -- An app that has a Dockerfile made for it and can be built into a Docker image and run as a container.
- Container -- You can think of a container for an app as a real-life shipping container for freight. An app container is also like a VM, but far more lightweight and with the same security and operational isolation from system resources.
-
0:00
You'd expect any software that could serve the needs of Facebook and
-
0:03
Google would be hard to use, right?
-
0:05
Using Docker is actually easy.
-
0:07
Here's a Docker configuration file that builds a Mongo DB service.
-
0:11
See the teacher's notes of you'd like to download a copy yourself.
-
0:15
This file should be named Docker file with the capital D and
-
0:17
no extension and saved in a directory on your host machine.
-
0:21
The docker file is used to set up an image, a self-contained package, that
-
0:25
includes an operating system and all the other dependencies your app needs to run.
-
0:29
Don't worry about all the details of this file right now, but
-
0:32
let me give you a quick overview of what it does.
-
0:34
The FROM line specifies that this image will be based on another image named Mongo
-
0:39
that has Mongo DB already installed.
-
0:41
The command line specifies a command that will be run when the container starts.
-
0:45
In this case, it's the MongoDB service.
-
0:48
And the exposed lines is expose network ports within the container to the host
-
0:52
operating system.
-
0:54
This lets other apps make network connections to the apps
-
0:56
running inside the container.
-
0:58
With this docker file saved to a directory, we can use the docker command
-
1:02
from our terminal to build an image that our mongo containers will be based on.
-
1:06
We just run the docker command with the build subcommand and
-
1:10
then pass the -t flag to tag the image.
-
1:14
We'll use an image name of mongo test.
-
1:18
Finally we'll have it running the current directory which contains our docker file.
-
1:22
When we run this command, Docker will go through all the instructions in the Docker
-
1:26
file and carry them out.
-
1:27
A process it may take a little while.
-
1:30
It downloads the Mongo image to use as a base 6,
-
1:33
sets mongodb up to run by default and exposes our requested ports.
-
1:38
None of these changes are made to the host operating system by the way.
-
1:42
It's all happening inside the image.
-
1:45
Now let's create a new container based on the image so we can try it out.
-
1:49
We run the docker command again.
-
1:53
This time with a sub command of run.
-
1:55
In order to be able to communicate with Mongo DB,
-
1:58
we need to add a couple things to the command line.
-
2:01
Even though we exposed ports 27017 and 28017 in the Docker image,
-
2:07
those ports won't be accessible unless we also publish them.
-
2:10
That is, make them accessible via a port on the host OS.
-
2:14
So let's publish the exposed port 27017 first.
-
2:18
We need to pass the -p flag, which stands for publish, to docker run.
-
2:24
Then we need to type the port on the host that we want to publish the exposed
-
2:28
port as.
-
2:29
We could publish it on a different port number than we exposed, but
-
2:33
we'll just keep it the same and publish it at 27017.
-
2:36
Then we type a colon, and
-
2:38
the number of the exposed port on the container which is also 27017.
-
2:43
Then we'll do the same for port 28017,
-
2:48
publish 28017 as 28017.
-
2:51
Lastly, we provide the name of the image we want to base our container on which is
-
2:56
mongotest.
-
3:00
The container will be created.
-
3:02
And as we specified in our docker file,
-
3:04
the mongod command will be run within the container.
-
3:07
And because we published the two exposed ports, we can switch to another terminal
-
3:11
window and connect to those ports with the mongo client.
-
3:15
We can then interact with the service running on the container just as we would
-
3:19
anywhere else.
-
3:20
So let's run the show dbs command.
-
3:23
And it shows our available databases.
-
3:25
And let's choose exit to exit out of the client.
-
3:29
But the Mongo service running in your container isn't just accessible from your
-
3:33
local machine.
-
3:34
It can be reached over the network by any existing service.
-
3:38
If you want, it could be deployed into any production environment.
-
3:41
Now imagine applying the same process to all the other software
-
3:44
your organization runs.
-
3:47
No longer would your team to have to pass around brittle scripts and build files or
-
3:51
match many different dependencies together.
-
3:54
With Docker you can isolate your various services and deploy them with ease.
-
3:58
And when you need to scale up the number of instances or distribute
-
4:01
them over the network differently, Docker will make that easy too.
You need to sign up for Treehouse in order to download course files.
Sign up